Class: String

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
mrblib/string.rb,
src/string.c,
mrbgems/mruby-sprintf/mrblib/string.rb,
mrbgems/mruby-string-ext/mrblib/string.rb

Overview

String

ISO 15.2.10

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #>, #>=, #between?, #clamp

Constructor Details

#new(str = "") ⇒ String

Returns a new string object containing a copy of str.



1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
# File 'src/string.c', line 1901

static mrb_value
mrb_str_init(mrb_state *mrb, mrb_value self)
{
  mrb_value str2;

  if (mrb_get_args(mrb, "|S", &str2) == 0) {
    struct RString *s = str_new(mrb, 0, 0);
    str2 = mrb_obj_value(s);
  }
  str_replace(mrb, mrb_str_ptr(self), mrb_str_ptr(str2));
  return self;
}

Instance Method Details

#%(args) ⇒ Object



2
3
4
5
6
7
8
# File 'mrbgems/mruby-sprintf/mrblib/string.rb', line 2

def %(args)
  if args.is_a? Array
    sprintf(self, *args)
  else
    sprintf(self, args)
  end
end

#*(integer) ⇒ String

Copy—Returns a new String containing integer copies of the receiver.

“Ho! “ * 3 #=> “Ho! Ho! Ho! “

Returns:



941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'src/string.c', line 941

static mrb_value
mrb_str_times(mrb_state *mrb, mrb_value self)
{
  mrb_int n,len,times;
  struct RString *str2;
  char *p;

  mrb_get_args(mrb, "i", &times);
  if (times < 0) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
  }
  if (times && MRB_INT_MAX / times < RSTRING_LEN(self)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "argument too big");
  }

  len = RSTRING_LEN(self)*times;
  str2 = str_new(mrb, 0, len);
  str_with_class(str2, self);
  p = RSTR_PTR(str2);
  if (len > 0) {
    n = RSTRING_LEN(self);
    memcpy(p, RSTRING_PTR(self), n);
    while (n <= len/2) {
      memcpy(p + n, p, n);
      n *= 2;
    }
    memcpy(p + n, p, len-n);
  }
  p[RSTR_LEN(str2)] = '\0';
  RSTR_COPY_ASCII_FLAG(str2, mrb_str_ptr(self));

  return mrb_obj_value(str2);
}

#+(other_str) ⇒ String

Concatenation—Returns a new String containing other_str concatenated to str.

“Hello from “ + self.to_s #=> “Hello from main”

Returns:



900
901
902
903
904
905
906
907
# File 'src/string.c', line 900

static mrb_value
mrb_str_plus_m(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  mrb_get_args(mrb, "S", &str);
  return mrb_str_plus(mrb, self, str);
}

#<<(integer) ⇒ String #concat(integer) ⇒ String #<<(obj) ⇒ String #concat(obj) ⇒ String

Append—Concatenates the given object to str. If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation (equivalent to str.concat(integer.chr(__ENCODING__))).

a = “hello “ a « “world” #=> “hello world” a.concat(33) #=> “hello world!”

Overloads:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'mrbgems/mruby-string-ext/src/string.c', line 164

static mrb_value
mrb_str_concat_m(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  mrb_get_args(mrb, "o", &str);
  if (mrb_fixnum_p(str) || mrb_float_p(str))
#ifdef MRB_UTF8_STRING
    str = int_chr_utf8(mrb, str);
#else
    str = int_chr_binary(mrb, str);
#endif
  else
    mrb_ensure_string_type(mrb, str);
  mrb_str_cat_str(mrb, self, str);
  return self;
}

#<=>(other_str) ⇒ -1, ...

Comparison—Returns -1 if other_str is less than, 0 if other_str is equal to, and +1 if other_str is greater than str. If the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered greater than the shorter one. If the variable $= is false, the comparison is based on comparing the binary values of each character in the string. In older versions of Ruby, setting $= allowed case-insensitive comparisons; this is now deprecated in favor of using String#casecmp.

<=> is the basis for the methods <, <=, >, >=, and between?, included from module Comparable. The method String#== does not use Comparable#==.

“abcdef” <=> “abcde” #=> 1 “abcdef” <=> “abcdef” #=> 0 “abcdef” <=> “abcdefg” #=> -1 “abcdef” <=> “ABCDEF” #=> 1

Returns:

  • (-1, 0, +1)


1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'src/string.c', line 1031

static mrb_value
mrb_str_cmp_m(mrb_state *mrb, mrb_value str1)
{
  mrb_value str2;
  mrb_int result;

  mrb_get_args(mrb, "o", &str2);
  if (!mrb_string_p(str2)) {
    return mrb_nil_value();
  }
  else {
    result = mrb_str_cmp(mrb, str1, str2);
  }
  return mrb_fixnum_value(result);
}

#==(obj) ⇒ Boolean

Equality— If obj is not a String, returns false. Otherwise, returns false or true

caution:if str <=> obj returns zero.

Returns:

  • (Boolean)


1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'src/string.c', line 1076

static mrb_value
mrb_str_equal_m(mrb_state *mrb, mrb_value str1)
{
  mrb_value str2;

  mrb_get_args(mrb, "o", &str2);

  return mrb_bool_value(mrb_str_equal(mrb, str1, str2));
}

#[](fixnum) ⇒ Fixnum? #[](fixnum, fixnum) ⇒ String? #[](range) ⇒ String? #[](regexp) ⇒ String? #[](regexp, fixnum) ⇒ String? #[](other_str) ⇒ String? #slice(fixnum) ⇒ Fixnum? #slice(fixnum, fixnum) ⇒ String? #slice(range) ⇒ String? #slice(other_str) ⇒ String?

Element Reference—If passed a single Fixnum, returns the code of the character at that position. If passed two Fixnum 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 characters at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end.

If a String is given, that string is returned if it occurs in str. In both cases, nil is returned if there is no match.

a = “hello there” a[1] #=> 101(1.8.7) “e”(1.9.2) a[1.1] #=> “e”(1.9.2) a[1,3] #=> “ell” a[1..3] #=> “ell” a[-3,2] #=> “er” a[-4..-2] #=> “her” a[12..-1] #=> nil a[-2..-4] #=> “” a[“lo”] #=> “lo” a[“bye”] #=> nil

Overloads:



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'src/string.c', line 1259

static mrb_value
mrb_str_aref_m(mrb_state *mrb, mrb_value str)
{
  mrb_value a1, a2;

  if (mrb_get_args(mrb, "o|o", &a1, &a2) == 1) {
    a2 = mrb_undef_value();
  }

  return mrb_str_aref(mrb, str, a1, a2);
}

#[]=(fixnum) ⇒ Object #[]=(fixnum, fixnum) ⇒ Object #[]=(range) ⇒ Object #[]=(regexp) ⇒ Object #[]=(regexp, fixnum) ⇒ Object #[]=(other_str) ⇒ Object

Modify +self+ by replacing the content of +self+. The portion of the string affected is determined using the same criteria as +String#[]+.



1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'src/string.c', line 1442

static mrb_value
mrb_str_aset_m(mrb_state *mrb, mrb_value str)
{
  mrb_value indx, alen, replace;

  switch (mrb_get_args(mrb, "oo|S!", &indx, &alen, &replace)) {
    case 2:
      replace = alen;
      alen = mrb_undef_value();
      break;
    case 3:
      break;
  }
  mrb_str_aset(mrb, str, indx, alen, replace);
  return str;
}

#__linesObject



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'mrbgems/mruby-string-ext/src/string.c', line 1174

static mrb_value
mrb_str_lines(mrb_state *mrb, mrb_value self)
{
  mrb_value result;
  int ai;
  mrb_int len;
  char *b = RSTRING_PTR(self);
  char *p = b, *t;
  char *e = b + RSTRING_LEN(self);

  result = mrb_ary_new(mrb);
  ai = mrb_gc_arena_save(mrb);
  while (p < e) {
    t = p;
    while (p < e && *p != '\n') p++;
    if (*p == '\n') p++;
    len = (mrb_int) (p - t);
    mrb_ary_push(mrb, result, mrb_str_new(mrb, t, len));
    mrb_gc_arena_restore(mrb, ai);
  }
  return result;
}

#__sub_replace(pre, m, post) ⇒ Object

private method for gsub/sub



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'mrblib/string.rb', line 55

def __sub_replace(pre, m, post)
  s = ""
  i = 0
  while j = index("\\", i)
    break if j == length-1
    t = case self[j+1]
        when "\\"
          "\\"
        when "`"
          pre
        when "&", "0"
          m
        when "'"
          post
        when "1", "2", "3", "4", "5", "6", "7", "8", "9"
          ""
        else
          self[j, 2]
        end
    s += self[i, j-i] + t
    i = j + 2
  end
  s + self[i, length-i]
end

#bytesObject

Returns an array of bytes in str.

str = “hello” str.bytes #=> [104, 101, 108, 108, 111]



2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
# File 'src/string.c', line 2765

static mrb_value
mrb_str_bytes(mrb_state *mrb, mrb_value str)
{
  struct RString *s = mrb_str_ptr(str);
  mrb_value a = mrb_ary_new_capa(mrb, RSTR_LEN(s));
  unsigned char *p = (unsigned char *)(RSTR_PTR(s)), *pend = p + RSTR_LEN(s);

  while (p < pend) {
    mrb_ary_push(mrb, a, mrb_fixnum_value(p[0]));
    p++;
  }
  return a;
}

#bytesizeObject



924
925
926
927
928
929
# File 'src/string.c', line 924

static mrb_value
mrb_str_bytesize(mrb_state *mrb, mrb_value self)
{
  mrb_int len = RSTRING_LEN(self);
  return mrb_fixnum_value(len);
}

#byteslice(integer) ⇒ String? #byteslice(integer, integer) ⇒ String? #byteslice(range) ⇒ String?

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 str. Returns nil 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”

Overloads:



2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
# File 'src/string.c', line 2847

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_range_p(a1)) {
    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();
  }
}

#capitalizeString

Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.

“hello”.capitalize #=> “Hello” “HELLO”.capitalize #=> “Hello” “123ABC”.capitalize #=> “123abc”

Returns:



1508
1509
1510
1511
1512
1513
1514
1515
1516
# File 'src/string.c', line 1508

static mrb_value
mrb_str_capitalize(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_capitalize_bang(mrb, str);
  return str;
}

#capitalize!String?

Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made.

a = “hello” a.capitalize! #=> “Hello” a #=> “Hello” a.capitalize! #=> nil

Returns:



1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
# File 'src/string.c', line 1472

static mrb_value
mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str)
{
  char *p, *pend;
  mrb_bool modify = FALSE;
  struct RString *s = mrb_str_ptr(str);

  mrb_str_modify_keep_ascii(mrb, s);
  if (RSTR_LEN(s) == 0 || !RSTR_PTR(s)) return mrb_nil_value();
  p = RSTR_PTR(s); pend = RSTR_PTR(s) + RSTR_LEN(s);
  if (ISLOWER(*p)) {
    *p = TOUPPER(*p);
    modify = TRUE;
  }
  while (++p < pend) {
    if (ISUPPER(*p)) {
      *p = TOLOWER(*p);
      modify = TRUE;
    }
  }
  if (modify) return str;
  return mrb_nil_value();
}

#casecmp(str) ⇒ Object

call-seq: str.casecmp(other_str) -> -1, 0, +1 or nil

Case-insensitive version of String#<=>.

“abcdef”.casecmp(“abcde”) #=> 1 “aBcDeF”.casecmp(“abcdef”) #=> 0 “abcdef”.casecmp(“abcdefg”) #=> -1 “abcdef”.casecmp(“ABCDEF”) #=> 0



125
126
127
128
129
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 125

def casecmp(str)
  self.downcase <=> str.__to_str.downcase
rescue NoMethodError
  nil
end

#casecmp?(str) ⇒ Boolean

call-seq: str.casecmp?(other) -> true, false, or nil

Returns true if str and other_str are equal after case folding, false if they are not equal, and nil if other_str is not a string.

Returns:

  • (Boolean)


138
139
140
141
142
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 138

def casecmp?(str)
  c = self.casecmp(str)
  return nil if c.nil?
  return c == 0
end

#chars(&block) ⇒ Object



302
303
304
305
306
307
308
309
310
311
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 302

def chars(&block)
  if block_given?
    self.split('').each do |i|
      block.call(i)
    end
    self
  else
    self.split('')
  end
end

#chomp(separator = "\n") ⇒ String

Returns a new String with the given record separator removed from the end of str (if present). chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

“hello”.chomp #=> “hello” “hello\n”.chomp #=> “hello” “hello\r\n”.chomp #=> “hello” “hello\n\r”.chomp #=> “hello\n” “hello\r”.chomp #=> “hello” “hello \n there”.chomp #=> “hello \n there” “hello”.chomp(“llo”) #=> “he”

Returns:



1612
1613
1614
1615
1616
1617
1618
1619
1620
# File 'src/string.c', line 1612

static mrb_value
mrb_str_chomp(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_chomp_bang(mrb, str);
  return str;
}

#chomp!(separator = "\n") ⇒ String?

Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.

Returns:



1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
# File 'src/string.c', line 1526

static mrb_value
mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
{
  mrb_value rs;
  mrb_int newline;
  char *p, *pp;
  mrb_int rslen;
  mrb_int len;
  mrb_int argc;
  struct RString *s = mrb_str_ptr(str);

  argc = mrb_get_args(mrb, "|S", &rs);
  mrb_str_modify_keep_ascii(mrb, s);
  len = RSTR_LEN(s);
  if (argc == 0) {
    if (len == 0) return mrb_nil_value();
  smart_chomp:
    if (RSTR_PTR(s)[len-1] == '\n') {
      RSTR_SET_LEN(s, RSTR_LEN(s) - 1);
      if (RSTR_LEN(s) > 0 &&
          RSTR_PTR(s)[RSTR_LEN(s)-1] == '\r') {
        RSTR_SET_LEN(s, RSTR_LEN(s) - 1);
      }
    }
    else if (RSTR_PTR(s)[len-1] == '\r') {
      RSTR_SET_LEN(s, RSTR_LEN(s) - 1);
    }
    else {
      return mrb_nil_value();
    }
    RSTR_PTR(s)[RSTR_LEN(s)] = '\0';
    return str;
  }

  if (len == 0 || mrb_nil_p(rs)) return mrb_nil_value();
  p = RSTR_PTR(s);
  rslen = RSTRING_LEN(rs);
  if (rslen == 0) {
    while (len>0 && p[len-1] == '\n') {
      len--;
      if (len>0 && p[len-1] == '\r')
        len--;
    }
    if (len < RSTR_LEN(s)) {
      RSTR_SET_LEN(s, len);
      p[len] = '\0';
      return str;
    }
    return mrb_nil_value();
  }
  if (rslen > len) return mrb_nil_value();
  newline = RSTRING_PTR(rs)[rslen-1];
  if (rslen == 1 && newline == '\n')
    newline = RSTRING_PTR(rs)[rslen-1];
  if (rslen == 1 && newline == '\n')
    goto smart_chomp;

  pp = p + len - rslen;
  if (p[len-1] == newline &&
     (rslen <= 1 ||
     memcmp(RSTRING_PTR(rs), pp, rslen) == 0)) {
    RSTR_SET_LEN(s, len - rslen);
    p[RSTR_LEN(s)] = '\0';
    return str;
  }
  return mrb_nil_value();
}

#chopString

Returns a new String with the last character removed. If the string ends with \r\n, both characters are removed. Applying chop to an empty string returns an empty string. String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.

“string\r\n”.chop #=> “string” “string\n\r”.chop #=> “string\n” “string\n”.chop #=> “string” “string”.chop #=> “strin” “x”.chop #=> “”

Returns:



1681
1682
1683
1684
1685
1686
1687
1688
# File 'src/string.c', line 1681

static mrb_value
mrb_str_chop(mrb_state *mrb, mrb_value self)
{
  mrb_value str;
  str = mrb_str_dup(mrb, self);
  mrb_str_chop_bang(mrb, str);
  return str;
}

#chop!String?

Processes str as for String#chop, returning str, or nil if str is the empty string. See also String#chomp!.

Returns:



1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
# File 'src/string.c', line 1631

static mrb_value
mrb_str_chop_bang(mrb_state *mrb, mrb_value str)
{
  struct RString *s = mrb_str_ptr(str);

  mrb_str_modify_keep_ascii(mrb, s);
  if (RSTR_LEN(s) > 0) {
    mrb_int len;
#ifdef MRB_UTF8_STRING
    const char* t = RSTR_PTR(s), *p = t;
    const char* e = p + RSTR_LEN(s);
    while (p<e) {
      mrb_int clen = utf8len(p, e);
      if (p + clen>=e) break;
      p += clen;
    }
    len = p - t;
#else
    len = RSTR_LEN(s) - 1;
#endif
    if (RSTR_PTR(s)[len] == '\n') {
      if (len > 0 &&
          RSTR_PTR(s)[len-1] == '\r') {
        len--;
      }
    }
    RSTR_SET_LEN(s, len);
    RSTR_PTR(s)[len] = '\0';
    return str;
  }
  return mrb_nil_value();
}

#chrString

Returns a one-character string at the beginning of the string.

a = “abcde” a.chr #=> “a”

Returns:



855
856
857
858
859
# File 'mrbgems/mruby-string-ext/src/string.c', line 855

static mrb_value
mrb_str_chr(mrb_state *mrb, mrb_value self)
{
  return mrb_str_substr(mrb, self, 0, 1);
}

#clearObject

call-seq: string.clear -> string

Makes string empty.

a = “abcde” a.clear #=> “”



12
13
14
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 12

def clear
  self.replace("")
end

#codepoints(&block) ⇒ Object Also known as: each_codepoint



326
327
328
329
330
331
332
333
334
335
336
337
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 326

def codepoints(&block)
  len = self.size

  if block_given?
    self.split('').each do|x|
      block.call(x.ord)
    end
    self
  else
    self.split('').map{|x| x.ord}
  end
end

#<<(integer) ⇒ String #concat(integer) ⇒ String #<<(obj) ⇒ String #concat(obj) ⇒ String

Append—Concatenates the given object to str. If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation (equivalent to str.concat(integer.chr(__ENCODING__))).

a = “hello “ a « “world” #=> “hello world” a.concat(33) #=> “hello world!”

Overloads:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'mrbgems/mruby-string-ext/src/string.c', line 164

static mrb_value
mrb_str_concat_m(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  mrb_get_args(mrb, "o", &str);
  if (mrb_fixnum_p(str) || mrb_float_p(str))
#ifdef MRB_UTF8_STRING
    str = int_chr_utf8(mrb, str);
#else
    str = int_chr_binary(mrb, str);
#endif
  else
    mrb_ensure_string_type(mrb, str);
  mrb_str_cat_str(mrb, self, str);
  return self;
}

