diff --git a/base32.go b/base32.go index 44e478a..fe833ee 100644 --- a/base32.go +++ b/base32.go @@ -8,6 +8,7 @@ package base32 import ( "io" "strconv" + "strings" ) /* @@ -382,6 +383,20 @@ func (enc *Encoding) DecodeString(s string) ([]byte, error) { return strb[:n], nil } +var removeNewlinesMapper = func(r rune) rune { + if r == '\r' || r == '\n' { + return -1 + } + return r +} + +func (enc *Encoding) DecodeStringOrig(s string) ([]byte, error) { + s = strings.Map(removeNewlinesMapper, s) + dbuf := make([]byte, enc.DecodedLen(len(s))) + n, _, err := enc.decode(dbuf, []byte(s)) + return dbuf[:n], err +} + // DecodeString returns the bytes represented by the base32 string s. func (enc *Encoding) DecodeStringKevOrig(s string) ([]byte, error) { stripped := make([]byte, 0, len(s)) diff --git a/base32_test.go b/base32_test.go index 39f3801..ef31ade 100644 --- a/base32_test.go +++ b/base32_test.go @@ -356,9 +356,19 @@ func TestNoPaddingRand(t *testing.T) { var keyLen = 55 var sampleKey = RandomBase32String(keyLen) -var sampleKeyBytes = []byte(sampleKey) +//var sampleKeyBytes = []byte(sampleKey) //var sampleKey = "BP\rH3BGZMIEGPCRY2LVF\nCP2IS7LPYWXVY3PFMJ\rX5SNSKDRT6E7ZVJVXI" +func BenchmarkKeyDecodeOrig(b *testing.B) { + key := sampleKey + for i := 0; i < b.N; i++ { + _,err := RawStdEncoding.DecodeStringOrig(key) + if err != nil { + panic(err) + } + } +} + func BenchmarkKeyDecodeKevOrig(b *testing.B) { key := sampleKey for i := 0; i < b.N; i++ {