From 220af28e4738882bc9f6f93b510783d45e152d21 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 10:46:23 +0200 Subject: [PATCH] Add String#hex String#hex was undefined (NoMethodError). It reads the string as a base-16 integer: an optional leading sign, an optional "0x"/"0X" prefix (b/d/o are hex digits, so no other radix prefix is special), hex digits with a single underscore allowed between them, stopping at the first non-hex character and returning 0 when no digit is present; a value beyond a machine word yields a Bignum. The digit scan, underscore rule, and Bignum conversion are factored out of strOct into a shared accumInum (with inumSignSkip for the leading sign), so String#oct and String#hex share one implementation rather than a copy. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/string/hex_spec 0 -> 7, core/string/oct_spec unchanged at 17. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 58 +++++++++++++++++++------ internal/vm/string_hex_internal_test.go | 49 +++++++++++++++++++++ 2 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 internal/vm/string_hex_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 75bd41d6..e5b316a7 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1827,6 +1827,9 @@ func (vm *VM) bootstrap() { vm.cString.define("oct", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return strOct(strOf(self)) }) + vm.cString.define("hex", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return strHex(strOf(self)) + }) vm.cString.define("to_s", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return self }) @@ -5496,6 +5499,39 @@ func digitValue(c byte) int { // digits valid for that base (with the single-underscore separator rule) — and // stops at the first invalid character, returning 0 when no digits are present. func strOct(s string) object.Value { + i, neg := inumSignSkip(s) + base := 8 + if i+1 < len(s) && s[i] == '0' { + switch s[i+1] { + case 'x', 'X': + base, i = 16, i+2 + case 'b', 'B': + base, i = 2, i+2 + case 'o', 'O': + base, i = 8, i+2 + case 'd', 'D': + base, i = 10, i+2 + } + } + return accumInum(s, i, base, neg) +} + +// strHex implements String#hex: the string is read as a base-16 integer, with an +// optional leading sign and an optional "0x"/"0X" prefix (no other radix prefix +// is special, since b/d/o are themselves hex digits). It shares the digit scan +// and stop rules with strOct via accumInum, and returns 0 when no hex digit +// follows. +func strHex(s string) object.Value { + i, neg := inumSignSkip(s) + if i+1 < len(s) && s[i] == '0' && (s[i+1] == 'x' || s[i+1] == 'X') { + i += 2 + } + return accumInum(s, i, 16, neg) +} + +// inumSignSkip skips leading ASCII whitespace and an optional sign, returning the +// index of the first following character and whether the sign was negative. +func inumSignSkip(s string) (int, bool) { i, n := 0, len(s) for i < n { switch s[i] { @@ -5510,22 +5546,16 @@ func strOct(s string) object.Value { neg = s[i] == '-' i++ } - base := 8 - if i+1 < n && s[i] == '0' { - switch s[i+1] { - case 'x', 'X': - base, i = 16, i+2 - case 'b', 'B': - base, i = 2, i+2 - case 'o', 'O': - base, i = 8, i+2 - case 'd', 'D': - base, i = 10, i+2 - } - } + return i, neg +} + +// accumInum scans the base-`base` digits of s starting at i (allowing a single +// underscore between digits) up to the first invalid character, and returns the +// resulting Integer, negated when neg, or 0 when no digit is present. +func accumInum(s string, i, base int, neg bool) object.Value { var digits []byte prevUnderscore := false - for i < n { + for i < len(s) { if s[i] == '_' { if len(digits) == 0 || prevUnderscore { break diff --git a/internal/vm/string_hex_internal_test.go b/internal/vm/string_hex_internal_test.go new file mode 100644 index 00000000..b6582188 --- /dev/null +++ b/internal/vm/string_hex_internal_test.go @@ -0,0 +1,49 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestStringHex covers String#hex: the string is read as a base-16 integer with +// an optional sign and an optional 0x/0X prefix (b/d/o are hex digits, not radix +// prefixes), a single underscore is allowed between digits, leading whitespace is +// skipped, scanning stops at the first non-hex character, and the result is 0 +// when no hex digit is present. A value beyond a machine word yields a Bignum. +// Verified against ruby 4.0.6. +func TestStringHex(t *testing.T) { + cases := []struct{ src, want string }{ + {`p "0x0a".hex`, `10`}, + {`p "ff".hex`, `255`}, + {`p "0xff".hex`, `255`}, + {`p "0X1F".hex`, `31`}, + {`p "-1234".hex`, `-4660`}, + {`p "+0x10".hex`, `16`}, + {`p " 0xa".hex`, `10`}, + {`p "deadbeef".hex`, `3735928559`}, + {`p "1_0".hex`, `16`}, + // b/d/o are hex digits, so only 0x/0X is a prefix. + {`p "0b11".hex`, `2833`}, + {`p "0d10".hex`, `3344`}, + {`p "0o17".hex`, `0`}, + // Stops at the first non-hex character; 0 when there is no digit. + {`p "10 ".hex`, `16`}, + {`p "wombat".hex`, `0`}, + {`p "0xg".hex`, `0`}, + {`p "_10".hex`, `0`}, + {`p "0x".hex`, `0`}, + {`p "".hex`, `0`}, + // A value past 64 bits becomes a Bignum. + {`p "0xFFFFFFFFFFFFFFFFFF".hex`, `4722366482869645213695`}, + // oct still resolves every radix prefix (shares accumInum with hex). + {`p "0b1010".oct`, `10`}, + {`p "0x1f".oct`, `31`}, + {`p "777".oct`, `511`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}