-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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中", | ||
}) | ||
} |