Class: Nyara::ParamHash

Inherits:
Hash show all
Defined in:
lib/nyara/hashes/param_hash.rb,
ext/hashes.c

Overview

Keys ignore case and access is indifferent between String keys and Symbol keys.
All keys are stored in String form.

Direct Known Subclasses

ConfigHash, HeaderHash, Part, Session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_param

Class Method Details

insert parsing result into output


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'ext/hashes.c', line 265

static VALUE param_hash_parse_cookie(VALUE _, VALUE output, VALUE str) {
  Check_Type(output, T_HASH);
  Check_Type(str, T_STRING);
  if (rb_obj_is_kind_of(output, nyara_header_hash_class)) {
    rb_raise(rb_eArgError, "can not parse cookie into HeaderHash");
  }
  const char* s = RSTRING_PTR(str);
  long len = RSTRING_LEN(str);

  // split with /[,;]/
  // scan in reverse order because entries on the left of the cookie has greater priority
  long i = len - 1;
  long last_i = i;
  for (; i >= 0; i--) {
    if (s[i] == ',' || s[i] == ';') {
      if (i < last_i) {
        _cookie_kv(output, s + i + 1, last_i - i);
      }
      last_i = i - 1;
    }
  }
  if (last_i > 0) {
    _cookie_kv(output, s, last_i + 1);
  }
  return output;
}

.parse_paramObject

insert parsing result into output


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/hashes.c', line 317

static VALUE param_hash_parse_param(VALUE _, VALUE output, VALUE str) {
  Check_Type(output, T_HASH);
  Check_Type(str, T_STRING);
  if (rb_obj_is_kind_of(output, nyara_header_hash_class)) {
    rb_raise(rb_eArgError, "can not parse param into HeaderHash");
  }
  const char* s = RSTRING_PTR(str);
  long len = RSTRING_LEN(str);

  // split with /[&;]/
  long i = 0;
  long last_i = i;
  for (; i < len; i++) {
    if (s[i] == '&' || s[i] == ';') {
      if (i > last_i) {
        _param_kv(output, s + last_i, i - last_i);
      }
      last_i = i + 1;
    }
  }
  if (i > last_i) {
    _param_kv(output, s + last_i, i - last_i);
  }
  return output;
}

.split_nameObject

"a[b][][c]" ==> ["a", "b", "", "c"]


102
103
104
105
# File 'ext/hashes.c', line 102

static VALUE param_hash_split_name(VALUE _, VALUE name) {
  Check_Type(name, T_STRING);
  return _split_name(name);
}

Instance Method Details

#[]Object

just return simple hash like rack


26
27
28
29
30
31
# File 'ext/hashes.c', line 26

static VALUE param_hash_aref(VALUE self, VALUE key) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return rb_hash_aref(self, key);
}

#[]=Object


40
41
42
43
44
45
# File 'ext/hashes.c', line 40

static VALUE param_hash_aset(VALUE self, VALUE key, VALUE value) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return rb_hash_aset(self, key, value);
}

#_arefObject

#_asetObject

for internal use

#key?Boolean Also known as: has_key?

Returns:

  • (Boolean)

33
34
35
36
37
38
# File 'ext/hashes.c', line 33

static VALUE param_hash_key_p(VALUE self, VALUE key) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return nyara_rb_hash_has_key(self, key) ? Qtrue : Qfalse;
}

#nested_arefObject

prereq: all elements in keys are string


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/hashes.c', line 108

static VALUE param_hash_nested_aref(volatile VALUE obj, VALUE keys) {
  Check_Type(keys, T_ARRAY);
  VALUE* keys_ptr = RARRAY_PTR(keys);
  long keys_len = RARRAY_LEN(keys);

  for (long i = 0; i < keys_len; i++) {
    volatile VALUE key = keys_ptr[i];
    if (RSTRING_LEN(key)) {
      if (rb_obj_is_kind_of(obj, rb_cHash)) {
        obj = rb_hash_aref(obj, key);
      } else {
        return Qnil;
      }
    } else {
      if (rb_obj_is_kind_of(obj, rb_cArray)) {
        long arr_len = RARRAY_LEN(obj);
        if (arr_len) {
          obj = RARRAY_PTR(obj)[arr_len - 1];
        } else {
          return Qnil;
        }
      } else {
        return Qnil;
      }
    }
  }
  return obj;
}

#nested_asetObject

created hashes has the same class with output


231
232
233
234
235
236
237
238
239
240
# File 'ext/hashes.c', line 231

static VALUE param_hash_nested_aset(VALUE output, VALUE keys, VALUE value) {
  Check_Type(keys, T_ARRAY);
  long len = RARRAY_LEN(keys);
  if (!len) {
    rb_raise(rb_eArgError, "aset 0 length key");
    return Qnil;
  }
  _nested_aset(output, keys, value);
  return output;
}