#countObject

call_seq: str.count([other_str]) -> integer

Each other_str parameter defines a set of characters to count. The intersection of these sets defines the characters to count in str. Any other_str that starts with a caret ^ is negated. The sequence c1-c2 means all characters between c1 and c2. The backslash character \ can be used to escape ^ or - and is otherwise ignored unless it appears at the end of a sequence or the end of a other_str.



810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'mrbgems/mruby-string-ext/src/string.c', line 810

static mrb_value
mrb_str_count(mrb_state *mrb, mrb_value str)
{
  mrb_value v_pat = mrb_nil_value();
  mrb_int i;
  char *s;
  mrb_int len;
  mrb_int count = 0;
  struct tr_pattern pat = STATIC_TR_PATTERN;
  uint8_t bitmap[32];

  mrb_get_args(mrb, "S", &v_pat);
  tr_parse_pattern(mrb, &pat, v_pat, TRUE);
  tr_compile_pattern(&pat, v_pat, bitmap);
  tr_free_pattern(mrb, &pat);

  s = RSTRING_PTR(str);
  len = RSTRING_LEN(str);
  for (i = 0; i < len; i++) {
    if (tr_bitmap_detect(bitmap, s[i])) count++;
  }
  return mrb_fixnum_value(count);
}

#cover?(obj) ⇒ Boolean

Returns true if +obj+ is between the begin and end of the range.

This tests begin <= obj <= end when #exclude_end? is +false+ and begin <= obj < end when #exclude_end? is +true+.

(“a”..”z”).cover?(“c”) #=> true (“a”..”z”).cover?(“5”) #=> false (“a”..”z”).cover?(“cc”) #=> true

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'mrbgems/mruby-range-ext/src/range.c', line 34

static mrb_value
range_cover(mrb_state *mrb, mrb_value range)
{
  mrb_value val;
  struct RRange *r = mrb_range_ptr(mrb, range);
  mrb_value beg, end;

  mrb_get_args(mrb, "o", &val);

  beg = RANGE_BEG(r);
  end = RANGE_END(r);

  if (r_le(mrb, beg, val)) {
    if (RANGE_EXCL(r)) {
      if (r_lt(mrb, val, end))
        return mrb_true_value();
    }
    else {
      if (r_le(mrb, val, end))
        return mrb_true_value();
    }
  }

  return mrb_false_value();
}

#deleteObject



775
776
777
778
779
780
781
782
783
784
785
# File 'mrbgems/mruby-string-ext/src/string.c', line 775

static mrb_value
mrb_str_delete(mrb_state *mrb, mrb_value str)
{
  mrb_value pat;
  mrb_value dup;

  mrb_get_args(mrb, "S", &pat);
  dup = mrb_str_dup(mrb, str);
  str_delete(mrb, dup, pat);
  return dup;
}

#delete!Object



787
788
789
790
791
792
793
794
795
796
797
# File 'mrbgems/mruby-string-ext/src/string.c', line 787

static mrb_value
mrb_str_delete_bang(mrb_state *mrb, mrb_value str)
{
  mrb_value pat;

  mrb_get_args(mrb, "S", &pat);
  if (str_delete(mrb, str, pat)) {
    return str;
  }
  return mrb_nil_value();
}

#delete_prefix(prefix) ⇒ String

Returns a copy of str with leading prefix deleted.

“hello”.delete_prefix(“hel”) #=> “lo” “hello”.delete_prefix(“llo”) #=> “hello”

Returns:



1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'mrbgems/mruby-string-ext/src/string.c', line 1105

static mrb_value
mrb_str_del_prefix(mrb_state *mrb, mrb_value self)
{
  mrb_int plen, slen;
  char *ptr;

  mrb_get_args(mrb, "s", &ptr, &plen);
  slen = RSTRING_LEN(self);
  if (plen > slen) return mrb_str_dup(mrb, self);
  if (memcmp(RSTRING_PTR(self), ptr, plen) != 0)
    return mrb_str_dup(mrb, self);
  return mrb_str_substr(mrb, self, plen, slen-plen);
}

#delete_prefix!(prefix) ⇒ self?

Deletes leading prefix from str, returning nil if no change was made.

“hello”.delete_prefix!(“hel”) #=> “lo” “hello”.delete_prefix!(“llo”) #=> nil

Returns:

  • (self, nil)


1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'mrbgems/mruby-string-ext/src/string.c', line 1072

static mrb_value
mrb_str_del_prefix_bang(mrb_state *mrb, mrb_value self)
{
  mrb_int plen, slen;
  char *ptr, *s;
  struct RString *str = RSTRING(self);

  mrb_get_args(mrb, "s", &ptr, &plen);
  slen = RSTR_LEN(str);
  if (plen > slen) return mrb_nil_value();
  s = RSTR_PTR(str);
  if (memcmp(s, ptr, plen) != 0) return mrb_nil_value();
  if (!mrb_frozen_p(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
    str->as.heap.ptr += plen;
  }
  else {
    mrb_str_modify(mrb, str);
    s = RSTR_PTR(str);
    memmove(s, s+plen, slen-plen);
  }
  RSTR_SET_LEN(str, slen-plen);
  return self;
}

#delete_suffix(suffix) ⇒ String

Returns a copy of str with leading suffix deleted.

“hello”.delete_suffix(“hel”) #=> “lo” “hello”.delete_suffix(“llo”) #=> “hello”

Returns:



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'mrbgems/mruby-string-ext/src/string.c', line 1160

static mrb_value
mrb_str_del_suffix(mrb_state *mrb, mrb_value self)
{
  mrb_int plen, slen;
  char *ptr;

  mrb_get_args(mrb, "s", &ptr, &plen);
  slen = RSTRING_LEN(self);
  if (plen > slen) return mrb_str_dup(mrb, self);
  if (memcmp(RSTRING_PTR(self)+slen-plen, ptr, plen) != 0)
    return mrb_str_dup(mrb, self);
  return mrb_str_substr(mrb, self, 0, slen-plen);
}

#delete_suffix!(suffix) ⇒ self?

Deletes trailing suffix from str, returning nil if no change was made.

“hello”.delete_suffix!(“llo”) #=> “he” “hello”.delete_suffix!(“hel”) #=> nil

Returns:

  • (self, nil)


1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'mrbgems/mruby-string-ext/src/string.c', line 1129

static mrb_value
mrb_str_del_suffix_bang(mrb_state *mrb, mrb_value self)
{
  mrb_int plen, slen;
  char *ptr, *s;
  struct RString *str = RSTRING(self);

  mrb_get_args(mrb, "s", &ptr, &plen);
  slen = RSTR_LEN(str);
  if (plen > slen) return mrb_nil_value();
  s = RSTR_PTR(str);
  if (memcmp(s+slen-plen, ptr, plen) != 0) return mrb_nil_value();
  if (!mrb_frozen_p(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
    /* no need to modify string */
  }
  else {
    mrb_str_modify(mrb, str);
  }
  RSTR_SET_LEN(str, slen-plen);
  return self;
}

#downcaseString

Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. The operation is locale insensitive—only characters ‘A’ to ‘Z’ are affected.

“hEllO”.downcase #=> “hello”

Returns:



1731
1732
1733
1734
1735
1736
1737
1738
1739
# File 'src/string.c', line 1731

static mrb_value
mrb_str_downcase(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_downcase_bang(mrb, str);
  return str;
}

#downcase!String?

Downcases the contents of str, returning nil if no changes were made.

Returns:



1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
# File 'src/string.c', line 1698

static mrb_value
mrb_str_downcase_bang(mrb_state *mrb, mrb_value str)
{
  char *p, *pend;
  mrb_bool modify = FALSE;
  struct RString *s = mrb_str_ptr(str);

  mrb_str_modify_keep_ascii(mrb, s);
  p = RSTR_PTR(s);
  pend = RSTR_PTR(s) + RSTR_LEN(s);
  while (p < pend) {
    if (ISUPPER(*p)) {
      *p = TOLOWER(*p);
      modify = TRUE;
    }
    p++;
  }

  if (modify) return str;
  return mrb_nil_value();
}

#dumpString

Produces a version of str with all nonprinting characters replaced by \nnn notation and all special characters escaped.

Returns:



2666
2667
2668
2669
2670
# File 'src/string.c', line 2666

mrb_value
mrb_str_dump(mrb_state *mrb, mrb_value str)
{
  return str_escape(mrb, str, FALSE);
}

#each_byte(&block) ⇒ Object

Call the given block for each byte of +self+.



194
195
196
197
198
199
200
201
202
203
# File 'mrblib/string.rb', line 194

def each_byte(&block)
  return to_enum(:each_byte, &block) unless block
  bytes = self.bytes
  pos = 0
  while pos < bytes.size
    block.call(bytes[pos])
    pos += 1
  end
  self
end

#each_char(&block) ⇒ Object

Call the given block for each character of +self+.



316
317
318
319
320
321
322
323
324
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 316

def each_char(&block)
  return to_enum :each_char unless block
  pos = 0
  while pos < self.size
    block.call(self[pos])
    pos += 1
  end
  self
end

#each_line(separator = "\n", &block) ⇒ Object

Calls the given block for each line and pass the respective line.

ISO 15.2.10.5.15

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'mrblib/string.rb', line 14

def each_line(separator = "\n", &block)
  return to_enum(:each_line, separator) unless block

  if separator.nil?
    block.call(self)
    return self
  end
  raise TypeError unless separator.is_a?(String)

  paragraph_mode = false
  if separator.empty?
    paragraph_mode = true
    separator = "\n\n"
  end
  start = 0
  string = dup
  self_len = length
  sep_len = separator.length
  should_yield_subclass_instances = self.class != String

  while (pointer = string.index(separator, start))
    pointer += sep_len
    pointer += 1 while paragraph_mode && string[pointer] == "\n"
    if should_yield_subclass_instances
      block.call(self.class.new(string[start, pointer - start]))
    else
      block.call(string[start, pointer - start])
    end
    start = pointer
  end
  return self if start == self_len

  if should_yield_subclass_instances
    block.call(self.class.new(string[start, self_len - start]))
  else
    block.call(string[start, self_len - start])
  end
  self
end

#empty?Boolean

Returns true if str has a length of zero.

“hello”.empty? #=> false ““.empty? #=> true

Returns:

  • (Boolean)


1751
1752
1753
1754
1755
1756
1757
# File 'src/string.c', line 1751

static mrb_value
mrb_str_empty_p(mrb_state *mrb, mrb_value self)
{
  struct RString *s = mrb_str_ptr(self);

  return mrb_bool_value(RSTR_LEN(s) == 0);
}

#end_with?([suffixes]) ⇒ Boolean

Returns true if +str+ ends with one of the +suffixes+ given.

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'mrbgems/mruby-string-ext/src/string.c', line 224

static mrb_value
mrb_str_end_with(mrb_state *mrb, mrb_value self)
{
  mrb_value *argv, sub;
  mrb_int argc, i;
  mrb_get_args(mrb, "*", &argv, &argc);

  for (i = 0; i < argc; i++) {
    size_t len_l, len_r;
    int ai = mrb_gc_arena_save(mrb);
    sub = mrb_ensure_string_type(mrb, argv[i]);
    mrb_gc_arena_restore(mrb, ai);
    len_l = RSTRING_LEN(self);
    len_r = RSTRING_LEN(sub);
    if (len_l >= len_r) {
      if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
                 RSTRING_PTR(sub),
                 len_r) == 0) {
        return mrb_true_value();
      }
    }
  }
  return mrb_false_value();
}

