Skip to content

Commit

Permalink
refs #7. reverse string.
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Jan 8, 2015
1 parent 3383fdd commit b57a8ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions manipulate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

package xstrings

import (
"unicode/utf8"
)

// Reverse a utf8 encoded string.
func Reverse(str string) string {
var size int

tail := len(str)
buf := make([]byte, tail)
s := buf

for len(str) > 0 {
_, size = utf8.DecodeRuneInString(str)
tail -= size
s = append(s[:tail], []byte(str[:size])...)
str = str[size:]
}

return string(buf)
}
16 changes: 16 additions & 0 deletions manipulate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

package xstrings

import (
"testing"
)

func TestReverse(t *testing.T) {
runTestCases(t, Reverse, map[string]string{
"reverse string": "gnirts esrever",
"中文如何?": "?何如文中",
"中en文混~排怎样?a": "a?样怎排~混文ne中",
})
}

0 comments on commit b57a8ab

Please sign in to comment.