Class: GrokMatch

Inherits:
Object
  • Object
show all
Defined in:
ext/ruby_grokmatch.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'ext/ruby_grokmatch.c', line 44

VALUE rGrokMatch_initialize(VALUE self) {
  grok_match_t *gm;
  VALUE captures;
  
  Data_Get_Struct(self, grok_match_t, gm);

  /* Set the @captures variable to a hash of array of strings */
  captures = rb_hash_new();
  //captures = rb_eval_string("Hash.new { |h,k| h[k] = Array.new }");
  rb_iv_set(self, "@captures", captures);

  return Qtrue;
}

Class Method Details

.newObject



17
18
19
20
21
22
# File 'ext/ruby_grokmatch.c', line 17

VALUE rGrokMatch_new(VALUE klass) {
  NEWOBJ(rgm, grok_match_t);
  OBJSETUP(rgm, klass, T_OBJECT);

  return (VALUE)rgm;
}

Instance Method Details

#capturesObject



103
104
105
106
107
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/ruby_grokmatch.c', line 103

VALUE rGrokMatch_captures(VALUE self) {
  char *name;
  const char *data;
  int namelen, datalen;
  grok_match_t *gm;
  VALUE captures;

  Data_Get_Struct(self, grok_match_t, gm);
  captures = rb_iv_get(self, "@captures");

  if (captures == Qnil) {
    captures = rb_hash_new();
  }

  if (FIX2INT(rb_funcall(captures, id_length, 0)) > 0) {
  //if (FIX2NUM(rb_hash_size(captures)) > 0) {
    return captures;
  }

  grok_match_walk_init(gm);
  while (grok_match_walk_next(gm, &name, &namelen, &data, &datalen) == 0) {
    VALUE key = Qnil;
    VALUE value = Qnil;

#ifdef _TRIM_KEY_EXCESS_IN_C_
  /* This section will skip captures of %{FOO} and rename captures of
   * %{FOO:bar} to just 'bar' */
    size_t koff = 0;
    /* there is no 'strcspn' that takes a length, so do it ourselves */
    while (koff < namelen && name[koff] != ':' && name[koff] != '\0') {
      koff++;
    }

    /* Skip captures that aren't named specially */
    if (koff == namelen) {
      //printf("Skipping %.*s\n", namelen, name);
      continue;
    }

    koff++;

    key = rb_tainted_str_new(name + koff, namelen - koff);
#else
    key = rb_tainted_str_new(name, namelen);
#endif
    value = rb_tainted_str_new(data, datalen);

    VALUE array;
    //if (rb_hash_has_key(captures, key) == Qfalse) {
    if (rb_funcall(captures, id_has_key_p, 1, key) == Qfalse) {
      array = rb_hash_aset(captures, key, rb_ary_new());
    } else {
      array = rb_hash_aref(captures, key);
    }
    rb_ary_push(array, value);
  }

  grok_match_walk_end(gm);
  return captures;
}

#each_captureObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/ruby_grokmatch.c', line 58

VALUE rGrokMatch_each_capture(VALUE self) {
  char *name;
  const char *data;
  int namelen, datalen;
  grok_match_t *gm;
  VALUE captures;

  Data_Get_Struct(self, grok_match_t, gm);
  captures = rb_iv_get(self, "@captures");
  grok_match_walk_init(gm);
  while (grok_match_walk_next(gm, &name, &namelen, &data, &datalen) == 0) {
    VALUE key, value;

#ifdef _TRIM_KEY_EXCESS_IN_C_
  /* This section will skip captures of %{FOO} and rename captures of
   * %{FOO:bar} to just 'bar' */
    size_t koff = 0;
    /* there is no 'strcspn' that takes a length, so do it ourselves */
    while (koff < namelen && name[koff] != ':' && name[koff] != '\0') {
      koff++;
    }

    /* Skip captures that aren't named specially */
    if (koff == namelen) {
      //printf("Skipping %.*s\n", namelen, name);
      continue;
    }

    koff++;

    key = rb_tainted_str_new(name + koff, namelen - koff);
#else
    key = rb_tainted_str_new(name, namelen);
#endif
    value = rb_tainted_str_new(data, datalen);

    // Yield [key, value]
    rb_yield(rb_ary_new3(2, key, value));
    //rb_ary_push(ary, value);
  }

  grok_match_walk_end(gm);
  return Qtrue;
}

#endObject



170
171
172
173
174
175
176
177
178
179
# File 'ext/ruby_grokmatch.c', line 170

VALUE rGrokMatch_end(VALUE self) {
  grok_match_t *gm;
  Data_Get_Struct(self, grok_match_t, gm);
  VALUE ret = Qnil;
  ret = rb_iv_get(self, "@end");
  if (ret == Qnil) {
    ret = rb_iv_set(self, "@end", INT2FIX(gm->end));
  }
  return INT2FIX(gm->end);
}

#startObject



164
165
166
167
168
# File 'ext/ruby_grokmatch.c', line 164

VALUE rGrokMatch_start(VALUE self) {
  grok_match_t *gm;
  Data_Get_Struct(self, grok_match_t, gm);
  return INT2FIX(gm->start);
}

#subjectObject



181
182
183
184
185
# File 'ext/ruby_grokmatch.c', line 181

VALUE rGrokMatch_subject(VALUE self) {
  grok_match_t *gm;
  Data_Get_Struct(self, grok_match_t, gm);
  return rb_tainted_str_new2(gm->subject);
}