summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/mrblib/string.rb
blob: a517aa209b9006c1be5a8c27f758ab9609e29cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class String
  def lstrip
    a = 0
    z = self.size - 1
    a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
    (z >= 0) ? self[a..z] : ""
  end

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

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

  def lstrip!
    s = self.lstrip
    (s == self) ? nil : self.replace(s)
  end

  def rstrip!
    s = self.rstrip
    (s == self) ? nil : self.replace(s)
  end

  def strip!
    s = self.strip
    (s == self) ? nil : self.replace(s)
  end

# call-seq:
#    str.casecmp(other_str)   -> -1, 0, +1 or nil
#
# Case-insensitive version of <code>String#<=></code>.
#
#    "abcdef".casecmp("abcde")     #=> 1
#    "aBcDeF".casecmp("abcdef")    #=> 0
#    "abcdef".casecmp("abcdefg")   #=> -1
#    "abcdef".casecmp("ABCDEF")    #=> 0
#
  def casecmp(str)
    self.downcase <=> str.downcase
  end

  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

  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
end