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
58 changes: 44 additions & 14 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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] {
Expand All @@ -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
Expand Down
49 changes: 49 additions & 0 deletions internal/vm/string_hex_internal_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}