Skip to content

Commit

Permalink
Add tests for sprintf extra args; Slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Aug 21, 2018
1 parent ba1188f commit 2f0380f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 8 additions & 7 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,28 +1387,29 @@ func (p *Interp) sprintf(format string, args []value) string {
}
converted := make([]interface{}, len(types))
for i, t := range types {
a := args[i]
var v interface{}
switch t {
case 'd':
v = int(args[i].num())
v = int(a.num())
case 'u':
v = uint32(args[i].num())
v = uint32(a.num())
case 'c':
var c []byte
if args[i].isTrueStr() {
s := p.toString(args[i])
if a.isTrueStr() {
s := p.toString(a)
if len(s) > 0 {
c = []byte{s[0]}
}
} else {
r := []rune{rune(args[i].num())}
r := []rune{rune(a.num())}
c = []byte(string(r))
}
v = c
case 'f':
v = args[i].num()
v = a.num()
case 's':
v = p.toString(args[i])
v = p.toString(a)
}
converted[i] = v
}
Expand Down
4 changes: 4 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ NR==3, NR==5 { print NR }
{`BEGIN { printf "%3d", 42 }`, "", " 42", "", ""},
{`BEGIN { printf "%3s", "x" }`, "", " x", "", ""},
{`BEGIN { printf "%.1g", 42 }`, "", "4e+01", "", ""},
{`BEGIN { printf "%d", 12, 34 }`, "", "12", "", ""},
{`BEGIN { printf "%d" }`, "", "", "format error: got 0 args, expected 1", "not enough args in printf"},

// if and loop statements
{`BEGIN { if (1) print "t"; }`, "", "t\n", "", ""},
Expand Down Expand Up @@ -118,6 +120,8 @@ NR==3, NR==5 { print NR }
{`BEGIN { print index("foo", "f"), index("foo0", 0), index("foo", "o"), index("foo", "x") }`, "", "1 4 2 0\n", "", ""},
{`BEGIN { print atan2(1, 0.5), atan2(-1, 0) }`, "", "1.10715 -1.5708\n", "", ""},
{`BEGIN { print sprintf("%3d", 42) }`, "", " 42\n", "", ""},
{`BEGIN { print sprintf("%d", 12, 34) }`, "", "12\n", "", ""},
{`BEGIN { print sprintf("%d") }`, "", "", "format error: got 0 args, expected 1", "not enough args in printf"},
{`BEGIN { print substr("food", 1) }`, "", "food\n", "", ""},
{`BEGIN { print substr("food", 1, 2) }`, "", "fo\n", "", ""},
{`BEGIN { print substr("food", 1, 4) }`, "", "food\n", "", ""},
Expand Down

0 comments on commit 2f0380f

Please sign in to comment.