Skip to content

Commit

Permalink
TrimSubstr
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkicoon committed Jul 25, 2024
1 parent 8792ee8 commit 99eeb9a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
70 changes: 58 additions & 12 deletions pkg/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"time"
"unicode"
)

// ConvertLineToCRLF converts a line ending of \n or \r to
Expand All @@ -31,24 +32,38 @@ func ConvertLineToCRLF(s string) string {
return res
}

type LDIFRegexps struct {
EmptyValue *regexp.Regexp
DoubleColon *regexp.Regexp
}

func NewLDIFRegexps() LDIFRegexps {
return LDIFRegexps{
DoubleColon: regexp.MustCompile("(?m)(^[^#][a-zA-Z]+:):"),
EmptyValue: regexp.MustCompile("(?m)^[^#][a-zA-Z]+:$[\n\r]"),
}
}

// DissolveEmptyValues removes all empty value lines in `s`.
// This is a work-around for https://github.com/go-ldap/ldif/issues/21.
func DissolveEmptyValues(s string) string {
start := time.Now()
re := regexp.MustCompile("(?m)^[^#][a-zA-Z]+:$[\n\r]")
res := re.ReplaceAllString(s, "")
fmt.Fprintf(os.Stderr, "execution time of %s: %s\n", GetCurrentFuncName(), time.Since(start).String())
return res
func (l LDIFRegexps) DissolveEmptyValues(s string, t ...bool) string {
if len(t) > 0 {
if t[1] {
defer TrackExecutionTime(time.Now())

Check failure on line 52 in pkg/string.go

View workflow job for this annotation

GitHub Actions / build

undefined: TrackExecutionTime

Check failure on line 52 in pkg/string.go

View workflow job for this annotation

GitHub Actions / lint

undefined: TrackExecutionTime

Check failure on line 52 in pkg/string.go

View workflow job for this annotation

GitHub Actions / lint

undefined: TrackExecutionTime

Check failure on line 52 in pkg/string.go

View workflow job for this annotation

GitHub Actions / build

undefined: TrackExecutionTime
}
}
return l.EmptyValue.ReplaceAllString(s, "")
}

// DissolveDoubleColon replaces all `t::` with `t:` in `s`.
// This is a work-around for https://github.com/go-ldap/ldif/issues/23.
func DissolveDoubleColon(s string) string {
start := time.Now()
re := regexp.MustCompile("(?m)(^[^#][a-zA-Z]+:):")
res := re.ReplaceAllString(s, "$1")
fmt.Fprintf(os.Stderr, "execution time of %s: %s\n", GetCurrentFuncName(), time.Since(start).String())
return res
func (l LDIFRegexps) DissolveDoubleColon(s string, t ...bool) string {
if len(t) > 0 {
if t[1] {
defer TrackExecutionTime(time.Now())

Check failure on line 63 in pkg/string.go

View workflow job for this annotation

GitHub Actions / build

undefined: TrackExecutionTime

Check failure on line 63 in pkg/string.go

View workflow job for this annotation

GitHub Actions / lint

undefined: TrackExecutionTime (typecheck)

Check failure on line 63 in pkg/string.go

View workflow job for this annotation

GitHub Actions / lint

undefined: TrackExecutionTime) (typecheck)

Check failure on line 63 in pkg/string.go

View workflow job for this annotation

GitHub Actions / build

undefined: TrackExecutionTime
}
}
return l.DoubleColon.ReplaceAllString(s, "$1")
}

func SplitMailString(s string) (string, string) {
Expand Down Expand Up @@ -83,3 +98,34 @@ func HasSuffixMultiple(s string, input []string) bool {
}
return res
}

func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
return false
}
}
return true
}

func IsLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}

func TrimSubstr(s string, substr string) string {
var res string
for {
res = strings.TrimPrefix(s, substr)
res = strings.TrimSuffix(res, substr)
if res == s { // exit if nothing was trimmed from s
break
}
s = res // update to last result
}
return res
}
11 changes: 11 additions & 0 deletions pkg/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg_test

import (
"github.com/nikkicoon/utility-go/pkg"
"github.com/stretchr/testify/assert"
"testing"
)

func TestTrimSubstr(t *testing.T) {
assert.Equal(t, "mushroom mushroom", pkg.TrimSubstr("badger badger badger mushroom mushroom", "badger "))
}

0 comments on commit 99eeb9a

Please sign in to comment.