Skip to content

Commit 1df1538

Browse files
committed
merge revision(s) f543698, 338eb00, ac636f5: [Backport #20516]
Revert "Rollback to released version numbers of stringio and strscan" This reverts commit 6a79e53. [ruby/strscan] StringScanner#captures: Return nil not "" for unmached capture (ruby/strscan#72) fix ruby/strscan#70 If there is no substring matching the group (s[3]), the behavior is different. If there is no substring matching the group, the corresponding element (s[3]) should be nil. ``` s = StringScanner.new('foobarbaz') #=> #<StringScanner 0/9 @ "fooba..."> s.scan /(foo)(bar)(BAZ)?/ #=> "foobar" s[0] #=> "foobar" s[1] #=> "foo" s[2] #=> "bar" s[3] #=> nil s.captures #=> ["foo", "bar", ""] s.captures.compact #=> ["foo", "bar", ""] ``` ``` s = StringScanner.new('foobarbaz') #=> #<StringScanner 0/9 @ "fooba..."> s.scan /(foo)(bar)(BAZ)?/ #=> "foobar" s[0] #=> "foobar" s[1] #=> "foo" s[2] #=> "bar" s[3] #=> nil s.captures #=> ["foo", "bar", nil] s.captures.compact #=> ["foo", "bar"] ``` https://docs.ruby-lang.org/ja/latest/method/MatchData/i/captures.html ``` /(foo)(bar)(BAZ)?/ =~ "foobarbaz" #=> 0 $~.to_a #=> ["foobar", "foo", "bar", nil] $~.captures #=> ["foo", "bar", nil] $~.captures.compact #=> ["foo", "bar"] ``` * StringScanner#captures is not yet documented. https://docs.ruby-lang.org/ja/latest/class/StringScanner.html ruby/strscan@1fbfdd3c6f [ruby/strscan] Bump version ruby/strscan@d6f97ec102
1 parent 0a0338b commit 1df1538

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

ext/stringio/stringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
**********************************************************************/
1414

1515
static const char *const
16-
STRINGIO_VERSION = "3.1.0";
16+
STRINGIO_VERSION = "3.1.1";
1717

1818
#include "ruby.h"
1919
#include "ruby/io.h"

ext/strscan/strscan.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern size_t onig_region_memsize(const struct re_registers *regs);
2222

2323
#include <stdbool.h>
2424

25-
#define STRSCAN_VERSION "3.0.7"
25+
#define STRSCAN_VERSION "3.0.9"
2626

2727
/* =======================================================================
2828
Data Type Definitions
@@ -1243,10 +1243,10 @@ strscan_size(VALUE self)
12431243
* If nothing was priorly matched, it returns nil.
12441244
*
12451245
* s = StringScanner.new("Fri Dec 12 1975 14:39")
1246-
* s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 "
1247-
* s.captures # -> ["Fri", "Dec", "12"]
1248-
* s.scan(/(\w+) (\w+) (\d+) /) # -> nil
1249-
* s.captures # -> nil
1246+
* s.scan(/(\w+) (\w+) (\d+) (1980)?/) # -> "Fri Dec 12 "
1247+
* s.captures # -> ["Fri", "Dec", "12", nil]
1248+
* s.scan(/(\w+) (\w+) (\d+) (1980)?/) # -> nil
1249+
* s.captures # -> nil
12501250
*/
12511251
static VALUE
12521252
strscan_captures(VALUE self)
@@ -1262,9 +1262,13 @@ strscan_captures(VALUE self)
12621262
new_ary = rb_ary_new2(num_regs);
12631263

12641264
for (i = 1; i < num_regs; i++) {
1265-
VALUE str = extract_range(p,
1266-
adjust_register_position(p, p->regs.beg[i]),
1267-
adjust_register_position(p, p->regs.end[i]));
1265+
VALUE str;
1266+
if (p->regs.beg[i] == -1)
1267+
str = Qnil;
1268+
else
1269+
str = extract_range(p,
1270+
adjust_register_position(p, p->regs.beg[i]),
1271+
adjust_register_position(p, p->regs.end[i]));
12681272
rb_ary_push(new_ary, str);
12691273
}
12701274

test/strscan/test_stringscanner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,8 @@ def test_size
737737
def test_captures
738738
s = create_string_scanner("Timestamp: Fri Dec 12 1975 14:39")
739739
s.scan("Timestamp: ")
740-
s.scan(/(\w+) (\w+) (\d+) /)
741-
assert_equal(["Fri", "Dec", "12"], s.captures)
740+
s.scan(/(\w+) (\w+) (\d+) (1980)?/)
741+
assert_equal(["Fri", "Dec", "12", nil], s.captures)
742742
s.scan(/(\w+) (\w+) (\d+) /)
743743
assert_nil(s.captures)
744744
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 2
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 84
14+
#define RUBY_PATCHLEVEL 85
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)