@@ -7,55 +7,60 @@ import (
77
88const qualitySign = ";q="
99const defaultQuality = 1000
10- const maxQualityDigits = 3
10+ const maxQualityDecimals = 3
1111
1212type acceptItem struct {
1313 value string
1414 quality int
1515}
1616
1717func parseAcceptItem (input string ) acceptItem {
18- indexQSign := strings . Index ( input , qualitySign )
19- if indexQSign < 0 {
20- return acceptItem { input , defaultQuality }
18+ value := input
19+ if semiColonIndex := strings . IndexByte ( value , ';' ); semiColonIndex >= 0 {
20+ value = value [: semiColonIndex ]
2121 }
2222
23- value := input [:indexQSign ]
24- strQuality := input [indexQSign + len (qualitySign ):]
25- if semiColonIndex := strings .IndexByte (strQuality , ';' ); semiColonIndex >= 0 {
26- strQuality = strQuality [:semiColonIndex ]
23+ rest := input [len (value ):]
24+ qSignIndex := strings .Index (rest , qualitySign )
25+ if qSignIndex < 0 {
26+ return acceptItem {value , defaultQuality }
27+ }
28+
29+ rest = rest [qSignIndex + len (qualitySign ):]
30+ if semiColonIndex := strings .IndexByte (rest , ';' ); semiColonIndex >= 0 {
31+ rest = rest [:semiColonIndex ]
2732 }
28- strQualityLen := len (strQuality )
33+ qLen := len (rest )
2934
30- if strQualityLen == 0 {
35+ if qLen == 0 {
3136 return acceptItem {value , defaultQuality }
3237 }
33- if strQualityLen > 1 && strQuality [1 ] != '.' {
38+ if qLen > 1 && rest [1 ] != '.' {
3439 return acceptItem {value , defaultQuality }
3540 }
3641
3742 // "q=1" or q is an invalid value
38- if strQuality [0 ] != '0' {
43+ if rest [0 ] != '0' {
3944 return acceptItem {value , defaultQuality }
4045 }
4146
4247 // "q=0."
43- if strQualityLen <= 2 {
48+ if qLen <= 2 {
4449 return acceptItem {value , 0 }
4550 }
4651
47- strRest := strQuality [2 :]
48- strRestLen := len (strRest )
49- if strRestLen > maxQualityDigits {
50- strRestLen = maxQualityDigits
51- strRest = strRest [: strRestLen ]
52+ rest = rest [2 :]
53+ qDecimalLen := len (rest )
54+ if qDecimalLen > maxQualityDecimals {
55+ qDecimalLen = maxQualityDecimals
56+ rest = rest [: qDecimalLen ]
5257 }
5358
54- quality , err := strconv .Atoi (strRest )
59+ quality , err := strconv .Atoi (rest )
5560 if err != nil {
5661 quality = defaultQuality
5762 } else {
58- missingDigits := maxQualityDigits - strRestLen
63+ missingDigits := maxQualityDecimals - qDecimalLen
5964 for i := 0 ; i < missingDigits ; i ++ {
6065 quality *= 10
6166 }
0 commit comments