Skip to content

Use rb_len_t for String lengths and positions - #222

Open
hsbt wants to merge 3 commits into
masterfrom
claude/frosty-grothendieck-e647e7
Open

hsbt wants to merge 3 commits into
masterfrom
claude/frosty-grothendieck-e647e7

Conversation

@hsbt

@hsbt hsbt commented Oct 11, 2026

Copy link
Copy Markdown
Member

Ruby master now has rb_len_t for String and Array lengths and indices (https://bugs.ruby-lang.org/issues/22400). It is long on every platform for now, and the proposal widens it on mswin, where long is 32 bits. This switches the scan positions and the lengths derived from them to rb_len_t, with a fallback to long on Ruby without HAVE_RB_LEN_T, so nothing changes on released Ruby.

The second commit fixes an existing bug where long is 32 bits. #rest_size, #skip, #skip_until, #match? and #exist? returned a length of 2**30 or more through INT2FIX, and on x64-mswin64 the result compared unequal to the right number.

I ran rake compile and rake test on mswin with Ruby 3.3 and a 4.1.0dev snapshot that predates rb_len_t. ruby-head in CI covers the HAVE_RB_LEN_T side.

Generated with Claude Code

hsbt and others added 2 commits October 10, 2026 03:51
It falls back to long on Ruby versions that do not define HAVE_RB_LEN_T, so the change also builds there.

https://bugs.ruby-lang.org/issues/22400

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#rest_size, #skip, #match?, #skip_until and #exist? passed the length to INT2FIX, which does not fit past FIXNUM_MAX. On x64-mswin64 the result printed as the right number but was not equal to it, and `rest_size + rest_size` came out negative.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 11, 2026 03:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The conversions consistently follow Ruby’s new length API while retaining backward compatibility.

0 open findings

What changed in this PR

Updates StringScanner to use Ruby’s rb_len_t for string lengths and positions while preserving compatibility with older Ruby versions.

Changes:

  • Migrates position and length handling to rb_len_t.
  • Adds fallback conversion and formatting macros.
  • Fixes large length results previously converted with INT2FIX.
File Description
ext/​strscan/​strscan.c Widens length handling and corrects large integer conversions.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread ext/strscan/strscan.c Outdated
Comment on lines 1735 to 1736
static rb_len_t
resolve_capture_index(struct strscanner *p, VALUE specifier)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this correct? It seems that the "index" is not a length. Should we keep long? (Or we may want to change to size_t but it's out of scope of rb_len_t support.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I checked, and Onigmo uses int for it, so long was right. I've changed it back.

They index the match registers, whose count is an int, rather than a String, so rb_len_t does not apply. Only the length of a group name given as a String stays in rb_len_t.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 07:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The conversions are consistent, backward compatible, and address the large-length bug without unresolved issues.

0 open findings

🧠 Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants