Skip to content

Commit

Permalink
improve direct type performance for string
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Oct 11, 2021
1 parent 8a90f0b commit 8059be3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
27 changes: 24 additions & 3 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ func String(v interface{}, def ...string) string {

// StringE convert an interface to a string type
func StringE(val interface{}) (string, error) {
v, _, rv := indirect(val)

// interface implements
switch vv := val.(type) {
case fmt.Stringer:
Expand All @@ -36,7 +34,30 @@ func StringE(val interface{}) (string, error) {
}
}

// source type
// direct type
switch vv := val.(type) {
case nil:
return "", nil
case bool:
return strconv.FormatBool(vv), nil
case string:
return vv, nil
case []byte:
return string(vv), nil
case []rune:
return string(vv), nil
case uint, uint8, uint16, uint32, uint64, uintptr:
return strconv.FormatUint(Uint64(vv), 10), nil
case int, int8, int16, int32, int64:
return strconv.FormatInt(Int64(vv), 10), nil
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(vv), 'f', -1, 32), nil
}

// indirect type
v, _, rv := indirect(val)
switch vv := v.(type) {
case nil:
return "", nil
Expand Down
24 changes: 23 additions & 1 deletion string_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cvt_test

import (
"encoding/json"
"errors"
"fmt"
"html/template"
Expand All @@ -19,6 +20,7 @@ func TestString_HasDefault(t *testing.T) {
expect string
}{
// supported value, def is not used, def != expect
{"hello", "world", "hello"},
{uint64(8), "xxx", "8"},
{float32(8.31), "xxx", "8.31"},
{float64(-8.31), "xxx", "-8.31"},
Expand Down Expand Up @@ -101,13 +103,17 @@ func TestStringE(t *testing.T) {
{int64(-8), "-8", false},
{float32(-8.31), "-8.31", false},
{float64(-8.31), "-8.31", false},
{"hello world!", "hello world!", false},
{[]byte("-8"), "-8", false},
{[]byte("-8.01"), "-8.01", false},
{[]byte("8"), "8", false},
{[]byte("8.00"), "8.00", false},
{[]byte("8.01"), "8.01", false},
{[]rune("我❤️中国"), "我❤️中国", false},
{nil, "", false},
{pointerInterNil, "", false},
{AliasTypeBytes_nil, "", false},
{&AliasTypeBytes_nil, "", false},
{aliasTypeInt_0, "0", false},
{&aliasTypeInt_0, "0", false},
{aliasTypeInt_1, "1", false},
Expand All @@ -124,12 +130,28 @@ func TestStringE(t *testing.T) {
{&aliasTypeBool_true, "true", false},
{aliasTypeBool_false, "false", false},
{&aliasTypeBool_false, "false", false},
{AliasTypeBytes("hello"), "hello", false},
{&pointerRunes, "中国", false},
{AliasTypeUint(12), "12", false},
{AliasTypeUint8(12), "12", false},
{AliasTypeUint16(12), "12", false},
{AliasTypeUint32(12), "12", false},
{AliasTypeUint64(12), "12", false},
{AliasTypeInt(-12), "-12", false},
{AliasTypeInt8(-12), "-12", false},
{AliasTypeInt16(-12), "-12", false},
{AliasTypeInt32(-12), "-12", false},
{AliasTypeInt64(-12), "-12", false},
{AliasTypeFloat32(-12.34), "-12.34", false},
{AliasTypeFloat64(12.34), "12.34", false},
{errors.New("errors"), "errors", false},
{time.Friday, "Friday", false},
{big.NewInt(123), "123", false},
{TestMarshalJSON{}, "MarshalJSON", false},
{&TestMarshalJSON{}, "MarshalJSON", false},
{template.URL("http://host.foo"), "http://host.foo", false},
{template.URL("https://host.foo"), "https://host.foo", false},
{template.HTML("<html></html>"), "<html></html>", false},
{json.Number("12.34"), "12.34", false},

// errors
{testing.T{}, "", true},
Expand Down

0 comments on commit 8059be3

Please sign in to comment.