#eql?(other) ⇒ Boolean

Two strings are equal if the have the same length and content.

Returns:

  • (Boolean)


1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
# File 'src/string.c', line 1766

static mrb_value
mrb_str_eql(mrb_state *mrb, mrb_value self)
{
  mrb_value str2;
  mrb_bool eql_p;

  mrb_get_args(mrb, "o", &str2);
  eql_p = (mrb_string_p(str2)) && str_eql(mrb, self, str2);

  return mrb_bool_value(eql_p);
}

#getbyte(index) ⇒ 0 .. 255

returns the indexth byte as an integer.

Returns:

  • (0 .. 255)


2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
# File 'src/string.c', line 2785

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

#gsub(*args, &block) ⇒ Object

Replace all matches of +pattern+ with +replacement+. Call block (if given) for each match and replace +pattern+ with the value of the block. Return the final value.

ISO 15.2.10.5.18

Raises:



87
88
89
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
115
116
# File 'mrblib/string.rb', line 87

def gsub(*args, &block)
  return to_enum(:gsub, *args) if args.length == 1 && !block
  raise ArgumentError, "wrong number of arguments" unless (1..2).include?(args.length)

  pattern, replace = *args
  plen = pattern.length
  if args.length == 2 && block
    block = nil
  end
  if !replace.nil? || !block
    replace.__to_str
  end
  offset = 0
  result = []
  while found = index(pattern, offset)
    result << self[offset, found - offset]
    offset = found + plen
    result << if block
      block.call(pattern).to_s
    else
      replace.__sub_replace(self[0, found], pattern, self[offset..-1] || "")
    end
    if plen == 0
      result << self[offset, 1]
      offset += 1
    end
  end
  result << self[offset..-1] if offset < length
  result.join
end

#gsub!(*args, &block) ⇒ Object

Replace all matches of +pattern+ with +replacement+. Call block (if given) for each match and replace +pattern+ with the value of the block. Modify +self+ with the final value.

ISO 15.2.10.5.19

Raises:



125
126
127
128
129
130
131
# File 'mrblib/string.rb', line 125

def gsub!(*args, &block)
  raise FrozenError, "can't modify frozen String" if frozen?
  return to_enum(:gsub!, *args) if args.length == 1 && !block
  str = self.gsub(*args, &block)
  return nil unless self.index(args[0])
  self.replace(str)
end

#hashFixnum

Return a hash based on the string’s length and content.

Returns:



1807
1808
1809
1810
1811
1812
# File 'src/string.c', line 1807

static mrb_value
mrb_str_hash_m(mrb_state *mrb, mrb_value self)
{
  mrb_int key = mrb_str_hash(mrb, self);
  return mrb_fixnum_value(key);
}

#hexObject



834
835
836
837
838
# File 'mrbgems/mruby-string-ext/src/string.c', line 834

static mrb_value
mrb_str_hex(mrb_state *mrb, mrb_value self)
{
  return mrb_str_to_inum(mrb, self, 16, FALSE);
}

#include?(other_str) ⇒ Boolean #include?(fixnum) ⇒ Boolean

Returns true if str contains the given string or character.

“hello”.include? “lo” #=> true “hello”.include? “ol” #=> false “hello”.include? ?h #=> true

Overloads:

  • #include?(other_str) ⇒ Boolean

    Returns:

    • (Boolean)
  • #include?(fixnum) ⇒ Boolean

    Returns:

    • (Boolean)


1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
# File 'src/string.c', line 1827

static mrb_value
mrb_str_include(mrb_state *mrb, mrb_value self)
{
  mrb_value str2;

  mrb_get_args(mrb, "S", &str2);
  if (str_index_str(mrb, self, str2, 0) < 0)
    return mrb_bool_value(FALSE);
  return mrb_bool_value(TRUE);
}

#index(substring[, offset]) ⇒ Fixnum?

Returns the index of the first occurrence of the given substring. Returns nil if not found. If the second parameter is present, it specifies the position in the string to begin the search.

“hello”.index(‘l’) #=> 2 “hello”.index(‘lo’) #=> 3 “hello”.index(‘a’) #=> nil “hello”.index(‘l’, -2) #=> 3

Returns:



1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
# File 'src/string.c', line 1853

static mrb_value
mrb_str_index_m(mrb_state *mrb, mrb_value str)
{
  mrb_value sub;
  mrb_int pos;

  if (mrb_get_args(mrb, "S|i", &sub, &pos) == 1) {
    pos = 0;
  }
  else if (pos < 0) {
    mrb_int clen = RSTRING_CHAR_LEN(str);
    pos += clen;
    if (pos < 0) {
      return mrb_nil_value();
    }
  }
  pos = str_index_str_by_char(mrb, str, sub, pos);

  if (pos == -1) return mrb_nil_value();
  BYTES_ALIGN_CHECK(pos);
  return mrb_fixnum_value(pos);
}

#replace(other_str) ⇒ String

s = “hello” #=> “hello” s.replace “world” #=> “world”

Returns:



1885
1886
1887
1888
1889
1890
1891
1892
# File 'src/string.c', line 1885

static mrb_value
mrb_str_replace(mrb_state *mrb, mrb_value str)
{
  mrb_value str2;

  mrb_get_args(mrb, "S", &str2);
  return str_replace(mrb, mrb_str_ptr(str), mrb_str_ptr(str2));
}

#insert(idx, str) ⇒ Object

call-seq: str.insert(index, other_str) -> str

Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is insert aString so that it starts at the given index.

"abcd".insert(0, 'X')    #=> "Xabcd"
"abcd".insert(3, 'X')    #=> "abcXd"
"abcd".insert(4, 'X')    #=> "abcdX"
"abcd".insert(-3, 'X')   #=> "abXcd"
"abcd".insert(-1, 'X')   #=> "abcdX"


254
255
256
257
258
259
260
261
262
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 254

def insert(idx, str)
  if idx == -1
    return self << str
  elsif idx < 0
    idx += 1
  end
  self[idx, 0] = str
  self
end

#inspectString

Returns a printable version of str, surrounded by quote marks, with special characters escaped.

str = “hello” str[3] = “\b” str.inspect #=> “"hel\bo"”

Returns:



2750
2751
2752
2753
2754
# File 'src/string.c', line 2750

mrb_value
mrb_str_inspect(mrb_state *mrb, mrb_value str)
{
  return str_escape(mrb, str, TRUE);
}

#internObject #to_symObject

Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

“Koala”.intern #=> :Koala s = ‘cat’.to_sym #=> :cat s == :cat #=> true s = ‘@cat’.to_sym #=> :@cat s == :@cat #=> true

This can also be used to create symbols that cannot be represented using the :xxx notation.

‘cat and dog’.to_sym #=> :”cat and dog”



1935
1936
1937
1938
1939
# File 'src/string.c', line 1935

MRB_API mrb_value
mrb_str_intern(mrb_state *mrb, mrb_value self)
{
  return mrb_symbol_value(mrb_intern_str(mrb, self));
}

#lastObject #last(n) ⇒ Array

Returns the last object in the range, or an array of the last +n+ elements.

Note that with no arguments +last+ will return the object that defines the end of the range even if #exclude_end? is +true+.

(10..20).last #=> 20 (10…20).last #=> 20 (10..20).last(3) #=> [18, 19, 20] (10…20).last(3) #=> [17, 18, 19]

Overloads:

  • #lastObject

    Returns:

    • (Object)
  • #last(n) ⇒ Array

    Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'mrbgems/mruby-range-ext/src/range.c', line 76

static mrb_value
range_last(mrb_state *mrb, mrb_value range)
{
  mrb_value num;
  mrb_value array;

  if (mrb_get_args(mrb, "|o", &num) == 0) {
    return mrb_range_end(mrb, range);
  }

  array = mrb_funcall(mrb, range, "to_a", 0);
  return mrb_funcall(mrb, array, "last", 1, mrb_to_int(mrb, num));
}

#lengthInteger

Same as sym.to_s.length.

Returns:



917
918
919
920
921
922
# File 'src/string.c', line 917

static mrb_value
mrb_str_size(mrb_state *mrb, mrb_value self)
{
  mrb_int len = RSTRING_CHAR_LEN(self);
  return mrb_fixnum_value(len);
}

#lines(&blk) ⇒ Object

call-seq: string.lines -> array of string string.lines {|s| block} -> array of string

Returns strings per line;

a = “abc\ndef” a.lines #=> [“abc\n”, “def”]

If a block is given, it works the same as each_line.



365
366
367
368
369
370
371
372
373
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 365

def lines(&blk)
  lines = self.__lines
  if blk
    lines.each do |line|
      blk.call(line)
    end
  end
  lines
end

#ljust(idx, padstr = ' ') ⇒ Object

call-seq: str.ljust(integer, padstr=’ ‘) -> new_str

If integer is greater than the length of str, returns a new String of length integer with str left justified and padded with padstr; otherwise, returns str.

"hello".ljust(4)            #=> "hello"
"hello".ljust(20)           #=> "hello               "
"hello".ljust(20, '1234')   #=> "hello123412341234123"

Raises:



275
276
277
278
279
280
281
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 275

def ljust(idx, padstr = ' ')
  raise ArgumentError, 'zero width padding' if padstr == ''
  return self if idx <= self.size
  pad_repetitions = (idx / padstr.length).ceil
  padding = (padstr * pad_repetitions)[0...(idx - self.length)]
  self + padding
end

#lstripObject

call-seq: str.lstrip -> new_str

Returns a copy of str with leading whitespace removed. See also String#rstrip and String#strip.

” hello “.lstrip #=> “hello “ “hello”.lstrip #=> “hello”



26
27
28
29
30
31
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 26

def lstrip
  a = 0
  z = self.size - 1
  a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
  (z >= 0) ? self[a..z] : ""
end

#lstrip!Object

call-seq: str.lstrip! -> self or nil

Removes leading whitespace from str, returning nil if no change was made. See also String#rstrip! and String#strip!.

” hello “.lstrip #=> “hello “ “hello”.lstrip! #=> nil

Raises:



78
79
80
81
82
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 78

def lstrip!
  raise FrozenError, "can't modify frozen String" if frozen?
  s = self.lstrip
  (s == self) ? nil : self.replace(s)
end

#nextObject



986
987
988
989
990
991
992
993
994
# File 'mrbgems/mruby-string-ext/src/string.c', line 986

static mrb_value
mrb_str_succ(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_succ_bang(mrb, str);
  return str;
}

#succString

Returns next sequence of the string;

a = “abc” a.succ #=> “abd”

Returns:



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'mrbgems/mruby-string-ext/src/string.c', line 908

static mrb_value
mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
{
  mrb_value result;
  unsigned char *p, *e, *b, *t;
  const char *prepend;
  struct RString *s = mrb_str_ptr(self);
  mrb_int l;

  if (RSTRING_LEN(self) == 0)
    return self;

  mrb_str_modify(mrb, s);
  l = RSTRING_LEN(self);
  b = p = (unsigned char*) RSTRING_PTR(self);
  t = e = p + l;
  *(e--) = 0;

  // find trailing ascii/number
  while (e >= b) {
    if (ISALNUM(*e))
      break;
    e--;
  }
  if (e < b) {
    e = p + l - 1;
    result = mrb_str_new_lit(mrb, "");
  }
  else {
    // find leading letter of the ascii/number
    b = e;
    while (b > p) {
      if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
        break;
      b--;
    }
    if (!ISALNUM(*b))
      b++;
    result = mrb_str_new(mrb, (char*) p, b - p);
  }

  while (e >= b) {
    if (!ISALNUM(*e)) {
      if (*e == 0xff) {
        mrb_str_cat_lit(mrb, result, "\x01");
        (*e) = 0;
      }
      else
        (*e)++;
      break;
    }
    prepend = NULL;
    if (*e == '9') {
      if (e == b) prepend = "1";
      *e = '0';
    }
    else if (*e == 'z') {
      if (e == b) prepend = "a";
      *e = 'a';
    }
    else if (*e == 'Z') {
      if (e == b) prepend = "A";
      *e = 'A';
    }
    else {
      (*e)++;
      break;
    }
    if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
    e--;
  }
  result = mrb_str_cat(mrb, result, (char*) b, t - b);
  l = RSTRING_LEN(result);
  mrb_str_resize(mrb, self, l);
  memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
  return self;
}

#octObject



840
841
842
843
844
# File 'mrbgems/mruby-string-ext/src/string.c', line 840

static mrb_value
mrb_str_oct(mrb_state *mrb, mrb_value self)
{
  return mrb_str_to_inum(mrb, self, 8, FALSE);
}

#ordObject



1053
1054
1055
1056
1057
1058
1059
# File 'mrbgems/mruby-string-ext/src/string.c', line 1053

static mrb_value
mrb_str_ord(mrb_state* mrb, mrb_value str)
{
  if (RSTRING_LEN(str) == 0)
    mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
  return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[0]);
}

#partition(sep) ⇒ Object

Raises:



144
145
146
147
148
149
150
151
152
153
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 144

def partition(sep)
  raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
  n = index(sep)
  unless n.nil?
    m = n + sep.size
    [ slice(0, n), sep, slice(m, size - m) ]
  else
    [ self, "", "" ]
  end
end

#prepend(arg) ⇒ Object

call-seq: str.prepend(other_str) -> str

Prepend—Prepend the given string to str.

a = “world” a.prepend(“hello “) #=> “hello world” a #=> “hello world”



349
350
351
352
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 349

def prepend(arg)
  self[0, 0] = arg
  self
end

#replace(other_str) ⇒ String

s = “hello” #=> “hello” s.replace “world” #=> “world”

Returns:



1885
1886
1887
1888
1889
1890
1891
1892
# File 'src/string.c', line 1885

static mrb_value
mrb_str_replace(mrb_state *mrb, mrb_value str)
{
  mrb_value str2;

  mrb_get_args(mrb, "S", &str2);
  return str_replace(mrb, mrb_str_ptr(str), mrb_str_ptr(str2));
}

#reverseString

Returns a new string with the characters from str in reverse order.

“stressed”.reverse #=> “desserts”

Returns:



2051
2052
2053
2054
2055
2056
2057
# File 'src/string.c', line 2051

static mrb_value
mrb_str_reverse(mrb_state *mrb, mrb_value str)
{
  mrb_value str2 = mrb_str_dup(mrb, str);
  mrb_str_reverse_bang(mrb, str2);
  return str2;
}

#reverse!String

Reverses str in place.

Returns:



2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
# File 'src/string.c', line 2004

static mrb_value
mrb_str_reverse_bang(mrb_state *mrb, mrb_value str)
{
  struct RString *s = mrb_str_ptr(str);
  char *p, *e;

#ifdef MRB_UTF8_STRING
  mrb_int utf8_len = RSTRING_CHAR_LEN(str);
  mrb_int len = RSTR_LEN(s);

  if (utf8_len < 2) return str;
  if (utf8_len < len) {
    mrb_str_modify(mrb, s);
    p = RSTR_PTR(s);
    e = p + RSTR_LEN(s);
    while (p<e) {
      mrb_int clen = utf8len(p, e);
      str_reverse(p, p + clen - 1);
      p += clen;
    }
    goto bytes;
  }
#endif

  if (RSTR_LEN(s) > 1) {
    mrb_str_modify(mrb, s);
    goto bytes;
  }
  return str;

 bytes:
  p = RSTR_PTR(s);
  e = p + RSTR_LEN(s) - 1;
  str_reverse(p, e);
  return str;
}

#rindex(substring[, offset]) ⇒ Fixnum?

Returns the index of the last occurrence of the given substring. Returns nil if not found. If the second parameter is present, it specifies the position in the string to end the search—characters beyond this point will not be considered.

“hello”.rindex(‘e’) #=> 1 “hello”.rindex(‘l’) #=> 3 “hello”.rindex(‘a’) #=> nil “hello”.rindex(‘l’, 2) #=> 2

Returns:



2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
# File 'src/string.c', line 2074

static mrb_value
mrb_str_rindex(mrb_state *mrb, mrb_value str)
{
  mrb_value sub;
  mrb_int pos, len = RSTRING_CHAR_LEN(str);

  if (mrb_get_args(mrb, "S|i", &sub, &pos) == 1) {
    pos = len;
  }
  else {
    if (pos < 0) {
      pos += len;
      if (pos < 0) {
        return mrb_nil_value();
      }
    }
    if (pos > len) pos = len;
  }
  pos = chars2bytes(str, 0, pos);
  pos = str_rindex(mrb, str, sub, pos);
  if (pos >= 0) {
    pos = bytes2chars(RSTRING_PTR(str), RSTRING_LEN(str), pos);
    BYTES_ALIGN_CHECK(pos);
    return mrb_fixnum_value(pos);
  }
  return mrb_nil_value();
}

#rjust(idx, padstr = ' ') ⇒ Object

call-seq: str.rjust(integer, padstr=’ ‘) -> new_str

If integer is greater than the length of str, returns a new String of length integer with str right justified and padded with padstr; otherwise, returns str.

"hello".rjust(4)            #=> "hello"
"hello".rjust(20)           #=> "               hello"
"hello".rjust(20, '1234')   #=> "123412341234123hello"

Raises:



294
295
296
297
298
299
300
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 294

def rjust(idx, padstr = ' ')
  raise ArgumentError, 'zero width padding' if padstr == ''
  return self if idx <= self.size
  pad_repetitions = (idx / padstr.length).ceil
  padding = (padstr * pad_repetitions)[0...(idx - self.length)]
  padding + self
end

#rpartition(sep) ⇒ Object

Raises:



155
156
157
158
159
160
161
162
163
164
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 155

def rpartition(sep)
  raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
  n = rindex(sep)
  unless n.nil?
    m = n + sep.size
    [ slice(0, n), sep, slice(m, size - m) ]
  else
    [ "", "", self ]
  end
end

#rstripObject

call-seq: str.rstrip -> new_str

Returns a copy of str with trailing whitespace removed. See also String#lstrip and String#strip.

” hello “.rstrip #=> “ hello” “hello”.rstrip #=> “hello”



43
44
45
46
47
48
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 43

def rstrip
  a = 0
  z = self.size - 1
  z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
  (z >= 0) ? self[a..z] : ""
end

#rstrip!Object

call-seq: str.rstrip! -> self or nil

Removes trailing whitespace from str, returning nil if no change was made. See also String#lstrip! and String#strip!.

” hello “.rstrip #=> “ hello” “hello”.rstrip! #=> nil

Raises:



95
96
97
98
99
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 95

def rstrip!
  raise FrozenError, "can't modify frozen String" if frozen?
  s = self.rstrip
  (s == self) ? nil : self.replace(s)
end

#setbyte(index, integer) ⇒ Integer

modifies the indexth byte as integer.

Returns:



2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
# File 'src/string.c', line 2805

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

#lengthInteger

Same as sym.to_s.length.

Returns:



917
918
919
920
921
922
# File 'src/string.c', line 917

static mrb_value
mrb_str_size(mrb_state *mrb, mrb_value self)
{
  mrb_int len = RSTRING_CHAR_LEN(self);
  return mrb_fixnum_value(len);
}

#[](fixnum) ⇒ Fixnum? #[](fixnum, fixnum) ⇒ String? #[](range) ⇒ String? #[](regexp) ⇒ String? #[](regexp, fixnum) ⇒ String? #[](other_str) ⇒ String? #slice(fixnum) ⇒ Fixnum? #slice(fixnum, fixnum) ⇒ String? #slice(range) ⇒ String? #slice(other_str) ⇒ String?

Element Reference—If passed a single Fixnum, returns the code of the character at that position. If passed two Fixnum 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 characters at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end.

If a String is given, that string is returned if it occurs in str. In both cases, nil is returned if there is no match.

a = “hello there” a[1] #=> 101(1.8.7) “e”(1.9.2) a[1.1] #=> “e”(1.9.2) a[1,3] #=> “ell” a[1..3] #=> “ell” a[-3,2] #=> “er” a[-4..-2] #=> “her” a[12..-1] #=> nil a[-2..-4] #=> “” a[“lo”] #=> “lo” a[“bye”] #=> nil

Overloads:



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'src/string.c', line 1259

static mrb_value
mrb_str_aref_m(mrb_state *mrb, mrb_value str)
{
  mrb_value a1, a2;

  if (mrb_get_args(mrb, "o|o", &a1, &a2) == 1) {
    a2 = mrb_undef_value();
  }

  return mrb_str_aref(mrb, str, a1, a2);
}

#slice!(arg1, arg2 = nil) ⇒ Object

call-seq: str.slice!(fixnum) -> new_str or nil str.slice!(fixnum, fixnum) -> new_str or nil str.slice!(range) -> new_str or nil str.slice!(other_str) -> new_str or nil

Deletes the specified portion from str, and returns the portion deleted.

string = “this is a string” string.slice!(2) #=> “i” string.slice!(3..6) #=> “ is “ string.slice!(“r”) #=> “r” string #=> “thsa sting”

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 182

def slice!(arg1, arg2=nil)
  raise FrozenError, "can't modify frozen String" if frozen?
  raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?

  if !arg1.nil? && !arg2.nil?
    idx = arg1
    idx += self.size if arg1 < 0
    if idx >= 0 && idx <= self.size && arg2 > 0
      str = self[idx, arg2]
    else
      return nil
    end
  else
    validated = false
    if arg1.kind_of?(Range)
      beg = arg1.begin
      ed = arg1.end
      beg += self.size if beg < 0
      ed += self.size if ed < 0
      ed -= 1 if arg1.exclude_end?
      validated = true
    elsif arg1.kind_of?(String)
      validated = true
    else
      idx = arg1
      idx += self.size if arg1 < 0
      validated = true if idx >=0 && arg1 < self.size
    end
    if validated
      str = self[arg1]
    else
      return nil
    end
  end
  unless str.nil? || str == ""
    if !arg1.nil? && !arg2.nil?
      idx = arg1 >= 0 ? arg1 : self.size+arg1
      str2 = self[0...idx] + self[idx+arg2..-1].to_s
    else
      if arg1.kind_of?(Range)
        idx = beg >= 0 ? beg : self.size+beg
        idx2 = ed>= 0 ? ed : self.size+ed
        str2 = self[0...idx] + self[idx2+1..-1].to_s
      elsif arg1.kind_of?(String)
        idx = self.index(arg1)
        str2 = self[0...idx] + self[idx+arg1.size..-1] unless idx.nil?
      else
        idx = arg1 >= 0 ? arg1 : self.size+arg1
        str2 = self[0...idx] + self[idx+1..-1].to_s
      end
    end
    self.replace(str2) unless str2.nil?
  end
  str
end

#split(pattern = "\n", [limit]) ⇒ Array

Divides str into substrings based on a delimiter, returning an array of these substrings.

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters.

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ‘ ‘ were specified.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

” now’s the time”.split #=> [“now’s”, “the”, “time”] “ now’s the time”.split(‘ ‘) #=> [“now’s”, “the”, “time”] “ now’s the time”.split(/ /) #=> [””, “now’s”, “”, “the”, “time”] “hello”.split(//) #=> [“h”, “e”, “l”, “l”, “o”] “hello”.split(//, 3) #=> [“h”, “e”, “llo”]

“mellow yellow”.split(“ello”) #=> [“m”, “w y”, “w”] “1,2,,3,4,,”.split(‘,’) #=> [“1”, “2”, “”, “3”, “4”] “1,2,,3,4,,”.split(‘,’, 4) #=> [“1”, “2”, “”, “3,4,,”] “1,2,,3,4,,”.split(‘,’, -4) #=> [“1”, “2”, “”, “3”, “4”, “”, “”]

Returns:



2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
# File 'src/string.c', line 2143

static mrb_value
mrb_str_split_m(mrb_state *mrb, mrb_value str)
{
  mrb_int argc;
  mrb_value spat = mrb_nil_value();
  enum {awk, string, regexp} split_type = string;
  mrb_int i = 0;
  mrb_int beg;
  mrb_int end;
  mrb_int lim = 0;
  mrb_bool lim_p;
  mrb_value result, tmp;

  argc = mrb_get_args(mrb, "|oi", &spat, &lim);
  lim_p = (lim > 0 && argc == 2);
  if (argc == 2) {
    if (lim == 1) {
      if (RSTRING_LEN(str) == 0)
        return mrb_ary_new_capa(mrb, 0);
      return mrb_ary_new_from_values(mrb, 1, &str);
    }
    i = 1;
  }

  if (argc == 0 || mrb_nil_p(spat)) {
    split_type = awk;
  }
  else if (!mrb_string_p(spat)) {
    mrb_raise(mrb, E_TYPE_ERROR, "expected String");
  }
  else if (RSTRING_LEN(spat) == 1 && RSTRING_PTR(spat)[0] == ' ') {
    split_type = awk;
  }

  result = mrb_ary_new(mrb);
  beg = 0;
  if (split_type == awk) {
    mrb_bool skip = TRUE;
    mrb_int idx = 0;
    mrb_int str_len = RSTRING_LEN(str);
    unsigned int c;
    int ai = mrb_gc_arena_save(mrb);

    idx = end = beg;
    while (idx < str_len) {
      c = (unsigned char)RSTRING_PTR(str)[idx++];
      if (skip) {
        if (ISSPACE(c)) {
          beg = idx;
        }
        else {
          end = idx;
          skip = FALSE;
          if (lim_p && lim <= i) break;
        }
      }
      else if (ISSPACE(c)) {
        mrb_ary_push(mrb, result, mrb_str_byte_subseq(mrb, str, beg, end-beg));
        mrb_gc_arena_restore(mrb, ai);
        skip = TRUE;
        beg = idx;
        if (lim_p) ++i;
      }
      else {
        end = idx;
      }
    }
  }
  else {                        /* split_type == string */
    mrb_int str_len = RSTRING_LEN(str);
    mrb_int pat_len = RSTRING_LEN(spat);
    mrb_int idx = 0;
    int ai = mrb_gc_arena_save(mrb);

    while (idx < str_len) {
      if (pat_len > 0) {
        end = mrb_memsearch(RSTRING_PTR(spat), pat_len, RSTRING_PTR(str)+idx, str_len - idx);
        if (end < 0) break;
      }
      else {
        end = chars2bytes(str, idx, 1);
      }
      mrb_ary_push(mrb, result, mrb_str_byte_subseq(mrb, str, idx, end));
      mrb_gc_arena_restore(mrb, ai);
      idx += end + pat_len;
      if (lim_p && lim <= ++i) break;
    }
    beg = idx;
  }
  if (RSTRING_LEN(str) > 0 && (lim_p || RSTRING_LEN(str) > beg || lim < 0)) {
    if (RSTRING_LEN(str) == beg) {
      tmp = mrb_str_new_empty(mrb, str);
    }
    else {
      tmp = mrb_str_byte_subseq(mrb, str, beg, RSTRING_LEN(str)-beg);
    }
    mrb_ary_push(mrb, result, tmp);
  }
  if (!lim_p && lim == 0) {
    mrb_int len;
    while ((len = RARRAY_LEN(result)) > 0 &&
           (tmp = RARRAY_PTR(result)[len-1], RSTRING_LEN(tmp) == 0))
      mrb_ary_pop(mrb, result);
  }

  return result;
}

#squeeze([other_str]) ⇒ String

Builds a set of characters from the other_str parameter(s) using the procedure described for String#count. Returns a new string where runs of the same character that occur in this set are replaced by a single character. If no arguments are given, all runs of identical characters are replaced by a single character.

“yellow moon”.squeeze #=> “yelow mon” “ now is the”.squeeze(“ “) #=> “ now is the” “putters shoot balls”.squeeze(“m-z”) #=> “puters shot balls”

Returns:



712
713
714
715
716
717
718
719
720
721
722
# File 'mrbgems/mruby-string-ext/src/string.c', line 712

static mrb_value
mrb_str_squeeze(mrb_state *mrb, mrb_value str)
{
  mrb_value pat = mrb_nil_value();
  mrb_value dup;

  mrb_get_args(mrb, "|S", &pat);
  dup = mrb_str_dup(mrb, str);
  str_squeeze(mrb, dup, pat);
  return dup;
}

#squeeze!([other_str]) ⇒ String?

Squeezes str in place, returning either str, or nil if no changes were made.

Returns:



731
732
733
734
735
736
737
738
739
740
741
# File 'mrbgems/mruby-string-ext/src/string.c', line 731

static mrb_value
mrb_str_squeeze_bang(mrb_state *mrb, mrb_value str)
{
  mrb_value pat = mrb_nil_value();

  mrb_get_args(mrb, "|S", &pat);
  if (str_squeeze(mrb, str, pat)) {
    return str;
  }
  return mrb_nil_value();
}

#start_with?([prefixes]) ⇒ Boolean

Returns true if +str+ starts with one of the +prefixes+ given.

“hello”.start_with?(“hell”) #=> true

# returns true if one of the prefixes matches. “hello”.start_with?(“heaven”, “hell”) #=> true “hello”.start_with?(“heaven”, “paradise”) #=> false “h”.start_with?(“heaven”, “hell”) #=> false

Returns:

  • (Boolean)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'mrbgems/mruby-string-ext/src/string.c', line 195

static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
{
  mrb_value *argv, sub;
  mrb_int argc, i;
  mrb_get_args(mrb, "*", &argv, &argc);

  for (i = 0; i < argc; i++) {
    size_t len_l, len_r;
    int ai = mrb_gc_arena_save(mrb);
    sub = mrb_ensure_string_type(mrb, argv[i]);
    mrb_gc_arena_restore(mrb, ai);
    len_l = RSTRING_LEN(self);
    len_r = RSTRING_LEN(sub);
    if (len_l >= len_r) {
      if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
        return mrb_true_value();
      }
    }
  }
  return mrb_false_value();
}

#stripObject

call-seq: str.strip -> new_str

Returns a copy of str with leading and trailing whitespace removed.

” hello “.strip #=> “hello” “\tgoodbye\r\n”.strip #=> “goodbye”



59
60
61
62
63
64
65
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 59

def strip
  a = 0
  z = self.size - 1
  a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
  z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
  (z >= 0) ? self[a..z] : ""
end

#strip!Object

call-seq: str.strip! -> str or nil

Removes leading and trailing whitespace from str. Returns nil if str was not altered.

Raises:



108
109
110
111
112
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 108

def strip!
  raise FrozenError, "can't modify frozen String" if frozen?
  s = self.strip
  (s == self) ? nil : self.replace(s)
end

#sub(*args, &block) ⇒ Object

Replace only the first match of +pattern+ with +replacement+. Call block (if given) for each match and replace +pattern+ with the value of the block. Return the final value.

ISO 15.2.10.5.36



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'mrblib/string.rb', line 150

def sub(*args, &block)
  unless (1..2).include?(args.length)
    raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)"
  end

  pattern, replace = *args
  pattern.__to_str
  if args.length == 2 && block
    block = nil
  end
  unless block
    replace.__to_str
  end
  result = []
  this = dup
  found = index(pattern)
  return this unless found
  result << this[0, found]
  offset = found + pattern.length
  result << if block
    block.call(pattern).to_s
  else
    replace.__sub_replace(this[0, found], pattern, this[offset..-1] || "")
  end
  result << this[offset..-1] if offset < length
  result.join
end

#sub!(*args, &block) ⇒ Object

Replace only the first match of +pattern+ with +replacement+. Call block (if given) for each match and replace +pattern+ with the value of the block. Modify +self+ with the final value.

ISO 15.2.10.5.37

Raises:



185
186
187
188
189
190
# File 'mrblib/string.rb', line 185

def sub!(*args, &block)
  raise FrozenError, "can't modify frozen String" if frozen?
  str = self.sub(*args, &block)
  return nil unless self.index(args[0])
  self.replace(str)
end

#succObject



986
987
988
989
990
991
992
993
994
# File 'mrbgems/mruby-string-ext/src/string.c', line 986

static mrb_value
mrb_str_succ(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_succ_bang(mrb, str);
  return str;
}

#succString

Returns next sequence of the string;

a = “abc” a.succ #=> “abd”

Returns:



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'mrbgems/mruby-string-ext/src/string.c', line 908

static mrb_value
mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
{
  mrb_value result;
  unsigned char *p, *e, *b, *t;
  const char *prepend;
  struct RString *s = mrb_str_ptr(self);
  mrb_int l;

  if (RSTRING_LEN(self) == 0)
    return self;

  mrb_str_modify(mrb, s);
  l = RSTRING_LEN(self);
  b = p = (unsigned char*) RSTRING_PTR(self);
  t = e = p + l;
  *(e--) = 0;

  // find trailing ascii/number
  while (e >= b) {
    if (ISALNUM(*e))
      break;
    e--;
  }
  if (e < b) {
    e = p + l - 1;
    result = mrb_str_new_lit(mrb, "");
  }
  else {
    // find leading letter of the ascii/number
    b = e;
    while (b > p) {
      if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
        break;
      b--;
    }
    if (!ISALNUM(*b))
      b++;
    result = mrb_str_new(mrb, (char*) p, b - p);
  }

  while (e >= b) {
    if (!ISALNUM(*e)) {
      if (*e == 0xff) {
        mrb_str_cat_lit(mrb, result, "\x01");
        (*e) = 0;
      }
      else
        (*e)++;
      break;
    }
    prepend = NULL;
    if (*e == '9') {
      if (e == b) prepend = "1";
      *e = '0';
    }
    else if (*e == 'z') {
      if (e == b) prepend = "a";
      *e = 'a';
    }
    else if (*e == 'Z') {
      if (e == b) prepend = "A";
      *e = 'A';
    }
    else {
      (*e)++;
      break;
    }
    if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
    e--;
  }
  result = mrb_str_cat(mrb, result, (char*) b, t - b);
  l = RSTRING_LEN(result);
  mrb_str_resize(mrb, self, l);
  memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
  return self;
}

#swapcaseString

Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase. Note: case conversion is effective only in ASCII region.

“Hello”.swapcase #=> “hELLO” “cYbEr_PuNk11”.swapcase #=> “CyBeR_pUnK11”

Returns:



138
139
140
141
142
143
144
145
146
# File 'mrbgems/mruby-string-ext/src/string.c', line 138

static mrb_value
mrb_str_swapcase(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_swapcase_bang(mrb, str);
  return str;
}

#swapcase!String?

Equivalent to String#swapcase, but modifies the receiver in place, returning str, or nil if no changes were made. Note: case conversion is effective only in ASCII region.

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'mrbgems/mruby-string-ext/src/string.c', line 101

static mrb_value
mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
{
  char *p, *pend;
  int modify = 0;
  struct RString *s = mrb_str_ptr(str);

  mrb_str_modify(mrb, s);
  p = RSTRING_PTR(str);
  pend = p + RSTRING_LEN(str);
  while (p < pend) {
    if (ISUPPER(*p)) {
      *p = TOLOWER(*p);
      modify = 1;
    }
    else if (ISLOWER(*p)) {
      *p = TOUPPER(*p);
      modify = 1;
    }
    p++;
  }

  if (modify) return str;
  return mrb_nil_value();
}

#to_fFloat

Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception.

“123.45e1”.to_f #=> 1234.5 “45.67 degrees”.to_f #=> 45.67 “thx1138”.to_f #=> 0.0

Returns:



2585
2586
2587
2588
2589
# File 'src/string.c', line 2585

static mrb_value
mrb_str_to_f(mrb_state *mrb, mrb_value self)
{
  return mrb_float_value(mrb, mrb_str_to_dbl(mrb, self, FALSE));
}

#to_i(base = 10) ⇒ Integer

Returns the result of interpreting leading characters in str as an integer base base (between 2 and 36). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception.

“12345”.to_i #=> 12345 “99 red balloons”.to_i #=> 99 “0a”.to_i #=> 0 “0a”.to_i(16) #=> 10 “hello”.to_i #=> 0 “1100101”.to_i(2) #=> 101 “1100101”.to_i(8) #=> 294977 “1100101”.to_i(10) #=> 1100101 “1100101”.to_i(16) #=> 17826049

Returns:



2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'src/string.c', line 2489

static mrb_value
mrb_str_to_i(mrb_state *mrb, mrb_value self)
{
  mrb_int base = 10;

  mrb_get_args(mrb, "|i", &base);
  if (base < 0) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal radix %i", base);
  }
  return mrb_str_to_inum(mrb, self, base, FALSE);
}

#to_sString

Returns the receiver.

Returns:



2599
2600
2601
2602
2603
2604
2605
2606
# File 'src/string.c', line 2599

static mrb_value
mrb_str_to_s(mrb_state *mrb, mrb_value self)
{
  if (mrb_obj_class(mrb, self) != mrb->string_class) {
    return mrb_str_dup(mrb, self);
  }
  return self;
}

#to_sString

Returns the receiver.

Returns:



2599
2600
2601
2602
2603
2604
2605
2606
# File 'src/string.c', line 2599

static mrb_value
mrb_str_to_s(mrb_state *mrb, mrb_value self)
{
  if (mrb_obj_class(mrb, self) != mrb->string_class) {
    return mrb_str_dup(mrb, self);
  }
  return self;
}

#internObject #to_symObject

Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

“Koala”.intern #=> :Koala s = ‘cat’.to_sym #=> :cat s == :cat #=> true s = ‘@cat’.to_sym #=> :@cat s == :@cat #=> true

This can also be used to create symbols that cannot be represented using the :xxx notation.

‘cat and dog’.to_sym #=> :”cat and dog”



1935
1936
1937
1938
1939
# File 'src/string.c', line 1935

MRB_API mrb_value
mrb_str_intern(mrb_state *mrb, mrb_value self)
{
  return mrb_symbol_value(mrb_intern_str(mrb, self));
}

#tr(from_str, to_str) ⇒ String

Returns a copy of str with the characters in from_str replaced by the corresponding characters in to_str. If to_str is shorter than from_str, it is padded with its last character in order to maintain the correspondence.

“hello”.tr(‘el’, ‘ip’) #=> “hippo” “hello”.tr(‘aeiou’, ‘’) #=> “hll” “hello”.tr(‘aeiou’, ‘AA’) #=> “hAll*”

Both strings may use the c1-c2 notation to denote ranges of characters, and from_str may start with a ^, which denotes all characters except those listed.

“hello”.tr(‘a-y’, ‘b-z’) #=> “ifmmp” “hello”.tr(‘^aeiou’, ‘’) #=> “e**o”

The backslash character \ can be used to escape ^ or - and is otherwise ignored unless it appears at the end of a range or the end of the from_str or to_str:

“hello^world”.tr(“\^aeiou”, “”) #=> “hll*wrld” “hello-world”.tr(“a\-eo”, “”) #=> “hll*wrld”

“hello\r\nworld”.tr(“\r”, “”) #=> “hello\nworld” “hello\r\nworld”.tr(“\r”, “”) #=> “hello\r\nwold” “hello\r\nworld”.tr(“\\r”, “”) #=> “hello\nworld”

“X[’\b’]”.tr(“X\”, “”) #=> “[‘b’]” “X[’\b’]”.tr(“X-\]”, “”) #=> “‘b’”

Note: conversion is effective only in ASCII region.

Returns:



576
577
578
579
580
581
582
583
584
585
586
# File 'mrbgems/mruby-string-ext/src/string.c', line 576

static mrb_value
mrb_str_tr(mrb_state *mrb, mrb_value str)
{
  mrb_value dup;
  mrb_value p1, p2;

  mrb_get_args(mrb, "SS", &p1, &p2);
  dup = mrb_str_dup(mrb, str);
  str_tr(mrb, dup, p1, p2, FALSE);
  return dup;
}

#tr!(from_str, to_str) ⇒ String?

Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.

Returns:



595
596
597
598
599
600
601
602
603
604
605
# File 'mrbgems/mruby-string-ext/src/string.c', line 595

static mrb_value
mrb_str_tr_bang(mrb_state *mrb, mrb_value str)
{
  mrb_value p1, p2;

  mrb_get_args(mrb, "SS", &p1, &p2);
  if (str_tr(mrb, str, p1, p2, FALSE)) {
    return str;
  }
  return mrb_nil_value();
}

#tr_s(from_str, to_str) ⇒ String

Processes a copy of str as described under String#tr, then removes duplicate characters in regions that were affected by the translation.

“hello”.tr_s(‘l’, ‘r’) #=> “hero” “hello”.tr_s(‘el’, ‘’) #=> “ho” “hello”.tr_s(‘el’, ‘hx’) #=> “hhxo”

Returns:



618
619
620
621
622
623
624
625
626
627
628
# File 'mrbgems/mruby-string-ext/src/string.c', line 618

static mrb_value
mrb_str_tr_s(mrb_state *mrb, mrb_value str)
{
  mrb_value dup;
  mrb_value p1, p2;

  mrb_get_args(mrb, "SS", &p1, &p2);
  dup = mrb_str_dup(mrb, str);
  str_tr(mrb, dup, p1, p2, TRUE);
  return dup;
}

#tr_s!(from_str, to_str) ⇒ String?

Performs String#tr_s processing on str in place, returning str, or nil if no changes were made.

Returns:



637
638
639
640
641
642
643
644
645
646
647
# File 'mrbgems/mruby-string-ext/src/string.c', line 637

static mrb_value
mrb_str_tr_s_bang(mrb_state *mrb, mrb_value str)
{
  mrb_value p1, p2;

  mrb_get_args(mrb, "SS", &p1, &p2);
  if (str_tr(mrb, str, p1, p2, TRUE)) {
    return str;
  }
  return mrb_nil_value();
}

#upcaseString

Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The operation is locale insensitive—only characters ‘a’ to ‘z’ are affected.

“hEllO”.upcase #=> “HELLO”

Returns:



2649
2650
2651
2652
2653
2654
2655
2656
2657
# File 'src/string.c', line 2649

static mrb_value
mrb_str_upcase(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  str = mrb_str_dup(mrb, self);
  mrb_str_upcase_bang(mrb, str);
  return str;
}

#upcase!String?

Upcases the contents of str, returning nil if no changes were made.

Returns:



2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
# File 'src/string.c', line 2616

static mrb_value
mrb_str_upcase_bang(mrb_state *mrb, mrb_value str)
{
  struct RString *s = mrb_str_ptr(str);
  char *p, *pend;
  mrb_bool modify = FALSE;

  mrb_str_modify_keep_ascii(mrb, s);
  p = RSTRING_PTR(str);
  pend = RSTRING_END(str);
  while (p < pend) {
    if (ISLOWER(*p)) {
      *p = TOUPPER(*p);
      modify = TRUE;
    }
    p++;
  }

  if (modify) return str;
  return mrb_nil_value();
}

#upto(max, exclusive = false, &block) ⇒ Object

call-seq: str.upto(other_str, exclusive=false) {|s| block } -> str str.upto(other_str, exclusive=false) -> an_enumerator

Iterates through successive values, starting at str and ending at other_str inclusive, passing each value in turn to the block. The String#succ method is used to generate each value. If optional second argument exclusive is omitted or is false, the last value will be included; otherwise it will be excluded.

If no block is given, an enumerator is returned instead.

"a8".upto("b6") {|s| print s, ' ' }
for s in "a8".."b6"
  print s, ' '
end

produces:

a8 a9 b0 b1 b2 b3 b4 b5 b6
a8 a9 b0 b1 b2 b3 b4 b5 b6

If str and other_str contains only ascii numeric characters, both are recognized as decimal numbers. In addition, the width of string (e.g. leading zeros) is handled appropriately.

"9".upto("11").to_a   #=> ["9", "10", "11"]
"25".upto("5").to_a   #=> []
"07".upto("11").to_a  #=> ["07", "08", "09", "10", "11"]

Raises:



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'mrbgems/mruby-string-ext/mrblib/string.rb', line 405

def upto(max, exclusive=false, &block)
  return to_enum(:upto, max, exclusive) unless block
  raise TypeError, "no implicit conversion of #{max.class} into String" unless max.kind_of? String

  len = self.length
  maxlen = max.length
  # single character
  if len == 1 and maxlen == 1
    c = self.ord
    e = max.ord
    while c <= e
      break if exclusive and c == e
      yield c.chr(__ENCODING__)
      c += 1
    end
    return self
  end
  # both edges are all digits
  bi = self.to_i(10)
  ei = max.to_i(10)
  len = self.length
  if (bi > 0 or bi == "0"*len) and (ei > 0 or ei == "0"*maxlen)
    while bi <= ei
      break if exclusive and bi == ei
      s = bi.to_s
      s = s.rjust(len, "0") if s.length < len
      yield s
      bi += 1
    end
    return self
  end
  bs = self
  while true
    n = (bs <=> max)
    break if n > 0
    break if exclusive and n == 0
    yield bs
    break if n == 0
    bs = bs.succ
  end
  self
end