Skip to content

Commit

Permalink
Merge pull request #494 from projectdiscovery/improve_str_normalize_func
Browse files Browse the repository at this point in the history
improve str normalize func
  • Loading branch information
Mzack9999 authored Aug 15, 2024
2 parents 7b58775 + 7383e38 commit f6b40d9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion channelutil/clone_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCloneCounter(t *testing.T) {
cloner := channelutil.NewCloneChannels[struct{}](cloneOpts)
err := cloner.Clone(context.TODO(), source, sinks...)
if err != nil {
t.Fatalf(err.Error())
t.Fatalf("%s", err.Error())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions errkit/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Wrap(err error, message string) error {
}
x := &ErrorX{}
parseError(x, err)
x.Msgf(message)
x.Msgf("%s", message)
return x
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func WithMessage(err error, message string) error {
}
x := &ErrorX{}
parseError(x, err)
x.Msgf(message)
x.Msgf("%s", message)
return x
}

Expand Down
8 changes: 4 additions & 4 deletions errors/enriched.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func (e *enrichedError) Wrap(err ...error) Error {
continue
}
if ee, ok := v.(*enrichedError); ok {
_ = e.Msgf(ee.errString).WithLevel(ee.Level).WithTag(ee.Tags...)
_ = e.Msgf("%s", ee.errString).WithLevel(ee.Level).WithTag(ee.Tags...)
e.StackTrace += ee.StackTrace
} else {
_ = e.Msgf(v.Error())
_ = e.Msgf("%s", v.Error())
}
}
return e
Expand Down Expand Up @@ -131,10 +131,10 @@ func NewWithErr(err error) Error {
return nil
}
if ee, ok := err.(*enrichedError); ok {
x := New(ee.errString).WithTag(ee.Tags...).WithLevel(ee.Level)
x := New("%s", ee.errString).WithTag(ee.Tags...).WithLevel(ee.Level)
x.(*enrichedError).StackTrace = ee.StackTrace
}
return New(err.Error())
return New("%s", err.Error())
}

// NewWithTag creates an error with tag
Expand Down
6 changes: 3 additions & 3 deletions exec/executil.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func RunSafe(cmd ...string) (string, error) {

if err := cmdExec.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
adbError = errkit.Append(err, errkit.New(string(errorData)), errkit.New("exit error"))
adbError = errkit.Append(err, errkit.New("%s", string(errorData)), errkit.New("exit error"))
outData = errorData
} else {
return "", errkit.Wrap(err, "process i/o error")
Expand Down Expand Up @@ -215,7 +215,7 @@ func RunSh(cmd ...string) (string, error) {

if err := cmdExec.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
adbError = errkit.Append(err, errkit.New(string(errorData)), errkit.New("exit error"))
adbError = errkit.Append(err, errkit.New("%s", string(errorData)), errkit.New("exit error"))
outData = errorData
} else {
return "", errkit.Wrap(err, "process i/o error")
Expand Down Expand Up @@ -272,7 +272,7 @@ func RunPS(cmd string) (string, error) {

if err := cmdExec.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
adbError = errkit.Append(err, errkit.New(string(errorData)), errkit.New("exit error"))
adbError = errkit.Append(err, errkit.New("%s", string(errorData)), errkit.New("exit error"))
outData = errorData
} else {
return "", errkit.Wrap(err, "process i/o error")
Expand Down
21 changes: 17 additions & 4 deletions strings/strings_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package stringsutil

import (
"strings"
"unicode"

"github.com/microcosm-cc/bluemonday"
)

type NormalizeOptions struct {
TrimSpaces bool
StripHTML bool
Lowercase bool
Uppercase bool
TrimSpaces bool
TrimCutset string
StripHTML bool
Lowercase bool
Uppercase bool
StripComments bool
}

var DefaultNormalizeOptions NormalizeOptions = NormalizeOptions{
Expand All @@ -25,6 +28,10 @@ func NormalizeWithOptions(data string, options NormalizeOptions) string {
data = strings.TrimSpace(data)
}

if options.TrimCutset != "" {
data = strings.Trim(data, options.TrimCutset)
}

if options.Lowercase {
data = strings.ToLower(data)
}
Expand All @@ -37,6 +44,12 @@ func NormalizeWithOptions(data string, options NormalizeOptions) string {
data = HTMLPolicy.Sanitize(data)
}

if options.StripComments {
if cut := strings.IndexAny(data, "#"); cut >= 0 {
data = strings.TrimRightFunc(data[:cut], unicode.IsSpace)
}
}

return data
}

Expand Down
43 changes: 43 additions & 0 deletions strings/stringsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,46 @@ func TestContainsAllI(t *testing.T) {
require.Equal(t, test.result, res)
}
}

func TestNormalizeWithOptions(t *testing.T) {
tests := []struct {
data string
options NormalizeOptions
result string
}{
{
data: " Hello World! ",
options: NormalizeOptions{TrimSpaces: true},
result: "Hello World!",
},
{
data: "\n\t\"'` Hello World! \n\t\"'` ",
options: NormalizeOptions{TrimCutset: "\n\t\"'` "},
result: "Hello World!",
},
{
data: " Hello World! ",
options: NormalizeOptions{Lowercase: true},
result: " hello world! ",
},
{
data: " Hello World! ",
options: NormalizeOptions{Uppercase: true},
result: " HELLO WORLD! ",
},
{
data: "<b>Hello World!</b>",
options: NormalizeOptions{StripHTML: true},
result: "Hello World!",
},
{
data: "Hello World! # Comment",
options: NormalizeOptions{StripComments: true},
result: "Hello World!",
},
}
for _, test := range tests {
res := NormalizeWithOptions(test.data, test.options)
require.Equal(t, test.result, res)
}
}
4 changes: 2 additions & 2 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func GetToolVersionCallback(toolName, version string) func() (string, error) {
if toolDetails.Version == "" {
msg := fmt.Sprintf("something went wrong, expected version string but got empty string for GET `%v` response `%v`", updateURL, string(body))
if err == nil {
return "", errorutil.New(msg)
return "", errorutil.New("%s", msg)
}
return "", errorutil.NewWithErr(err).Msgf(msg)
return "", errorutil.NewWithErr(err).Msgf("%s", msg)
}
return toolDetails.Version, nil
}
Expand Down

0 comments on commit f6b40d9

Please sign in to comment.