diff --git a/aws/awsutil/strings.go b/aws/awsutil/strings.go deleted file mode 100644 index 93bb091b502..00000000000 --- a/aws/awsutil/strings.go +++ /dev/null @@ -1,67 +0,0 @@ -package awsutil - -import ( - "unicode" - "unicode/utf8" -) - -// StringHasPrefixFold tests whether the string s begins with prefix, interpreted as UTF-8 strings, -// under Unicode case-folding. -// -// This implementation is a modified version of the Go Standard Library's strings.EqualFold -// Copyright 2009 The Go Authors. All rights reserved. -func StringHasPrefixFold(s, prefix string) bool { - for s != "" && prefix != "" { - // Extract first rune from each string. - var sr, tr rune - if s[0] < utf8.RuneSelf { - sr, s = rune(s[0]), s[1:] - } else { - r, size := utf8.DecodeRuneInString(s) - sr, s = r, s[size:] - } - if prefix[0] < utf8.RuneSelf { - tr, prefix = rune(prefix[0]), prefix[1:] - } else { - r, size := utf8.DecodeRuneInString(prefix) - tr, prefix = r, prefix[size:] - } - - // If they match, keep going; if not, return false. - - // Easy case. - if tr == sr { - continue - } - - // Make sr < tr to simplify what follows. - if tr < sr { - tr, sr = sr, tr - } - // Fast check for ASCII. - if tr < utf8.RuneSelf { - // ASCII only, sr/tr must be upper/lower case - if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' { - continue - } - return false - } - - // General case. SimpleFold(x) returns the next equivalent rune > x - // or wraps around to smaller values. - r := unicode.SimpleFold(sr) - for r != sr && r < tr { - r = unicode.SimpleFold(r) - } - if r == tr { - continue - } - return false - } - - if len(s) == 0 && len(prefix) > 0 { - return false - } - - return true -} diff --git a/aws/signer/v4/header_rules.go b/aws/signer/v4/header_rules.go index 11ef095a428..07ea799fbd3 100644 --- a/aws/signer/v4/header_rules.go +++ b/aws/signer/v4/header_rules.go @@ -1,7 +1,7 @@ package v4 import ( - "github.com/aws/aws-sdk-go/aws/awsutil" + "github.com/aws/aws-sdk-go/internal/strings" ) // validator houses a set of rule needed for validation of a @@ -60,7 +60,7 @@ type patterns []string // been found func (p patterns) IsValid(value string) bool { for _, pattern := range p { - if awsutil.StringHasPrefixFold(value, pattern) { + if strings.HasPrefixFold(value, pattern) { return true } } diff --git a/internal/strings/strings.go b/internal/strings/strings.go new file mode 100644 index 00000000000..d008ae27cb3 --- /dev/null +++ b/internal/strings/strings.go @@ -0,0 +1,11 @@ +package strings + +import ( + "strings" +) + +// HasPrefixFold tests whether the string s begins with prefix, interpreted as UTF-8 strings, +// under Unicode case-folding. +func HasPrefixFold(s, prefix string) bool { + return len(s) >= len(prefix) && strings.EqualFold(s[0:len(prefix)], prefix) +} diff --git a/aws/awsutil/strings_test.go b/internal/strings/strings_test.go similarity index 73% rename from aws/awsutil/strings_test.go rename to internal/strings/strings_test.go index 35de34fefee..34d1769487f 100644 --- a/aws/awsutil/strings_test.go +++ b/internal/strings/strings_test.go @@ -1,13 +1,13 @@ // +build go1.7 -package awsutil +package strings import ( "strings" "testing" ) -func TestStringPrefixFold(t *testing.T) { +func TestHasPrefixFold(t *testing.T) { type args struct { s string prefix string @@ -57,18 +57,25 @@ func TestStringPrefixFold(t *testing.T) { }, want: true, }, + "mixed-case prefixes": { + args: args{ + s: "SoMe String", + prefix: "sOme", + }, + want: true, + }, } for name, tt := range tests { t.Run(name, func(t *testing.T) { - if got := StringHasPrefixFold(tt.args.s, tt.args.prefix); got != tt.want { - t.Errorf("StringHasPrefixFold() = %v, want %v", got, tt.want) + if got := HasPrefixFold(tt.args.s, tt.args.prefix); got != tt.want { + t.Errorf("HasPrefixFold() = %v, want %v", got, tt.want) } }) } } -func BenchmarkStringHasPrefixFold(b *testing.B) { - StringHasPrefixFold("SoME string", "sOmE") +func BenchmarkHasPrefixFold(b *testing.B) { + HasPrefixFold("SoME string", "sOmE") } func BenchmarkHasPrefix(b *testing.B) { diff --git a/private/protocol/rest/rest_test.go b/private/protocol/rest/rest_test.go index 5902bcc50d8..58ed85abf0b 100644 --- a/private/protocol/rest/rest_test.go +++ b/private/protocol/rest/rest_test.go @@ -1,3 +1,5 @@ +// +build go1.7 + package rest_test import ( diff --git a/private/protocol/rest/unmarshal.go b/private/protocol/rest/unmarshal.go index e947ecc68f4..f5f4530aba4 100644 --- a/private/protocol/rest/unmarshal.go +++ b/private/protocol/rest/unmarshal.go @@ -14,8 +14,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/aws/aws-sdk-go/aws/request" + awsStrings "github.com/aws/aws-sdk-go/internal/strings" "github.com/aws/aws-sdk-go/private/protocol" ) @@ -154,7 +154,7 @@ func unmarshalHeaderMap(r reflect.Value, headers http.Header, prefix string, nor case map[string]*string: // we only support string map value types out := map[string]*string{} for k, v := range headers { - if awsutil.StringHasPrefixFold(k, prefix) { + if awsStrings.HasPrefixFold(k, prefix) { if normalize == true { k = strings.ToLower(k) } else {