From 16f391d3ed72df989d225ff9a4fc7093ba3cc57f Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 15 Apr 2023 14:17:13 +0800 Subject: [PATCH] :necktie: up(fmt,math): update some util and convert func code logic --- fmtutil/format.go | 23 ++++++++++++++--------- mathutil/convert.go | 2 +- stdio/ioutil.go | 8 ++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/fmtutil/format.go b/fmtutil/format.go index 1f0b40d65..686d9859d 100644 --- a/fmtutil/format.go +++ b/fmtutil/format.go @@ -2,12 +2,13 @@ package fmtutil import ( "encoding/json" - "fmt" "strconv" "strings" "unicode" "github.com/gookit/goutil/basefn" + "github.com/gookit/goutil/byteutil" + "github.com/gookit/goutil/strutil" ) // data size @@ -95,15 +96,19 @@ func StringsToInts(ss []string) (ints []int, err error) { } // ArgsWithSpaces it like Println, will add spaces for each argument -func ArgsWithSpaces(args []any) (message string) { - if ln := len(args); ln == 0 { - message = "" +func ArgsWithSpaces(vs []any) (message string) { + if ln := len(vs); ln == 0 { + return "" } else if ln == 1 { - message = fmt.Sprint(args[0]) + return strutil.SafeString(vs[0]) } else { - message = fmt.Sprintln(args...) - // clear last "\n" - message = message[:len(message)-1] + bs := make([]byte, 0, ln*8) + for i := range vs { + if i > 0 { // add space + bs = append(bs, ' ') + } + bs = byteutil.AppendAny(bs, vs[i]) + } + return string(bs) } - return } diff --git a/mathutil/convert.go b/mathutil/convert.go index c8bf85519..2f0355658 100644 --- a/mathutil/convert.go +++ b/mathutil/convert.go @@ -389,7 +389,7 @@ func TryToString(val any, defaultAsErr bool) (str string, err error) { str = strconv.FormatFloat(value, 'f', -1, 64) case time.Duration: str = strconv.FormatUint(uint64(value.Nanoseconds()), 10) - case json.Number: + case fmt.Stringer: str = value.String() default: if defaultAsErr { diff --git a/stdio/ioutil.go b/stdio/ioutil.go index 13d160882..1bf65c0f4 100644 --- a/stdio/ioutil.go +++ b/stdio/ioutil.go @@ -7,8 +7,8 @@ import ( ) // QuietFprint to writer, will ignore error -func QuietFprint(w io.Writer, ss ...string) { - _, _ = fmt.Fprint(w, strings.Join(ss, "")) +func QuietFprint(w io.Writer, a ...any) { + _, _ = fmt.Fprint(w, a...) } // QuietFprintf to writer, will ignore error @@ -17,8 +17,8 @@ func QuietFprintf(w io.Writer, tpl string, vs ...any) { } // QuietFprintln to writer, will ignore error -func QuietFprintln(w io.Writer, ss ...string) { - _, _ = fmt.Fprintln(w, strings.Join(ss, "")) +func QuietFprintln(w io.Writer, a ...any) { + _, _ = fmt.Fprintln(w, a...) } // QuietWriteString to writer, will ignore error