Class: String

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#kin_kvp(*args) ⇒ Object

be specified to use symbols instead of strings for the hash keys.



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'ext/kinutils/kinutils.c', line 364

VALUE rb_kvp(int argc, VALUE *argv, VALUE self)
{
	VALUE hash = rb_hash_new();

	if (argc < 0 || argc > 1 || (argc == 1 && !RB_TYPE_P(argv[0], T_HASH))) {
		rb_raise(rb_eArgError, "Call with no parameters, or with an options Hash.");
	}

	VALUE symbolize = Qnil;

	if (argc == 1) {
		symbolize = rb_hash_lookup(argv[0], ID2SYM(rb_intern("symbolize_keys")));
	}

	struct kvp_info info = {
		.hash = hash,
		.symbolize = RB_TEST(symbolize),
	};

	nl_parse_kvp(RSTRING_PTR(self), nl_kvp_wrapper, &(struct nl_kvp_wrap){kvp_hashcb, &info});

	return hash;
}

#kin_unescape(*args, self) ⇒ Object

TODO: merge with rb_unescape_modify



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'ext/kinutils/kinutils.c', line 242

VALUE rb_unescape(int argc, VALUE *args, VALUE self)
{
	int dequote = 0;
	int include_zero = 0;
	VALUE str;
	long len;
	int ret;

	if(argc > 2) {
		rb_raise(rb_eArgError, "Only 0 to 2 parameters supported.");
	}
	if(argc >= 1) {
		Check_Type(args[0], T_FIXNUM);
		dequote = FIX2INT(args[0]);
		if(dequote < 0 || dequote > 2) {
			rb_raise(rb_eArgError, "First parameter (dequote) must be one of the ESCAPE_* constants.");
		}
	}
	if(argc == 2) {
		if(args[1] == Qtrue) {
			include_zero = 1;
		} else if(args[1] == Qfalse) {
			include_zero = 0;
		} else {
			rb_raise(rb_eArgError, "Second parameter (include_zero) must be true or false.");
		}
	}

	str = rb_str_dup(self);
	if(!rb_enc_asciicompat(rb_enc_get(self))) {
		str = rb_str_export_to_enc(str, utf8);
	}

	len = RSTRING_LEN(str);
	ret = nl_unescape_string(RSTRING_PTR(str), include_zero, dequote);
	if(ret == -1) {
		rb_raise(rb_eRuntimeError, "Error unescaping string.");
	}

	rb_str_resize(str, len - ret);

	return str;
}

#kin_unescape!(*args, self) ⇒ Object

Unescapes the given string in place



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/kinutils/kinutils.c', line 287

VALUE rb_unescape_modify(int argc, VALUE *args, VALUE self)
{
	int dequote = 0;
	int include_zero = 0;
	long len;
	int ret;

	if(argc > 2) {
		rb_raise(rb_eArgError, "Only 0 to 2 parameters supported.");
	}
	if(argc >= 1) {
		Check_Type(args[0], T_FIXNUM);
		dequote = FIX2INT(args[0]);
		if(dequote < 0 || dequote > 2) {
			rb_raise(rb_eArgError, "First parameter (dequote) must be one of the ESCAPE_* constants.");
		}
	}
	if(argc == 2) {
		if(args[1] == Qtrue) {
			include_zero = 1;
		} else if(args[1] == Qfalse) {
			include_zero = 0;
		} else {
			rb_raise(rb_eArgError, "Second parameter (include_zero) must be true or false.");
		}
	}

	rb_check_frozen(self);

	if(!rb_enc_asciicompat(rb_enc_get(self))) {
		rb_raise(rb_eRuntimeError, "This method only works with ASCII-compatible encodings.");
	}

	len = RSTRING_LEN(self);
	ret = nl_unescape_string(RSTRING_PTR(self), include_zero, dequote);
	if(ret == -1) {
		rb_raise(rb_eRuntimeError, "Error unescaping string.");
	}

	rb_str_resize(self, len - ret);

	return self;
}