Repository navigation
Add String#undump - #445
Merged
Merged
Conversation
String#undump was undefined (NoMethodError). It now parses a String#dump literal
back into the original string, the exact inverse of the escaping String#dump
produces:
- the named escapes \a \b \t \n \v \f \r \e, plus \" \\ \#;
- \xHH (either hex case) to a byte;
- \uXXXX and \u{XXXX ...} (one or more space-separated code points) to UTF-8;
- printable ASCII kept verbatim; an unrecognized escape keeps its backslash;
- a trailing .force_encoding("NAME") suffix sets the result encoding.
The receiver must be an ASCII-compatible encoding (else
Encoding::CompatibilityError). A missing or excess wrapping quote, a malformed
\x or \u escape, an out-of-range code point, a raw NUL or non-ASCII byte, a
trailing backslash, or a malformed/unknown .force_encoding suffix each raises
RuntimeError with MRI's message.
This also fixes String#dump: a non-ASCII-compatible source now dumps to a
US-ASCII string (the escaped text is pure ASCII) rather than carrying the source
encoding, so String#dump.undump round-trips such a string — closing the one
error that remained in core/string/dump_spec.
Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero-
regression rubyspec sweep; core/string/undump_spec 0 -> 24, core/string/dump_spec
16 -> 17.
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.
String#undumpwas undefined (NoMethodError). It now parses aString#dumpliteral back into the original string — the exact inverse of
String#dump:\a \b \t \n \v \f \r \e, plus\"\\\#;\xHH(either hex case) to a byte;\uXXXXand\u{XXXX …}(one or more space-separated code points) to UTF-8;.force_encoding("NAME")suffix sets the result encoding.The receiver must be an ASCII-compatible encoding (else
Encoding::CompatibilityError). A missing or excess wrapping quote, a malformed\x/\uescape, an out-of-range code point, a raw NUL or non-ASCII byte, atrailing backslash, or a malformed/unknown
.force_encodingsuffix each raisesRuntimeErrorwith MRI's message.This also fixes
String#dump: a non-ASCII-compatible source now dumps to aUS-ASCII string (the escaped text is pure ASCII) rather than carrying the source
encoding, so
dump.undumpround-trips such a string — closing the one errorthat remained in
core/string/dump_spec.Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep;
core/string/undump_spec0 → 24,core/string/dump_spec16 → 17.🤖 Generated with Claude Code