mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Move String#{getbyte,setbyte,byteslice} to the core; #4696
Unlike CRuby, there's no way to process strings byte-wise by core methods because there's no per string encoding in mruby, so that we moved 3 byte-wise operation methods from `mruby-string-ext` gem.
This commit is contained in:
+102
@@ -2941,6 +2941,104 @@ mrb_str_bytes(mrb_state *mrb, mrb_value str)
|
||||
return a;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.getbyte(index) -> 0 .. 255
|
||||
*
|
||||
* returns the <i>index</i>th byte as an integer.
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
mrb_int pos;
|
||||
mrb_get_args(mrb, "i", &pos);
|
||||
|
||||
if (pos < 0)
|
||||
pos += RSTRING_LEN(str);
|
||||
if (pos < 0 || RSTRING_LEN(str) <= pos)
|
||||
return mrb_nil_value();
|
||||
|
||||
return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[pos]);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.setbyte(index, integer) -> integer
|
||||
*
|
||||
* modifies the <i>index</i>th byte as <i>integer</i>.
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_setbyte(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
mrb_int pos, byte;
|
||||
mrb_int len;
|
||||
|
||||
mrb_get_args(mrb, "ii", &pos, &byte);
|
||||
|
||||
len = RSTRING_LEN(str);
|
||||
if (pos < -len || len <= pos)
|
||||
mrb_raisef(mrb, E_INDEX_ERROR, "index %i out of string", pos);
|
||||
if (pos < 0)
|
||||
pos += len;
|
||||
|
||||
mrb_str_modify(mrb, mrb_str_ptr(str));
|
||||
byte &= 0xff;
|
||||
RSTRING_PTR(str)[pos] = (unsigned char)byte;
|
||||
return mrb_fixnum_value((unsigned char)byte);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.byteslice(integer) -> new_str or nil
|
||||
* str.byteslice(integer, integer) -> new_str or nil
|
||||
* str.byteslice(range) -> new_str or nil
|
||||
*
|
||||
* Byte Reference---If passed a single Integer, returns a
|
||||
* substring of one byte at that position. If passed two Integer
|
||||
* objects, returns a substring starting at the offset given by the first, and
|
||||
* a length given by the second. If given a Range, a substring containing
|
||||
* bytes at offsets given by the range is returned. In all three cases, if
|
||||
* an offset is negative, it is counted from the end of <i>str</i>. Returns
|
||||
* <code>nil</code> if the initial offset falls outside the string, the length
|
||||
* is negative, or the beginning of the range is greater than the end.
|
||||
* The encoding of the resulted string keeps original encoding.
|
||||
*
|
||||
* "hello".byteslice(1) #=> "e"
|
||||
* "hello".byteslice(-1) #=> "o"
|
||||
* "hello".byteslice(1, 2) #=> "el"
|
||||
* "\x80\u3042".byteslice(1, 3) #=> "\u3042"
|
||||
* "\x03\u3042\xff".byteslice(1..3) #=> "\u3042"
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_byteslice(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
mrb_value a1, a2;
|
||||
mrb_int str_len = RSTRING_LEN(str), beg, len;
|
||||
mrb_bool empty = TRUE;
|
||||
|
||||
if (mrb_get_args(mrb, "o|o", &a1, &a2) == 2) {
|
||||
beg = mrb_fixnum(mrb_to_int(mrb, a1));
|
||||
len = mrb_fixnum(mrb_to_int(mrb, a2));
|
||||
}
|
||||
else if (mrb_type(a1) == MRB_TT_RANGE) {
|
||||
if (mrb_range_beg_len(mrb, a1, &beg, &len, str_len, TRUE) != MRB_RANGE_OK) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
}
|
||||
else {
|
||||
beg = mrb_fixnum(mrb_to_int(mrb, a1));
|
||||
len = 1;
|
||||
empty = FALSE;
|
||||
}
|
||||
|
||||
if (mrb_str_beg_len(str_len, &beg, &len) && (empty || len != 0)) {
|
||||
return mrb_str_byte_subseq(mrb, str, beg, len);
|
||||
}
|
||||
else {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------*/
|
||||
void
|
||||
mrb_init_string(mrb_state *mrb)
|
||||
@@ -2998,6 +3096,10 @@ mrb_init_string(mrb_state *mrb)
|
||||
mrb_define_method(mrb, s, "upcase!", mrb_str_upcase_bang, MRB_ARGS_NONE()); /* 15.2.10.5.43 */
|
||||
mrb_define_method(mrb, s, "inspect", mrb_str_inspect, MRB_ARGS_NONE()); /* 15.2.10.5.46(x) */
|
||||
mrb_define_method(mrb, s, "bytes", mrb_str_bytes, MRB_ARGS_NONE());
|
||||
|
||||
mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, s, "setbyte", mrb_str_setbyte, MRB_ARGS_REQ(2));
|
||||
mrb_define_method(mrb, s, "byteslice", mrb_str_byteslice, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
|
||||
}
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
|
||||
Reference in New Issue
Block a user