Commit ca0b63f2
ca0b63f27d27Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
Add Regexp support via oniguruma
Regex patterns compiled at startup as static regex_t* variables. Uses onigmo (Ruby's oniguruma fork) with direct extern declarations (no header dependency — generated C is self-contained).
Features:
- /pattern/ literals → compiled regex
- str =~ /pattern/ → onig_search, stores match groups
- $1, $2, $3 → capture group extraction from global region
- String#match?(/pat/) → boolean match test
- String#gsub(/pat/, repl) → global substitution
- String#sub(/pat/, repl) → first substitution
- String#scan(/pat/) { |m| ... } → iterate all matches
- String#split(/pat/) → split by regex delimiter
Only emitted when program uses regex — non-regexp programs need no oniguruma headers or linking.
Link with: /usr/lib/x86_64-linux-gnu/libonig.so.5 (or -lonig)
Test: examples/bm_regexp.rb — =~, captures, match?, gsub, sub, scan with block, split. Output matches Ruby exactly.
All regressions pass (no onig linking needed).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ca0b63f27d27
- Parent
- a943e77b3eec
examples/bm_regexp.rbaddedView file
1+
# Test Regexp support2+
3+
# match operator4+
if "hello 123 world" =~ /\d+/5+
puts "matched"6+
end7+
8+
# match with capture9+
str = "2024-03-17"10+
if str =~ /(\d{4})-(\d{2})-(\d{2})/11+
puts $1 # 202412+
puts $2 # 0313+
puts $3 # 1714+
end15+
16+
# String#match?17+
puts "hello".match?(/ell/) # true18+
puts "hello".match?(/xyz/) # false19+
20+
# String#gsub with regexp21+
puts "hello world".gsub(/o/, "0") # hell0 w0rld22+
23+
# String#sub with regexp24+
puts "hello world".sub(/o/, "0") # hell0 world25+
26+
# String#scan27+
"one 1 two 2 three 3".scan(/\d+/) do |m|28+
puts m29+
end30+
# 1, 2, 331+
32+
# String#split with regexp33+
parts = "a, b, c".split(/,\s*/)34+
puts parts.length # 3src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…