Skip to content

Commit

Permalink
👔 up(str): update the SplitInlineComment() logic
Browse files Browse the repository at this point in the history
- fix: textscan parse url as comments error
  • Loading branch information
inhere committed Feb 6, 2023
1 parent 9eaddb5 commit 31445cd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 24 deletions.
10 changes: 5 additions & 5 deletions strutil/convert.go
Expand Up @@ -92,6 +92,11 @@ func String(val any) (string, error) {
return AnyToString(val, true)
}

// ToString convert value to string
func ToString(val any) (string, error) {
return AnyToString(val, true)
}

// QuietString convert value to string, will ignore error
func QuietString(in any) string {
val, _ := AnyToString(in, false)
Expand All @@ -109,11 +114,6 @@ func StringOrErr(val any) (string, error) {
return AnyToString(val, true)
}

// ToString convert value to string
func ToString(val any) (string, error) {
return AnyToString(val, true)
}

// AnyToString convert value to string.
//
// if defaultAsErr:
Expand Down
26 changes: 19 additions & 7 deletions strutil/split.go
Expand Up @@ -123,15 +123,27 @@ func Substr(s string, pos, length int) string {
return string(runes[pos:stopIdx])
}

// SplitInlineComment for a text string.
func SplitInlineComment(val string) (string, string) {
if pos := strings.IndexByte(val, '#'); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos:]
}
// SplitInlineComment for an inline text string.
func SplitInlineComment(val string, strict ...bool) (string, string) {
// strict check: must with space
if len(strict) > 0 && strict[0] {
if pos := strings.Index(val, " #"); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos+1:]
}

if pos := strings.Index(val, "//"); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos:]
if pos := strings.Index(val, " //"); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos+1:]
}
} else {
if pos := strings.IndexByte(val, '#'); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos:]
}

if pos := strings.Index(val, "//"); pos > -1 {
return strings.TrimRight(val[0:pos], " "), val[pos:]
}
}

return val, ""
}

Expand Down
76 changes: 65 additions & 11 deletions strutil/split_test.go
Expand Up @@ -86,15 +86,69 @@ func TestSubstr(t *testing.T) {
}

func TestSplitInlineComment(t *testing.T) {
val, comment := strutil.SplitInlineComment("value0")
assert.Eq(t, "value0", val)
assert.Eq(t, "", comment)

val, comment = strutil.SplitInlineComment("value0 // comments at end")
assert.Eq(t, "value0", val)
assert.Eq(t, "// comments at end", comment)

val, comment = strutil.SplitInlineComment("value0 # comments at end")
assert.Eq(t, "value0", val)
assert.Eq(t, "# comments at end", comment)
tests := []struct {
str string
val string
cmt string
strict bool
}{
{
str: "value",
val: "value",
},
{
str: "value // comments at end",
val: "value",
cmt: "// comments at end",
},
{
str: "value // comments at end",
val: "value",
cmt: "// comments at end",
strict: true,
},
{
str: "value// comments at end",
val: "value",
cmt: "// comments at end",
},
{
str: "value// comments at end",
val: "value// comments at end",
strict: true,
},
{
str: "value # comments at end",
val: "value",
cmt: "# comments at end",
},
{
str: "value # comments at end",
val: "value",
cmt: "# comments at end",
strict: true,
},
{
str: "value# comments at end",
val: "value",
cmt: "# comments at end",
},
{
str: "value# comments at end",
val: "value# comments at end",
strict: true,
},
}

for i, tt := range tests {
idx := strutil.QuietString(i)
val, comment := strutil.SplitInlineComment(tt.str+idx, tt.strict)
if comment == "" {
assert.Eq(t, tt.val+idx, val)
assert.Eq(t, tt.cmt, comment)
} else {
assert.Eq(t, tt.val, val)
assert.Eq(t, tt.cmt+idx, comment)
}
}
}
2 changes: 1 addition & 1 deletion strutil/textscan/kvparse.go
Expand Up @@ -127,7 +127,7 @@ func (m *KeyValueMatcher) inlineCommentsAndUnquote(vt *ValueToken, val string) s
if m.InlineComment {
// split inline comments
var comment string
val, comment = strutil.SplitInlineComment(val)
val, comment = strutil.SplitInlineComment(val, true)

if len(comment) > 0 {
cmt := NewCommentToken(comment)
Expand Down

0 comments on commit 31445cd

Please sign in to comment.