Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions mrbgems/mruby-regexp/mrblib/string_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ def gsub(pattern, replacement = nil, &block)
end
# block case: keep in Ruby to avoid VM callback from C
parts = []
rest = self
while rest.length > 0
md = pattern.match(rest)
pos = 0
len = self.bytesize
while pos <= len
md = pattern.match(self, pos)
break unless md
parts << md.pre_match
match_start = md.begin(0)
match_end = md.end(0)
parts << self.byteslice(pos, match_start - pos)
parts << block.call(md[0]).to_s
matched_len = md[0].length
if matched_len == 0
parts << rest[0] if rest.length > 0
rest = rest[1..-1] || ""
if match_start == match_end
rest = self.byteslice(match_end..-1)
if rest && rest.bytesize > 0
char = rest[0]
parts << char
pos = match_end + char.bytesize
else
pos = match_end + 1
end
else
rest = md.post_match
pos = match_end
end
end
parts << rest
parts << self.byteslice(pos..-1)
parts.join
end

Expand Down
4 changes: 2 additions & 2 deletions mrbgems/mruby-regexp/src/re_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ add_thread(pike_state *s, re_threadlist *list,
continue;

case RE_BOL:
if (sp == s->str || ((s->pat->flags & RE_FLAG_MULTILINE) && sp > s->str && sp[-1] == '\n')) {
if (sp == s->str || ((s->pat->flags & RE_FLAG_MULTILINE) && sp > s->str && sp < s->str_end && sp[-1] == '\n')) {
pc++; continue;
}
return;
Expand Down Expand Up @@ -452,7 +452,7 @@ bt_match(const mrb_regexp_pattern *pat, const char *str, const char *str_end,
}

case RE_BOL:
if (sp != str && !(pat->flags & RE_FLAG_MULTILINE && sp > str && sp[-1] == '\n')) return FALSE;
if (sp != str && !(pat->flags & RE_FLAG_MULTILINE && sp > str && sp < str_end && sp[-1] == '\n')) return FALSE;
pc++;
break;

Expand Down
14 changes: 14 additions & 0 deletions mrbgems/mruby-regexp/test/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,20 @@
assert_equal "HELLO WORLD", "hello world".gsub(/\w+/) { |m| m.upcase }
end

assert("String#gsub with block and zero-width match") do
assert_equal "!abc", "abc".gsub(/^/) { "!" }
assert_equal "a!bc", "abc".gsub(/(?=b)/) { "!" }
assert_equal "!a!b!c!", "abc".gsub(//) { "!" }
assert_equal "!\n", "\n".gsub(/^/m) { "!" }
assert_equal "!a\n", "a\n".gsub(/^/m) { "!" }
assert_equal "!a\n!b", "a\nb".gsub(/^/m) { "!" }
if __ENCODING__ == "UTF-8"
assert_equal "!いろは", "いろは".gsub(/^/) { "!" }
assert_equal "い!ろは", "いろは".gsub(/(?=ろ)/) { "!" }
assert_equal "!い!ろ!は!", "いろは".gsub(//) { "!" }
end
end

assert("String#gsub date reformat") do
result = "2026-03-21".gsub(/(\d+)-(\d+)-(\d+)/) { "#{$~[3]}/#{$~[2]}/#{$~[1]}" }
assert_equal "21/03/2026", result
Expand Down
Loading