From 43122d2c8aaf275ebd6683c110a41cb7e6e68593 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Fri, 16 Apr 2021 23:58:46 +0300 Subject: [PATCH] feat: add bin, oct, hex support --- int_fmt.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ int_fmt_test.go | 7 +++++++ module.go | 3 +++ testdata/fmt.star | 29 ++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 int_fmt.go create mode 100644 int_fmt_test.go create mode 100644 testdata/fmt.star diff --git a/int_fmt.go b/int_fmt.go new file mode 100644 index 0000000..da387c6 --- /dev/null +++ b/int_fmt.go @@ -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") +} diff --git a/int_fmt_test.go b/int_fmt_test.go new file mode 100644 index 0000000..079e25f --- /dev/null +++ b/int_fmt_test.go @@ -0,0 +1,7 @@ +package sun + +import "testing" + +func TestFmt(t *testing.T) { + runTestData(t, "fmt.star") +} diff --git a/module.go b/module.go index 8fa054d..f232c0b 100644 --- a/module.go +++ b/module.go @@ -13,5 +13,8 @@ var Module = &starlarkstruct.Module{ "next": starlark.NewBuiltin("next", next), "filter": starlark.NewBuiltin("filter", filter), "callable": starlark.NewBuiltin("callable", callable), + "hex": starlark.NewBuiltin("hex", hex), + "oct": starlark.NewBuiltin("oct", oct), + "bin": starlark.NewBuiltin("bin", bin), }, } diff --git a/testdata/fmt.star b/testdata/fmt.star new file mode 100644 index 0000000..68b8bbb --- /dev/null +++ b/testdata/fmt.star @@ -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() \ No newline at end of file