10 # Calls the given block for each line
11 # and pass the respective line.
14 def each_line(separator = "\n", &block)
15 return to_enum(:each_line, separator) unless block
21 raise TypeError unless separator.is_a?(String)
23 paragraph_mode = false
31 sep_len = separator.length
32 should_yield_subclass_instances = self.class != String
34 while (pointer = string.index(separator, start))
36 pointer += 1 while paragraph_mode && string[pointer] == "\n"
37 if should_yield_subclass_instances
38 block.call(self.class.new(string[start, pointer - start]))
40 block.call(string[start, pointer - start])
44 return self if start == self_len
46 if should_yield_subclass_instances
47 block.call(self.class.new(string[start, self_len - start]))
49 block.call(string[start, self_len - start])
54 # private method for gsub/sub
55 def __sub_replace(pre, m, post)
58 while j = index("\\", i)
59 break if j == length-1
69 when "1", "2", "3", "4", "5", "6", "7", "8", "9"
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. Return the
87 def gsub(*args, &block)
88 return to_enum(:gsub, *args) if args.length == 1 && !block
89 raise ArgumentError, "wrong number of arguments" unless (1..2).include?(args.length)
91 pattern, replace = *args
93 if args.length == 2 && block
96 if !replace.nil? || !block
101 while found = index(pattern, offset)
102 result << self[offset, found - offset]
103 offset = found + plen
105 block.call(pattern).to_s
107 replace.__sub_replace(self[0, found], pattern, self[offset..-1] || "")
110 result << self[offset, 1]
114 result << self[offset..-1] if offset < length
119 # Replace all matches of +pattern+ with +replacement+.
120 # Call block (if given) for each match and replace
121 # +pattern+ with the value of the block. Modify
122 # +self+ with the final value.
125 def gsub!(*args, &block)
126 raise FrozenError, "can't modify frozen String" if frozen?
127 return to_enum(:gsub!, *args) if args.length == 1 && !block
128 str = self.gsub(*args, &block)
129 return nil unless self.index(args[0])
134 # # Calls the given block for each match of +pattern+
135 # # If no block is given return an array with all
136 # # matches of +pattern+.
139 # def scan(pattern, &block)
140 # # TODO: String#scan is not implemented yet
144 # Replace only the first match of +pattern+ with
145 # +replacement+. Call block (if given) for each
146 # match and replace +pattern+ with the value of the
147 # block. Return the final value.
150 def sub(*args, &block)
151 unless (1..2).include?(args.length)
152 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)"
155 pattern, replace = *args
157 if args.length == 2 && block
165 found = index(pattern)
166 return this unless found
167 result << this[0, found]
168 offset = found + pattern.length
170 block.call(pattern).to_s
172 replace.__sub_replace(this[0, found], pattern, this[offset..-1] || "")
174 result << this[offset..-1] if offset < length
179 # Replace only the first match of +pattern+ with
180 # +replacement+. Call block (if given) for each
181 # match and replace +pattern+ with the value of the
182 # block. Modify +self+ with the final value.
185 def sub!(*args, &block)
186 raise FrozenError, "can't modify frozen String" if frozen?
187 str = self.sub(*args, &block)
188 return nil unless self.index(args[0])
193 # Call the given block for each byte of +self+.
194 def each_byte(&block)
195 return to_enum(:each_byte, &block) unless block
198 while pos < bytes.size
199 block.call(bytes[pos])
205 # those two methods requires Regexp that is optional in mruby
214 #def match(re, &block)
215 # re.match(self, &block)