Skip to content

Commit

Permalink
👔 up(fmt,math): update some util and convert func code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 15, 2023
1 parent 7f57ef7 commit 16f391d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
23 changes: 14 additions & 9 deletions fmtutil/format.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion mathutil/convert.go
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions stdio/ioutil.go
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 16f391d

Please sign in to comment.