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.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.digest(*args) ⇒ Object



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

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



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

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



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

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



252
253
254
255
256
257
258
# File 'ext/digest/stringbuffer/init.c', line 252

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



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

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



177
178
179
180
181
182
183
184
# File 'ext/digest/stringbuffer/init.c', line 177

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



204
205
206
207
208
209
210
211
212
213
# File 'ext/digest/stringbuffer/init.c', line 204

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



186
187
188
189
190
# File 'ext/digest/stringbuffer/init.c', line 186

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

#hexdigest!Object



192
193
194
195
196
# File 'ext/digest/stringbuffer/init.c', line 192

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

#initialize_copy(origin) ⇒ Object



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

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);
		ptr_copy->memsize = ptr_origin->memsize;
	}

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

	return copy;
}

#resetObject



116
117
118
119
120
121
122
123
# File 'ext/digest/stringbuffer/init.c', line 116

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

#to_sObject



198
199
200
201
202
# File 'ext/digest/stringbuffer/init.c', line 198

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

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



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

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;
}