1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:34:41 -07:00

string slicing is efficient so remove base and bounds arguments from RuneCountInString

R=rsc
DELTA=6  (1 added, 0 deleted, 5 changed)
OCL=28242
CL=28256
This commit is contained in:
Rob Pike 2009-05-04 22:12:13 -07:00
parent 567a7bf664
commit 2067b9fb92
3 changed files with 6 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import "utf8"
// Explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings). // Explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings).
// Invalid UTF-8 sequences become correct encodings of U+FFF8. // Invalid UTF-8 sequences become correct encodings of U+FFF8.
func Explode(s string) []string { func Explode(s string) []string {
a := make([]string, utf8.RuneCountInString(s, 0, len(s))); a := make([]string, utf8.RuneCountInString(s));
j := 0; j := 0;
var size, rune int; var size, rune int;
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
@ -24,7 +24,7 @@ func Explode(s string) []string {
// Count counts the number of non-overlapping instances of sep in s. // Count counts the number of non-overlapping instances of sep in s.
func Count(s, sep string) int { func Count(s, sep string) int {
if sep == "" { if sep == "" {
return utf8.RuneCountInString(s, 0, len(s))+1 return utf8.RuneCountInString(s)+1
} }
c := sep[0]; c := sep[0];
n := 0; n := 0;

View File

@ -273,8 +273,9 @@ func RuneCount(p []byte) int {
} }
// RuneCountInString is like RuneCount but its input is a string. // RuneCountInString is like RuneCount but its input is a string.
func RuneCountInString(s string, i int, l int) int { func RuneCountInString(s string) int {
ei := i + l; ei := len(s);
i := 0;
n := 0; n := 0;
for n = 0; i < ei; n++ { for n = 0; i < ei; n++ {
if s[i] < RuneSelf { if s[i] < RuneSelf {

View File

@ -169,7 +169,7 @@ var runecounttests = []RuneCountTest {
func TestRuneCount(t *testing.T) { func TestRuneCount(t *testing.T) {
for i := 0; i < len(runecounttests); i++ { for i := 0; i < len(runecounttests); i++ {
tt := runecounttests[i]; tt := runecounttests[i];
if out := utf8.RuneCountInString(tt.in, 0, len(tt.in)); out != tt.out { if out := utf8.RuneCountInString(tt.in); out != tt.out {
t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out); t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out);
} }
if out := utf8.RuneCount(bytes(tt.in)); out != tt.out { if out := utf8.RuneCount(bytes(tt.in)); out != tt.out {