From 7d0aa75f551c442c16adbf94c29e7bee9b50de37 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 10:35:40 +0200 Subject: [PATCH] Add the 'u' (uuencode) pack/unpack directive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Array#pack("u") and String#unpack("u") raised "unknown directive 'u'". They now implement uuencoding and uudecoding: - pack("u") emits one line per chunk: a length character (data-byte count), then the bytes in groups of three as four 6-bit characters (value 0 as a backtick, others as value+0x20), then a newline. The count modifier sets the bytes-per-line — '*', 0, 1, 2, or none means 45; otherwise it is rounded down to a multiple of three. An empty input yields an empty string, a non-String element raises TypeError, and the result is US-ASCII. - unpack("u") decodes the whole input across newline-separated lines back to a single BINARY string, ignoring the count/'*' modifier; each character contributes (c-0x20)&0x3f and a CR?LF terminates a line. Decoding stops at the first character that cannot begin a line. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/array/pack/u_spec 17 -> 35 and core/string/unpack/u_spec 17 -> 25 (with the remaining failures in both files being pre-existing 'U'-directive and shared-mock issues, unrelated to 'u'). Two shared examples in pack/a_spec and pack/z_spec also start passing now that 'u' is a recognized directive. Co-Authored-By: Claude Opus 4.8 --- internal/vm/pack.go | 8 ++- internal/vm/pack_uu.go | 99 ++++++++++++++++++++++++++++ internal/vm/pack_uu_internal_test.go | 68 +++++++++++++++++++ 3 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 internal/vm/pack_uu.go create mode 100644 internal/vm/pack_uu_internal_test.go diff --git a/internal/vm/pack.go b/internal/vm/pack.go index 0bc1da6d..a3bbf409 100644 --- a/internal/vm/pack.go +++ b/internal/vm/pack.go @@ -176,7 +176,7 @@ func isPackCode(c byte) bool { case 'C', 'c', 'S', 's', 'L', 'l', 'Q', 'q', 'I', 'i', 'J', 'j', 'n', 'N', 'v', 'V', 'a', 'A', 'Z', 'H', 'h', 'U', 'f', 'F', 'd', 'D', 'e', 'E', 'g', 'G', - 'x', 'X', '@', 'b', 'B', 'm', 'M': + 'x', 'X', '@', 'b', 'B', 'm', 'M', 'u': return true } return false @@ -461,6 +461,8 @@ func (vm *VM) packBytes(elems []object.Value, fmtStr string) ([]byte, string) { out = packBase64(out, d, vm.packStrArg(next())) case d.code == 'M': out = qpencode(out, []byte(vm.displayStr(next())), packQPLen(d)) + case d.code == 'u': + out = packUuencode(out, d, vm.packStrArg(next())) } } return out, packEncoding(dirs) @@ -1061,6 +1063,10 @@ func unpackElems(data []byte, fmtStr string) []object.Value { dec := qpdecode(data[pos:]) pos = len(data) out = append(out, object.NewStringBytesEnc(dec, "ASCII-8BIT")) + case d.code == 'u': + dec := uudecode(data[pos:]) + pos = len(data) + out = append(out, object.NewStringBytesEnc(dec, "ASCII-8BIT")) } } return out diff --git a/internal/vm/pack_uu.go b/internal/vm/pack_uu.go new file mode 100644 index 00000000..0448cdaa --- /dev/null +++ b/internal/vm/pack_uu.go @@ -0,0 +1,99 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +// uuChar encodes a 6-bit value as a uuencode output character: MRI writes value +// 0 as a backtick (`) rather than the traditional space, and every other value +// as value+0x20. +func uuChar(v byte) byte { + if v &= 0x3f; v == 0 { + return '`' + } + return v + 0x20 +} + +// packUuencode implements Array#pack's 'u' directive (uuencoding). Each output +// line begins with a length character (the count of data bytes it encodes) and +// then the bytes in groups of three encoded as four 6-bit characters, and ends +// with a newline. The count modifier sets the bytes-per-line: '*', 0, 1, 2, or +// no count means 45, otherwise it is rounded down to a multiple of three. An +// empty input produces empty output. +func packUuencode(out []byte, d packDir, b []byte) []byte { + length := d.count + if length <= 2 { + length = 45 + } else { + length = length / 3 * 3 + } + for len(b) > 0 { + todo := length + if len(b) < todo { + todo = len(b) + } + out = uuEncodeLine(out, b[:todo]) + b = b[todo:] + } + return out +} + +// uuEncodeLine appends one uuencoded line (length character, encoded triples, +// trailing newline) for the data slice s, which is at most one line's worth. +func uuEncodeLine(out, s []byte) []byte { + out = append(out, uuChar(byte(len(s)))) + for i := 0; i < len(s); i += 3 { + var c0, c1, c2 byte + c0 = s[i] + if i+1 < len(s) { + c1 = s[i+1] + } + if i+2 < len(s) { + c2 = s[i+2] + } + out = append(out, + uuChar(c0>>2), + uuChar(c0<<4|c1>>4), + uuChar(c1<<2|c2>>6), + uuChar(c2)) + } + return append(out, '\n') +} + +// uudecode implements String#unpack's 'u' directive (uudecoding). It reads the +// whole input, one line at a time: the first character of a line gives the +// number of data bytes it holds, the rest decode in groups of four characters +// back to three bytes (each character contributing (c-0x20)&0x3f), and newlines +// separate lines. Decoding stops at the first character that cannot begin a +// line (<= space or >= 'a'). +func uudecode(data []byte) []byte { + var out []byte + i, n := 0, len(data) + for i < n && data[i] > ' ' && data[i] < 'a' { + length := int((data[i] - ' ') & 0x3f) + i++ + for length > 0 { + var v [4]byte + for k := 0; k < 4; k++ { + if i < n && data[i] != '\n' && data[i] != '\r' { + v[k] = (data[i] - ' ') & 0x3f + i++ + } + } + b := [3]byte{v[0]<<2 | v[1]>>4, v[1]<<4 | v[2]>>2, v[2]<<6 | v[3]} + for k := 0; k < 3 && length > 0; k++ { + out = append(out, b[k]) + length-- + } + } + // Consume the line's terminator (CR?LF). Anything else the outer loop's + // range test rejects, stopping decoding — MRI does not skip trailing junk. + if i < n && data[i] == '\r' { + i++ + } + if i < n && data[i] == '\n' { + i++ + } + } + return out +} diff --git a/internal/vm/pack_uu_internal_test.go b/internal/vm/pack_uu_internal_test.go new file mode 100644 index 00000000..870894ed --- /dev/null +++ b/internal/vm/pack_uu_internal_test.go @@ -0,0 +1,68 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestPackUuencode covers Array#pack's 'u' directive: the length character and +// 1/2/3-byte group encoding, the count modifier selecting bytes-per-line (45 by +// default, rounded down to a multiple of three otherwise), an empty input, one +// element per directive, a US-ASCII result, and the TypeError for a non-String +// element. Expectations use \x60 for the backtick (value-zero) character. +// Verified against ruby 4.0.6. +func TestPackUuencode(t *testing.T) { + cases := []struct{ src, want string }{ + {`p ["a"].pack("u") == "!80\x60\x60\n"`, `true`}, + {`p ["ab"].pack("u") == "\"86(\x60\n"`, `true`}, + {`p ["abc"].pack("u") == "#86)C\n"`, `true`}, + {`p ["hello"].pack("u") == "%:&5L;&\\\x60\n"`, `true`}, + {`p [""].pack("u") == ""`, `true`}, + // Count modifiers <= 2 (and * and none) mean 45 bytes per line. + {`p ["a"].pack("u0") == ["a"].pack("u")`, `true`}, + {`p ["a"].pack("u*") == ["a"].pack("u")`, `true`}, + {`p ["a"].pack("u2") == ["a"].pack("u")`, `true`}, + // A count >= 3 is rounded down to a multiple of three. + {`p ["abcdefg"].pack("u3") == "#86)C\n#9&5F\n!9P\x60\x60\n"`, `true`}, + {`p ["abcdefghijklm"].pack("u7") == "&86)C9&5F\n&9VAI:FML\n!;0\x60\x60\n"`, `true`}, + // One element per directive. + {`p ["abc", "DEF"].pack("uu") == "#86)C\n#1$5&\n"`, `true`}, + // The result is US-ASCII. + {`p ["abcd"].pack("u").encoding.name`, `"US-ASCII"`}, + // A non-String element (Integer, nil) raises TypeError. + {`begin; [0].pack("u"); rescue TypeError => e; p e.class; end`, `TypeError`}, + {`begin; [nil].pack("u"); rescue TypeError => e; p e.class; end`, `TypeError`}, + } + 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") + } + } +} + +// TestUnpackUudecode covers String#unpack's 'u' directive: decoding across +// newline-separated lines with a single directive, empty strings for directives +// beyond the input, the count/'*' modifier being ignored, an empty input, and +// the BINARY result encoding. Verified against ruby 4.0.6. +func TestUnpackUudecode(t *testing.T) { + cases := []struct{ src, want string }{ + {`p "#86)C\n#1$5&\n".unpack("u")`, `["abcDEF"]`}, + {`p "#86)C\n#1$5&\n".unpack("uuu")`, `["abcDEF", "", ""]`}, + {`p "#86)C\n#1$5&\n".unpack("u*")`, `["abcDEF"]`}, + {`p "#86)C\n#1$5&\n".unpack("u238")`, `["abcDEF"]`}, + {`p "".unpack("u")`, `[""]`}, + {`p "".unpack("u")[0].encoding.name`, `"ASCII-8BIT"`}, + {`p "!80\x60\x60\n".unpack("u")`, `["a"]`}, + // CR?LF line endings are consumed; other trailing characters stop decoding. + {`p "#86)C\r\n#1$5&\r\n".unpack("u")`, `["abcDEF"]`}, + {`p "#86)C junk\n#1$5&\n".unpack("u")`, `["abc"]`}, + // A round trip is the identity for every byte value. + {`s = (0..255).to_a.pack("C*"); p [s].pack("u").unpack("u")[0].bytes == s.bytes`, `true`}, + } + 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") + } + } +}