Skip to content

Commit 0d6c6a0

Browse files
committed
Simplify stripNewlines() and add docs
1 parent 3a9ab89 commit 0d6c6a0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/encoding/base32/base32.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
373373
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
374374
buf := make([]byte, len(src))
375375
copy(buf, src)
376-
l := stripNewlines(buf, len(buf))
376+
l := stripNewlines(buf)
377377
n, _, err = enc.decode(dst, buf[:l])
378378
return
379379
}
@@ -383,7 +383,7 @@ func (enc *Encoding) DecodeString(s string) ([]byte, error) {
383383
dbuf := make([]byte, enc.DecodedLen(len(s)))
384384
src := make([]byte, len(s))
385385
copy(src, s)
386-
l := stripNewlines(src, len(src))
386+
l := stripNewlines(src)
387387
n, _, err := enc.decode(dbuf, src[:l])
388388
return dbuf[:n], err
389389
}
@@ -500,9 +500,11 @@ type newlineFilteringReader struct {
500500
wrapped io.Reader
501501
}
502502

503-
func stripNewlines(p []byte, n int) int {
503+
// stripNewlines removes newline characters and returns the number
504+
// of non-newline characters moved to the beginning of p.
505+
func stripNewlines(p []byte) int {
504506
offset := 0
505-
for i, b := range p[0:n] {
507+
for i, b := range p[0:len(p)] {
506508
if b != '\r' && b != '\n' {
507509
if i != offset {
508510
p[offset] = b
@@ -516,7 +518,7 @@ func stripNewlines(p []byte, n int) int {
516518
func (r *newlineFilteringReader) Read(p []byte) (int, error) {
517519
n, err := r.wrapped.Read(p)
518520
for n > 0 {
519-
offset := stripNewlines(p, n)
521+
offset := stripNewlines(p[:n])
520522
if err != nil || offset > 0 {
521523
return offset, err
522524
}

0 commit comments

Comments
 (0)