Repository navigation
Match MRI for sprintf hex-float strings and named-key KeyError - #506
Merged
Merged
Conversation
Advance the format-string binding (Kernel#sprintf / Kernel#format /
String#%) toward MRI 4.0.6 in three byte-for-byte respects, all in the
thin rbgo<->go-ruby-format binding layer:
- parseFormatFloat now parses a String operand exactly as Kernel#Float
does (the "%e/%f/%g behaves as if calling Kernel#Float" rule), routing
through normalizeFloatLiteral: a "0x…" hexadecimal-float literal is
accepted ("%f" % "0xA" => "10.000000") and an out-of-range magnitude
yields ±Inf / 0.0 rather than an error, while decimal parsing is
unchanged. Previously any hex string raised ArgumentError.
- An unmatched %<name> / %{name} reference now raises a KeyError whose
#key is the missing key as a Symbol and whose #receiver is the very
hash operand (identity), matching MRI's KeyError#key / #receiver.
- parseFormatInteger / parseFormatFloat return only the bare message on a
malformed operand. The engine promotes a parse error by wrapping
err.Error(); a *format.Error there (whose Error() is "Class: Message")
doubled the prefix, so "%d" % "abc" reported "ArgumentError: invalid
value for Integer(): \"abc\"". It now reads "invalid value for
Integer(): \"abc\"" as in MRI.
Spec deltas (ruby/spec, MRI 4.0.6):
- core/string/modulo_spec.rb: 245 -> 252 pass (+7: five %e/%E/%f/%g/%G
hex-string examples, two "sets the unmatched key" KeyError examples).
- core/kernel/sprintf_spec.rb / format_spec.rb: unchanged; their
remaining failures are the harness's unsupported `complain` matcher,
the encoding subsystem, and engine-internal parsing (%a alternate
form, %c to_str coercion, `*` positional width/precision), all outside
this binding.
New code is covered 100% by internal/vm/sprintf_conformance_test.go plus
the updated format_bind_internal_test.go; every expectation is verified
against MRI.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Advances MRI-Ruby-4.0.6 conformance of the sprintf /
Kernel#format/String#%cluster, entirely within the rbgo<->go-ruby-formatbindinglayer (
internal/vm/format_bind.go). Every behaviour is verifiedbyte-for-byte against MRI.
Fixes
Hexadecimal-float String operands —
parseFormatFloatnow parsesa String exactly as
Kernel#Float(the "%e/%f/%g behaves as ifcalling
Kernel#Float" rule) by routing throughnormalizeFloatLiteral:"%f" % "0xA"=>"10.000000", and anout-of-range magnitude yields ±Inf / 0.0 rather than raising. Decimal
parsing is unchanged. Previously any hex string raised
ArgumentError.KeyError#key/#receiverfor named references — an unmatched%<name>/%{name}reference now raises aKeyErrorwhose#keyis the missing key as a
Symboland whose#receiveris the veryhash operand (object identity), matching MRI.
Un-doubled coercion messages —
parseFormatInteger/parseFormatFloatreturn only the bare message on a malformedoperand. The engine wraps
err.Error(), so returning a*format.Error(whoseError()is"Class: Message") doubled theprefix.
"%d" % "abc"now reportsinvalid value for Integer(): "abc"instead ofArgumentError: invalid value for Integer(): "abc".Per-file spec deltas (ruby/spec, MRI 4.0.6)
core/string/modulo_spec.rbcore/kernel/sprintf_spec.rbcore/kernel/format_spec.rbThe +7 in
modulo_specare the five%e/%E/%f/%g/%Ghex-stringexamples and the two "sets the unmatched key as the key of KeyError"
examples.
Out of scope (engine / harness, not the binding)
Remaining failures live outside this repo's binding layer: the harness's
unsupported
complainmatcher, the String encoding subsystem(US-ASCII/BINARY negotiation,
Encoding::CompatibilityError), andgo-ruby-formatengine internals (%#aalternate form,%0…azero-fill placement,
%cto_strcoercion,*positionalwidth/precision,
Hash#defaultfor named refs which needs an enginelookup callback). These would need changes in the separate
go-ruby-format/formatmodule.Verification
go test ./internal/vm/andgo test ./...green (GOWORK=off CGO_ENABLED=0).GOOS=wasip1 GOARCH=wasm go build ./...OK.gofmt -lempty on changed files.(
internal/vm/sprintf_conformance_test.go+format_bind_internal_test.go).🤖 Generated with Claude Code