Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(365)

Side by Side Diff: src/pkg/bytes/bytes.go

Issue 1704044: code review 1704044: strings and bytes.Split: make count of 0 mean 0, not in... (Closed)
Patch Set: code review 1704044: strings and bytes.Split: make count of 0 mean 0, not in... Created 14 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/asn1/common.go ('k') | src/pkg/bytes/bytes_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // The bytes package implements functions for the manipulation of byte slices. 5 // The bytes package implements functions for the manipulation of byte slices.
6 // Analagous to the facilities of the strings package. 6 // Analagous to the facilities of the strings package.
7 package bytes 7 package bytes
8 8
9 import ( 9 import (
10 "unicode" 10 "unicode"
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 147 }
148 } 148 }
149 } 149 }
150 } 150 }
151 return -1 151 return -1
152 } 152 }
153 153
154 // Generic split: splits after each instance of sep, 154 // Generic split: splits after each instance of sep,
155 // including sepSave bytes of sep in the subarrays. 155 // including sepSave bytes of sep in the subarrays.
156 func genSplit(s, sep []byte, sepSave, n int) [][]byte { 156 func genSplit(s, sep []byte, sepSave, n int) [][]byte {
157 if n == 0 {
158 return nil
159 }
157 if len(sep) == 0 { 160 if len(sep) == 0 {
158 return explode(s, n) 161 return explode(s, n)
159 } 162 }
160 if n <= 0 { 163 if n <= 0 {
161 n = Count(s, sep) + 1 164 n = Count(s, sep) + 1
162 } 165 }
163 c := sep[0] 166 c := sep[0]
164 start := 0 167 start := 0
165 a := make([][]byte, n) 168 a := make([][]byte, n)
166 na := 0 169 na := 0
167 for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ { 170 for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
168 if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) { 171 if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) {
169 a[na] = s[start : i+sepSave] 172 a[na] = s[start : i+sepSave]
170 na++ 173 na++
171 start = i + len(sep) 174 start = i + len(sep)
172 i += len(sep) - 1 175 i += len(sep) - 1
173 } 176 }
174 } 177 }
175 a[na] = s[start:] 178 a[na] = s[start:]
176 return a[0 : na+1] 179 return a[0 : na+1]
177 } 180 }
178 181
179 // Split splits the array s around each instance of sep, returning an array of s ubarrays of s. 182 // Split splits the array s around each instance of sep, returning an array of s ubarrays of s.
180 // If sep is empty, Split splits s after each UTF-8 sequence. 183 // If sep is empty, Split splits s after each UTF-8 sequence.
181 // If n > 0, Split splits s into at most n subarrays; the last subarray will con tain an unsplit remainder. 184 // If n >= 0, Split splits s into at most n subarrays; the last subarray will co ntain an unsplit remainder.
185 // Thus if n == 0, the result will ne nil.
182 func Split(s, sep []byte, n int) [][]byte { return genSplit(s, sep, 0, n) } 186 func Split(s, sep []byte, n int) [][]byte { return genSplit(s, sep, 0, n) }
183 187
184 // SplitAfter splits the array s after each instance of sep, returning an array of subarrays of s. 188 // SplitAfter splits the array s after each instance of sep, returning an array of subarrays of s.
185 // If sep is empty, SplitAfter splits s after each UTF-8 sequence. 189 // If sep is empty, SplitAfter splits s after each UTF-8 sequence.
186 // If n > 0, SplitAfter splits s into at most n subarrays; the last subarray wil l contain an 190 // If n >= 0, SplitAfter splits s into at most n subarrays; the last subarray wi ll contain an
187 // unsplit remainder. 191 // unsplit remainder.
192 // Thus if n == 0, the result will ne nil.
188 func SplitAfter(s, sep []byte, n int) [][]byte { 193 func SplitAfter(s, sep []byte, n int) [][]byte {
189 return genSplit(s, sep, len(sep), n) 194 return genSplit(s, sep, len(sep), n)
190 } 195 }
191 196
192 // Fields splits the array s around each instance of one or more consecutive whi te space 197 // Fields splits the array s around each instance of one or more consecutive whi te space
193 // characters, returning a slice of subarrays of s or an empty list if s contain s only white space. 198 // characters, returning a slice of subarrays of s or an empty list if s contain s only white space.
194 func Fields(s []byte) [][]byte { 199 func Fields(s []byte) [][]byte {
195 n := 0 200 n := 0
196 inField := false 201 inField := false
197 for i := 0; i < len(s); { 202 for i := 0; i < len(s); {
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 r, l := utf8.DecodeRune(s) 463 r, l := utf8.DecodeRune(s)
459 t[i] = r 464 t[i] = r
460 i++ 465 i++
461 s = s[l:] 466 s = s[l:]
462 } 467 }
463 return t 468 return t
464 } 469 }
465 470
466 // Replace returns a copy of the slice s with the first n 471 // Replace returns a copy of the slice s with the first n
467 // non-overlapping instances of old replaced by new. 472 // non-overlapping instances of old replaced by new.
468 // If n <= 0, there is no limit on the number of replacements. 473 // If n < 0, there is no limit on the number of replacements.
469 func Replace(s, old, new []byte, n int) []byte { 474 func Replace(s, old, new []byte, n int) []byte {
475 if n == 0 {
476 return s // avoid allocation
477 }
470 // Compute number of replacements. 478 // Compute number of replacements.
471 if m := Count(s, old); m == 0 { 479 if m := Count(s, old); m == 0 {
472 return s // avoid allocation 480 return s // avoid allocation
473 } else if n <= 0 || m < n { 481 } else if n <= 0 || m < n {
474 n = m 482 n = m
475 } 483 }
476 484
477 // Apply replacements to buffer. 485 // Apply replacements to buffer.
478 t := make([]byte, len(s)+n*(len(new)-len(old))) 486 t := make([]byte, len(s)+n*(len(new)-len(old)))
479 w := 0 487 w := 0
480 start := 0 488 start := 0
481 for i := 0; i < n; i++ { 489 for i := 0; i < n; i++ {
482 j := start 490 j := start
483 if len(old) == 0 { 491 if len(old) == 0 {
484 if i > 0 { 492 if i > 0 {
485 _, wid := utf8.DecodeRune(s[start:]) 493 _, wid := utf8.DecodeRune(s[start:])
486 j += wid 494 j += wid
487 } 495 }
488 } else { 496 } else {
489 j += Index(s[start:], old) 497 j += Index(s[start:], old)
490 } 498 }
491 w += copy(t[w:], s[start:j]) 499 w += copy(t[w:], s[start:j])
492 w += copy(t[w:], new) 500 w += copy(t[w:], new)
493 start = j + len(old) 501 start = j + len(old)
494 } 502 }
495 w += copy(t[w:], s[start:]) 503 w += copy(t[w:], s[start:])
496 return t[0:w] 504 return t[0:w]
497 } 505 }
OLDNEW
« no previous file with comments | « src/pkg/asn1/common.go ('k') | src/pkg/bytes/bytes_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b