Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative fix for #236 that avoids decoding entire string #237

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 6 additions & 5 deletions interp/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,16 @@ func (p *interp) sprintf(format string, args []value) (string, error) {
n, isStr := a.isTrueStr()
if isStr {
s := p.toString(a)
if len(s) > 0 {
c = []byte{s[0]}
_, size := utf8.DecodeRuneInString(s)
if size > 0 {
c = []byte(s[:size])
} else {
c = []byte{0}
}
} else {
// Follow the behaviour of awk and mawk, where %c
// operates on bytes (0-255), not Unicode codepoints
c = []byte{byte(n)}
c = make([]byte, utf8.UTFMax)
size := utf8.EncodeRune(c, rune(n))
c = c[:size]
}
v = c
}
Expand Down
10 changes: 5 additions & 5 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ NR==3, NR==5 { print NR }
{`BEGIN { printf "%.1g", 42 } # !windows-gawk`, "", "4e+01", "", ""}, // for some reason gawk gives "4e+001" on Windows
{`BEGIN { printf "%d", 12, 34 }`, "", "12", "", ""},
{`BEGIN { printf "%d" }`, "", "", "format error: got 0 args, expected 1", "not enough arg"},
// Our %c handling is mostly like awk's, except for multiples
// 256, where awk is weird, and we're like mawk
{`BEGIN { printf "%c", 0 }`, "", "\x00", "", ""},
{`BEGIN { printf "%c", 127 }`, "", "\x7f", "", ""},
{`BEGIN { printf "%c", 128 } # !gawk`, "", "\x80", "", ""},
{`BEGIN { printf "%c", 255 } # !gawk`, "", "\xff", "", ""},
{`BEGIN { printf "%c", 256 } # !awk !gawk`, "", "\x00", "", ""},
{`BEGIN { printf "%c", 128 } # !windows-gawk`, "", "\u0080", "", ""},
{`BEGIN { printf "%c", 255 } # !windows-gawk`, "", "ÿ", "", ""},
{`BEGIN { printf "%c", 256 } # !windows-gawk`, "", "Ā", "", ""},
{`BEGIN { printf "%c", 4660 } # !windows-gawk`, "", "\u1234", "", ""},
{`BEGIN { printf "%c", "xyz" }`, "", "x", "", ""},
{`BEGIN { printf "%c %c %c", "Ā", "ĀĀĀ", "Āx" } # !windows-gawk`, "", "Ā Ā Ā", "", ""},
{`BEGIN { printf "%c", "" } # !awk`, "", "\x00", "", ""},
{`BEGIN { printf } # !awk !posix - doesn't error on this`, "", "", "parse error at 1:16: expected printf args, got none", "printf: no arguments"},
{`BEGIN { printf("%%%dd", 4) }`, "", "%4d", "", ""},
Expand Down
Loading