-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package sun | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
func formatInt( | ||
thread *starlark.Thread, | ||
b *starlark.Builtin, | ||
args starlark.Tuple, | ||
kwargs []starlark.Tuple, | ||
f string, | ||
) (starlark.Value, error) { | ||
var i starlark.Int | ||
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 1, &i); err != nil { | ||
return nil, err | ||
} | ||
|
||
if v, ok := i.Int64(); ok { | ||
return starlark.String(fmt.Sprintf(f, v)), nil | ||
} | ||
|
||
return starlark.String(fmt.Sprintf(f, i.BigInt())), nil | ||
} | ||
|
||
func bin( | ||
thread *starlark.Thread, | ||
b *starlark.Builtin, | ||
args starlark.Tuple, | ||
kwargs []starlark.Tuple, | ||
) (starlark.Value, error) { | ||
return formatInt(thread, b, args, kwargs, "%#b") | ||
} | ||
|
||
func oct( | ||
thread *starlark.Thread, | ||
b *starlark.Builtin, | ||
args starlark.Tuple, | ||
kwargs []starlark.Tuple, | ||
) (starlark.Value, error) { | ||
return formatInt(thread, b, args, kwargs, "%O") | ||
} | ||
|
||
func hex( | ||
thread *starlark.Thread, | ||
b *starlark.Builtin, | ||
args starlark.Tuple, | ||
kwargs []starlark.Tuple, | ||
) (starlark.Value, error) { | ||
return formatInt(thread, b, args, kwargs, "%#x") | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package sun | ||
|
||
import "testing" | ||
|
||
func TestFmt(t *testing.T) { | ||
runTestData(t, "fmt.star") | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
load("assert.star", "assert") | ||
|
||
def test_main(): | ||
assert.fails(lambda: hex([]), "want int") | ||
assert.fails(lambda: oct([]), "want int") | ||
assert.fails(lambda: bin([]), "want int") | ||
|
||
tests = [(-3, '-0x3'), (-2, '-0x2'), (-1, '-0x1'), (0, '0x0'), (1, '0x1'), (2, '0x2')] | ||
for test in tests: | ||
assert.eq( | ||
hex(test[0]), | ||
test[1], | ||
) | ||
|
||
tests = [(-3, '-0o3'), (-2, '-0o2'), (-1, '-0o1'), (0, '0o0'), (1, '0o1'), (2, '0o2')] | ||
for test in tests: | ||
assert.eq( | ||
oct(test[0]), | ||
test[1], | ||
) | ||
|
||
tests = [(-3, '-0b11'), (-2, '-0b10'), (-1, '-0b1'), (0, '0b0'), (1, '0b1'), (2, '0b10')] | ||
for test in tests: | ||
assert.eq( | ||
bin(test[0]), | ||
test[1], | ||
) | ||
|
||
test_main() |