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
27 changes: 27 additions & 0 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,33 @@ public IRubyObject getbyte(ThreadContext context) {
return get_byte(context);
}

@JRubyMethod(name = "scan_byte")
public IRubyObject scan_byte(ThreadContext context) {
Ruby runtime = context.runtime;
check(context);
clearMatched();
if (curr >= str.getByteList().getRealSize()) return context.nil;

byte[] bytes = str.getBytes();

byte bite = bytes[curr];
prev = curr;
curr++;

setMatched();
adjustRegisters();
return RubyFixnum.newFixnum(context.runtime, bite & 0xff);
}

@JRubyMethod(name = "peek_byte")
public IRubyObject peek_byte(ThreadContext context) {
Ruby runtime = context.runtime;
check(context);
if (curr >= str.getByteList().getRealSize()) return context.nil;

return RubyFixnum.newFixnum(context.runtime, (str.getBytes()[curr]) & 0xff);
}

@JRubyMethod(name = "peek")
public IRubyObject peek(ThreadContext context, IRubyObject length) {
check(context);
Expand Down
55 changes: 55 additions & 0 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,57 @@ strscan_getch(VALUE self)
adjust_register_position(p, p->regs.end[0]));
}

/*
* Scans one byte and returns it as an integer.
* This method is not multibyte character sensitive.
* See also: #getch.
*
* s = StringScanner.new('ab')
* s.scan_byte # => 97
* s.scan_byte # => 98
* s.scan_byte # => nil
*
* s = StringScanner.new("\244\242".force_encoding("euc-jp"))
* s.scan_byte # => 0xA4
* s.scan_byte # => 0xA2
* s.scan_byte # => nil
*/
static VALUE
strscan_scan_byte(VALUE self)
{
struct strscanner *p;

GET_SCANNER(self, p);
CLEAR_MATCH_STATUS(p);
if (EOS_P(p))
return Qnil;

VALUE byte = INT2FIX((unsigned char)*CURPTR(p));
p->prev = p->curr;
p->curr++;
MATCHED(p);
adjust_registers_to_matched(p);
return byte;
}

/*
* Peeks at the current byte and returns it as an integer.
*
* s = StringScanner.new('ab')
* s.peek_byte # => 97
*/
static VALUE
strscan_peek_byte(VALUE self)
{
struct strscanner *p;

GET_SCANNER(self, p);
if (EOS_P(p))
return Qnil;

return INT2FIX((unsigned char)*CURPTR(p));
}

/*
* Scans one byte and returns it.
* This method is not multibyte character sensitive.
Expand Down Expand Up @@ -1605,6 +1656,7 @@ strscan_named_captures(VALUE self)
*
* - #getch
* - #get_byte
* - #scan_byte
* - #scan
* - #scan_until
* - #skip
Expand All @@ -1617,6 +1669,7 @@ strscan_named_captures(VALUE self)
* - #exist?
* - #match?
* - #peek
* - #peek_byte
*
* === Finding Where we Are
*
Expand Down Expand Up @@ -1708,7 +1761,9 @@ Init_strscan(void)
rb_define_method(StringScanner, "getch", strscan_getch, 0);
rb_define_method(StringScanner, "get_byte", strscan_get_byte, 0);
rb_define_method(StringScanner, "getbyte", strscan_getbyte, 0);
rb_define_method(StringScanner, "scan_byte", strscan_scan_byte, 0);
rb_define_method(StringScanner, "peek", strscan_peek, 1);
rb_define_method(StringScanner, "peek_byte", strscan_peek_byte, 0);
rb_define_method(StringScanner, "peep", strscan_peep, 1);

rb_define_method(StringScanner, "unscan", strscan_unscan, 0);
Expand Down
1 change: 1 addition & 0 deletions run-test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env ruby

gem 'strscan'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! I didn't notice that we need this to use installed gem with Ruby 3.2 and 3.3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might be able to use bundle exec? I'm not sure what the policy of bundled gems is. I thought you didn't need gem either, but it seems to fix tests in CI.

require 'strscan'
puts "Loaded strscan from #{$".grep(/\/strscan\./).join(', ')}"
puts "Gem from #{Gem.loaded_specs["strscan"]&.full_gem_path}"
Expand Down
23 changes: 23 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
require 'test/unit'

module StringScannerTests
def test_peek_byte
s = create_string_scanner('ab')
assert_equal 97, s.peek_byte
assert_equal 97, s.scan_byte
assert_equal 98, s.peek_byte
assert_equal 98, s.scan_byte
assert_nil s.peek_byte
assert_nil s.scan_byte
end

def test_scan_byte
s = create_string_scanner('ab')
assert_equal 97, s.scan_byte
assert_equal 98, s.scan_byte
assert_nil s.scan_byte

str = "\244\242".dup.force_encoding("euc-jp")
s = StringScanner.new(str)
assert_equal str.getbyte(s.pos), s.scan_byte
assert_equal str.getbyte(s.pos), s.scan_byte
assert_nil s.scan_byte
end

def test_s_new
s = create_string_scanner('test string')
assert_instance_of StringScanner, s
Expand Down