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 support
2+
3+# match operator
4+if "hello 123 world" =~ /\d+/
5+ puts "matched"
6+end
7+
8+# match with capture
9+str = "2024-03-17"
10+if str =~ /(\d{4})-(\d{2})-(\d{2})/
11+ puts $1 # 2024
12+ puts $2 # 03
13+ puts $3 # 17
14+end
15+
16+# String#match?
17+puts "hello".match?(/ell/) # true
18+puts "hello".match?(/xyz/) # false
19+
20+# String#gsub with regexp
21+puts "hello world".gsub(/o/, "0") # hell0 w0rld
22+
23+# String#sub with regexp
24+puts "hello world".sub(/o/, "0") # hell0 world
25+
26+# String#scan
27+"one 1 two 2 three 3".scan(/\d+/) do |m|
28+ puts m
29+end
30+# 1, 2, 3
31+
32+# String#split with regexp
33+parts = "a, b, c".split(/,\s*/)
34+puts parts.length # 3