Created
August 2, 2013 09:15
-
-
Save mash/6138557 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package strings | |
import ( | |
"fmt" | |
"unicode/utf8" | |
) | |
func MultibyteToEntityString(in string) string { | |
ret := "" | |
for _, c := range in { | |
if c < utf8.RuneSelf { | |
ret = ret + string(c) | |
} else { | |
ret = ret + fmt.Sprintf("&#x%x;", c) | |
} | |
} | |
return ret | |
} |
This file contains hidden or 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
package strings | |
import ( | |
"testing" | |
) | |
func TestConvert(t *testing.T) { | |
tests := []struct { | |
In string | |
Out string | |
}{ | |
{ | |
In: "a", | |
Out: "a", | |
}, { | |
In: "あい", | |
Out: "あい", | |
}, { | |
In: ">", | |
Out: ">", | |
}, | |
} | |
for _, test := range tests { | |
out := MultibyteToEntityString(test.In) | |
if out != test.Out { | |
t.Errorf("in: %s expected: %s but got: %s", test.In, test.Out, out) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment