Debian Codegraph @

vendor/mruby-source/mrblib/string.rb

raw
1 ##
2 # String
3 #
4 # ISO 15.2.10
5 class String
6 # ISO 15.2.10.3
7 include Comparable
8
9 ##
10 # Calls the given block for each line
11 # and pass the respective line.
12 #
13 # ISO 15.2.10.5.15
14 def each_line(separator = "\n", &block)
15 return to_enum(:each_line, separator) unless block
16
17 if separator.nil?
18 block.call(self)
19 return self
20 end
21 raise TypeError unless separator.is_a?(String)
22
23 paragraph_mode = false
24 if separator.empty?
25 paragraph_mode = true
26 separator = "\n\n"
27 end
28 start = 0
29 string = dup
30 self_len = self.bytesize
31 sep_len = separator.bytesize
32
33 while (pointer = string.byteindex(separator, start))
34 pointer += sep_len
35 pointer += 1 while paragraph_mode && string.getbyte(pointer) == 10 # 10 == \n
36 block.call(string.byteslice(start, pointer - start))
37 start = pointer
38 end
39 return self if start == self_len
40
41 block.call(string.byteslice(start, self_len - start))
42 self
43 end
44
45 ##
46 # Replace all matches of +pattern+ with +replacement+.
47 # Call block (if given) for each match and replace
48 # +pattern+ with the value of the block. Return the
49 # final value.
50 #
51 # ISO 15.2.10.5.18
52 def gsub(*args, &block)
53 return to_enum(:gsub, *args) if args.length == 1 && !block
54 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1..2)" unless (1..2).include?(args.length)
55
56 pattern, replace = *args
57 plen = pattern.length
58 if args.length == 2 && block
59 block = nil
60 end
61 offset = 0
62 result = []
63 while found = self.byteindex(pattern, offset)
64 result << self.byteslice(offset, found - offset)
65 offset = found + plen
66 result << if block
67 block.call(pattern).to_s
68 else
69 self.__sub_replace(replace, pattern, found)
70 end
71 if plen == 0
72 result << self.byteslice(offset, 1)
73 offset += 1
74 end
75 end
76 result << self.byteslice(offset..-1) if offset < length
77 result.join
78 end
79
80 ##
81 # Replace all matches of +pattern+ with +replacement+.
82 # Call block (if given) for each match and replace
83 # +pattern+ with the value of the block. Modify
84 # +self+ with the final value.
85 #
86 # ISO 15.2.10.5.19
87 def gsub!(*args, &block)
88 raise FrozenError, "can't modify frozen String" if frozen?
89 return to_enum(:gsub!, *args) if args.length == 1 && !block
90 str = self.gsub(*args, &block)
91 return nil unless self.index(args[0])
92 self.replace(str)
93 end
94
95 # ##
96 # # Calls the given block for each match of +pattern+
97 # # If no block is given return an array with all
98 # # matches of +pattern+.
99 # #
100 # # ISO 15.2.10.5.32
101 # def scan(pattern, &block)
102 # # TODO: String#scan is not implemented yet
103 # end
104
105 ##
106 # Replace only the first match of +pattern+ with
107 # +replacement+. Call block (if given) for each
108 # match and replace +pattern+ with the value of the
109 # block. Return the final value.
110 #
111 # ISO 15.2.10.5.36
112 def sub(*args, &block)
113 unless (1..2).include?(args.length)
114 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)"
115 end
116
117 pattern, replace = *args
118 if args.length == 2 && block
119 block = nil
120 end
121 result = []
122 found = self.index(pattern)
123 return self.dup unless found
124 result << self.byteslice(0, found)
125 offset = found + pattern.length
126 result << if block
127 block.call(pattern).to_s
128 else
129 self.__sub_replace(replace, pattern, found)
130 end
131 result << self.byteslice(offset..-1) if offset < length
132 result.join
133 end
134
135 ##
136 # Replace only the first match of +pattern+ with
137 # +replacement+. Call block (if given) for each
138 # match and replace +pattern+ with the value of the
139 # block. Modify +self+ with the final value.
140 #
141 # ISO 15.2.10.5.37
142 def sub!(*args, &block)
143 raise FrozenError, "can't modify frozen String" if frozen?
144 str = self.sub(*args, &block)
145 return nil unless self.index(args[0])
146 self.replace(str)
147 end
148
149 ##
150 # Call the given block for each byte of +self+.
151 def each_byte(&block)
152 return to_enum(:each_byte, &block) unless block
153 pos = 0
154 while pos < bytesize
155 block.call(getbyte(pos))
156 pos += 1
157 end
158 self
159 end
160
161 # those two methods requires Regexp that is optional in mruby
162 ##
163 # ISO 15.2.10.5.3
164 #def =~(re)
165 # re =~ self
166 #end
167
168 ##
169 # ISO 15.2.10.5.27
170 #def match(re, &block)
171 # re.match(self, &block)
172 #end
173 end