Skip to content

Commit

Permalink
Merge pull request #26 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.6.3
  • Loading branch information
andyone committed Feb 8, 2016
2 parents 9c2e732 + d27850d commit 81750cc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

#### v1.6.3

* `[strutil]` Added methods PrefixSize and SuffixSize

#### v1.6.2

* `[fsutil]` Improved working with paths
Expand Down
38 changes: 38 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,41 @@ func Tail(s string, n int) string {

return s[l-n:]
}

// PrefixSize return prefix size
func PrefixSize(str string, prefix rune) int {
if str == "" {
return 0
}

var result int

for i := 0; i < len(str); i++ {
if rune(str[i]) != prefix {
return result
}

result++
}

return result
}

// SuffixSize return suffix size
func SuffixSize(str string, suffix rune) int {
if str == "" {
return 0
}

var result int

for i := len(str) - 1; i >= 0; i-- {
if rune(str[i]) != suffix {
return result
}

result++
}

return result
}
12 changes: 12 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ func (s *StrUtilSuite) TestTail(c *C) {
c.Assert(Tail("ABCD1234", 4), Equals, "1234")
c.Assert(Tail("ABCD1234", 100), Equals, "ABCD1234")
}

func (s *StrUtilSuite) TestSize(c *C) {
c.Assert(PrefixSize("", ' '), Equals, 0)
c.Assert(PrefixSize("abcd", ' '), Equals, 0)
c.Assert(PrefixSize(" abcd", ' '), Equals, 4)
c.Assert(PrefixSize(" ", ' '), Equals, 4)

c.Assert(SuffixSize("", ' '), Equals, 0)
c.Assert(SuffixSize("abcd", ' '), Equals, 0)
c.Assert(SuffixSize("abcd ", ' '), Equals, 4)
c.Assert(SuffixSize(" ", ' '), Equals, 4)
}

0 comments on commit 81750cc

Please sign in to comment.