Skip to content

Commit

Permalink
Added guard in take/drop methods to ensure they exit when specifying …
Browse files Browse the repository at this point in the history
…N > string length
  • Loading branch information
daniel authored and daniel committed Aug 20, 2012
1 parent d526a8b commit f0c18d5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion strings_ext.go
Expand Up @@ -43,7 +43,7 @@ func Tail(s string) string {

//Take returns the n rune prefix of s or s itself if n > len([]rune(s))
func Take(n int, s string) string {
if n <= 0 || s == "" {
if n <= 0 || s == "" || n > len(s) {
return ""
}

Expand Down Expand Up @@ -73,6 +73,8 @@ func Take(n int, s string) string {
func Drop(n int, s string) string {
if n <= 0 || s == "" {
return s
} else if n > len(s) {
return ""
}

//TODO: Deal with rune encoding errors
Expand Down
2 changes: 1 addition & 1 deletion strings_ext_test.go
Expand Up @@ -133,7 +133,7 @@ func TestTakeWithOneChar(t *testing.T) {
func TestTakeWithMoreThanStringLength(t *testing.T) {
var input string = "test"
var expected string = "test"
var actual string = Take(20, input)
var actual string = Take(500, input)

assert.Equal(t, len(actual), len(expected))
assert.Equal(t, actual, expected)
Expand Down

0 comments on commit f0c18d5

Please sign in to comment.