Skip to content

Commit

Permalink
👔 up: str,byte - add and update some util func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 20, 2023
1 parent ecc23cb commit cf5d34e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
11 changes: 11 additions & 0 deletions byteutil/byteutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package byteutil

import "bytes"

// FirstLine from command output
func FirstLine(bs []byte) []byte {
if i := bytes.IndexByte(bs, '\n'); i >= 0 {
return bs[0:i]
}
return bs
}
10 changes: 4 additions & 6 deletions cliutil/cliutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)

Expand Down Expand Up @@ -134,9 +135,6 @@ func OutputLines(output string) []string {
}

// FirstLine from command output
func FirstLine(output string) string {
if i := strings.Index(output, "\n"); i >= 0 {
return output[0:i]
}
return output
}
//
// Deprecated: please use strutil.FirstLine
var FirstLine = strutil.FirstLine
14 changes: 11 additions & 3 deletions strutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ func QuietBool(s string) bool {

// MustBool convert, will panic on error
func MustBool(s string) bool {
val, _ := comfunc.StrToBool(strings.TrimSpace(s))
val, err := comfunc.StrToBool(strings.TrimSpace(s))
if err != nil {
panic(err)
}
return val
}

Expand All @@ -236,6 +239,12 @@ func ToInt(s string) (int, error) {
return strconv.Atoi(strings.TrimSpace(s))
}

// Int2 convert string to int, will ignore error
func Int2(s string) int {
val, _ := ToInt(s)
return val
}

// QuietInt convert string to int, will ignore error
func QuietInt(s string) int {
val, _ := ToInt(s)
Expand All @@ -244,8 +253,7 @@ func QuietInt(s string) int {

// MustInt convert string to int, will panic on error
func MustInt(s string) int {
val, _ := ToInt(s)
return val
return IntOrPanic(s)
}

// IntOrPanic convert value to int, will panic on error
Expand Down
3 changes: 3 additions & 0 deletions strutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func TestStrToInt(t *testing.T) {
iVal = strutil.QuietInt("-23")
is.Eq(-23, iVal)

iVal = strutil.Int2("-23")
is.Eq(-23, iVal)

iVal = strutil.IntOrPanic("-23")
is.Eq(-23, iVal)

Expand Down
8 changes: 8 additions & 0 deletions strutil/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ func SplitInlineComment(val string) (string, string) {
}
return val, ""
}

// FirstLine from command output
func FirstLine(output string) string {
if i := strings.IndexByte(output, '\n'); i >= 0 {
return output[0:i]
}
return output
}
36 changes: 36 additions & 0 deletions strutil/textscan/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package textscan_test

import (
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/strutil/textscan"
"github.com/gookit/goutil/testutil/assert"
)

func TestParser_ParseText(t *testing.T) {
p := textscan.NewParser(func(t textscan.Token) {
dump.P(t)
})

err := p.ParseText(`
# comments 1
# comments 1.1
# comments 1.2
name = inhere
// comments 2
age = 28
/*
multi line
comments 3
*/
desc = '''
a multi
line string
'''
`)
assert.NoErr(t, err)

}

0 comments on commit cf5d34e

Please sign in to comment.