Simplify get arguments
`mrb_str_index_m()` and `mrb_str_rindex()`
Make `mrb_get_args()` called only once from called twice.
`mrb_str_byteslice()`
Replace `goto` with `if ~ else`.
MODIFY
mrbgems/mruby-string-ext/src/string.c
mrbgems/mruby-string-ext/src/string.c
@@ -134,19 +134,18 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str)
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));
- goto subseq;
- }
- if (mrb_type(a1) == MRB_TT_RANGE) {
- if (mrb_range_beg_len(mrb, a1, &beg, &len, str_len, TRUE) == MRB_RANGE_OK) {
- goto subseq;
}
+ 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;
-subseq:
+ }
+
if (mrb_str_beg_len(str_len, &beg, &len) && (empty || len != 0)) {
return mrb_str_byte_subseq(mrb, str, beg, len);
}

MODIFY
src/string.c
src/string.c
@@ -1759,29 +1759,26 @@ mrb_str_include(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_str_index_m(mrb_state *mrb, mrb_value str)
{
- mrb_value *argv;
- mrb_int argc;
mrb_value sub;
- mrb_int pos, clen;
+ mrb_int pos;
- mrb_get_args(mrb, "*!", &argv, &argc);
- if (argc == 2) {
- mrb_get_args(mrb, "oi", &sub, &pos);
- }
- else {
- pos = 0;
- if (argc > 0)
- sub = argv[0];
- else
+ switch (mrb_get_args(mrb, "|oi", &sub, &pos)) {
+ case 0:
sub = mrb_nil_value();
- }
+ /* fall through */
+ case 1:
+ pos = 0;
+ break;
+ case 2:
if (pos < 0) {
- clen = RSTRING_CHAR_LEN(str);
+ mrb_int clen = RSTRING_CHAR_LEN(str);
pos += clen;
if (pos < 0) {
return mrb_nil_value();
}
}
+ break;
+ }
switch (mrb_type(sub)) {
default: {
@@ -2009,14 +2006,17 @@ mrb_str_reverse(mrb_state *mrb, mrb_value str)
static mrb_value
mrb_str_rindex(mrb_state *mrb, mrb_value str)
{
- mrb_value *argv;
- mrb_int argc;
mrb_value sub;
mrb_int pos, len = RSTRING_CHAR_LEN(str);
- mrb_get_args(mrb, "*!", &argv, &argc);
- if (argc == 2) {
- mrb_get_args(mrb, "oi", &sub, &pos);
+ switch (mrb_get_args(mrb, "|oi", &sub, &pos)) {
+ case 0:
+ sub = mrb_nil_value();
+ /* fall through */
+ case 1:
+ pos = len;
+ break;
+ case 2:
if (pos < 0) {
pos += len;
if (pos < 0) {
@@ -2024,13 +2024,7 @@ mrb_str_rindex(mrb_state *mrb, mrb_value str)
}
}
if (pos > len) pos = len;
- }
- else {
- pos = len;
- if (argc > 0)
- sub = argv[0];
- else
- sub = mrb_nil_value();
+ break;
}
pos = chars2bytes(str, 0, pos);

Simplify get arguments

- `mrb_str_index_m()` and `mrb_str_rindex()`
  Make `mrb_get_args()` called only once from called twice.
- `mrb_str_byteslice()`
  Replace `goto` with `if ~ else`.