Skip to content

Commit

Permalink
Add extract alfanumeric with selected special characters (#4)
Browse files Browse the repository at this point in the history
* add extract alfanumeric with selected special characters

* fix comment
  • Loading branch information
necrophidia authored Jul 19, 2024
1 parent cbf769d commit 6b05c20
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions str/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ func ExtractAlfaNumericFromText(source string) string {
return result.String()
}

func ExtractAlfaNumericWithSelectedSpecialCharactersFromText(source string) string {
// allowed special characters are !"#$%&'()*+,-./ \[]\n
var result strings.Builder
for i := 0; i < len(source); i++ {
b := source[i]
if (32 <= b && b <= 57) ||
(65 <= b && b <= 93) ||
(97 <= b && b <= 122) ||
b == 10 {
result.WriteByte(b)
}
}
return result.String()
}

func SplitOnNotEmpty(str string, delimiter string) []string {
if strings.TrimSpace(str) != "" {
var result []string
Expand Down
13 changes: 13 additions & 0 deletions str/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package str

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestExtractAlfaNumericWithSelectedSpecialCharactersFromText(t *testing.T) {
sourceText := "Ab9 \\!@#$%^&*_+-=\n"
validateText := "Ab9 \\!#$%&*+-\n"
filteredText := ExtractAlfaNumericWithSelectedSpecialCharactersFromText(sourceText)
assert.Equal(t, validateText, filteredText)
}

0 comments on commit 6b05c20

Please sign in to comment.