Class: Digest::StringBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/digest/stringbuffer/version.rb,
ext/digest/stringbuffer/init.c

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.digest(*args) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'ext/digest/stringbuffer/init.c', line 263

static VALUE
buffer_s_digest(int argc, VALUE *argv, VALUE klass)
{
  VALUE str;
  volatile VALUE obj;

  if (argc < 1) {
    rb_raise(rb_eArgError, "no data given");
  }

  str = *argv++;
  argc--;

  StringValue(str);

  obj = rb_obj_alloc(klass);
  rb_obj_call_init(obj, argc, argv);

  return buffer_digest(1, &str, obj);
}

.hexdigest(*args) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'ext/digest/stringbuffer/init.c', line 284

static VALUE
buffer_s_hexdigest(int argc, VALUE *argv, VALUE klass)
{
  VALUE str;
  volatile VALUE obj;

  if (argc < 1) {
    rb_raise(rb_eArgError, "no data given");
  }

  str = *argv++;
  argc--;

  StringValue(str);

  obj = rb_obj_alloc(klass);
  rb_obj_call_init(obj, argc, argv);

  return hexencode_str_new(buffer_digest(1, &str, obj));
}

Instance Method Details

#==(other) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'ext/digest/stringbuffer/init.c', line 218

static VALUE
buffer_equal(VALUE self, VALUE other)
{
  VALUE str1, str2;
  str1 = buffer_digest(0, 0, self);
  str2 = buffer_digest(0, 0, other);

  StringValue(str1);
  StringValue(str2);

  if (RSTRING_LEN(str1) == RSTRING_LEN(str2) &&
      rb_str_cmp(str1, str2) == 0) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

#bufferObject



255
256
257
258
259
260
261
# File 'ext/digest/stringbuffer/init.c', line 255

static VALUE
buffer_get(VALUE self)
{
  buffer_t *ptr;
  Data_Get_Struct(self, buffer_t, ptr);
  return rb_str_new(ptr->buffer, buffer_buffer_length(ptr));
}

#digest(*args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/digest/stringbuffer/init.c', line 163

static VALUE
buffer_digest(int argc, VALUE *argv, VALUE self)
{
  VALUE str, value;

  if (0 < rb_scan_args(argc, argv, "01", &str)) {
    buffer_reset(self);
    buffer_update(self, str);
    value = rb_funcall(self, rb_intern("finish"), 0);
    buffer_reset(self);
  } else {
    value = rb_funcall(self, rb_intern("finish"), 0);
  }

  return value;
}

#digest!Object



180
181
182
183
184
185
186
187
# File 'ext/digest/stringbuffer/init.c', line 180

static VALUE
buffer_digest_bang(VALUE self)
{
  VALUE value = rb_funcall(self, rb_intern("finish"), 0);
  buffer_reset(self);

  return value;
}

#digest_lengthObject Also known as: length, size



207
208
209
210
211
212
213
214
215
216
# File 'ext/digest/stringbuffer/init.c', line 207

static VALUE
buffer_digest_length(VALUE self)
{
  /* subclasses really should redefine this method */
  VALUE digest = buffer_digest(0, 0, self);

  /* never blindly assume that #digest() returns a string */
  StringValue(digest);
  return UINT2NUM(RSTRING_LEN(digest));
}

#hexdigest(*args) ⇒ Object



189
190
191
192
193
# File 'ext/digest/stringbuffer/init.c', line 189

static VALUE
buffer_hexdigest(int argc, VALUE *argv, VALUE self)
{
  return hexencode_str_new(buffer_digest(argc, argv, self));
}

#hexdigest!Object



195
196
197
198
199
# File 'ext/digest/stringbuffer/init.c', line 195

static VALUE
buffer_hexdigest_bang(VALUE self)
{
  return hexencode_str_new(buffer_digest_bang(self));
}

#initialize_copy(origin) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/digest/stringbuffer/init.c', line 93

static VALUE
buffer_initialize_copy(VALUE copy, VALUE origin)
{
  buffer_t *ptr_copy, *ptr_origin;
  size_t buffer_len;

  if (copy == origin) return copy;

  rb_check_frozen(copy);

  Data_Get_Struct(copy, buffer_t, ptr_copy);
  Data_Get_Struct(origin, buffer_t, ptr_origin);

  buffer_len = buffer_buffer_length(ptr_origin);

  if (ptr_copy->memsize < ptr_origin->memsize) {
    buffer_realloc(ptr_copy, sizeof(char) * ptr_origin->memsize);
  }

  memcpy(ptr_copy->buffer, ptr_origin->buffer, buffer_len);
  ptr_copy->p = ptr_copy->buffer + buffer_len;
  ptr_copy->memsize = ptr_origin->memsize;

  return copy;
}

#resetObject



119
120
121
122
123
124
125
126
# File 'ext/digest/stringbuffer/init.c', line 119

static VALUE
buffer_reset(VALUE self)
{
  buffer_t *ptr;
  Data_Get_Struct(self, buffer_t, ptr);
  ptr->p = ptr->buffer;
  return self;
}

#to_sObject



201
202
203
204
205
# File 'ext/digest/stringbuffer/init.c', line 201

static VALUE
buffer_to_s(VALUE self)
{
  return hexencode_str_new(buffer_digest(0, 0, self));
}

#update(str) ⇒ Object Also known as: <<



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
# File 'ext/digest/stringbuffer/init.c', line 128

static VALUE
buffer_update(VALUE self, VALUE str)
{
  size_t buffer_len, str_len, require, newsize;
  const char* str_p;
  buffer_t *ptr;
  Data_Get_Struct(self, buffer_t, ptr);

  StringValue(str);
  str_p = RSTRING_PTR(str);
  str_len = RSTRING_LEN(str);
  buffer_len = buffer_buffer_length(ptr);
  require = buffer_len + str_len;
  if (ptr->memsize < require) {
    newsize = ptr->memsize;
    while (newsize < require) {
      newsize *= 2;
    }
    buffer_realloc(ptr, sizeof(char) * newsize);
    ptr->p = ptr->buffer + buffer_len;
    ptr->memsize = newsize;
  }
  memcpy(ptr->p, str_p, str_len);
  ptr->p += str_len;

  return self;
}