Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 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 package textproto | 5 package textproto |
6 | 6 |
7 import ( | 7 import ( |
8 "bufio" | 8 "bufio" |
9 "bytes" | 9 "bytes" |
10 "io" | 10 "io" |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 } | 499 } |
500 upper = c == '-' | 500 upper = c == '-' |
501 } | 501 } |
502 return s | 502 return s |
503 } | 503 } |
504 | 504 |
505 // canonicalMIMEHeaderKey is like CanonicalMIMEHeaderKey but is | 505 // canonicalMIMEHeaderKey is like CanonicalMIMEHeaderKey but is |
506 // allowed to mutate the provided byte slice before returning the | 506 // allowed to mutate the provided byte slice before returning the |
507 // string. | 507 // string. |
508 func canonicalMIMEHeaderKey(a []byte) string { | 508 func canonicalMIMEHeaderKey(a []byte) string { |
509 // Look in the headerTrie before doing proper canonicalization. | |
510 // For common headers, this lets us avoid an allocation and copy. | |
511 if false { | |
dfc
2012/10/30 16:28:15
I think this is an accident. Was it part of your b
jeff.allen
2012/10/31 14:27:33
Fixed. As for gofmt, no idea. hg gofmt said it pro
| |
512 if h, ok := searchHeaderTrie(a); ok { | |
bradfitz
2012/10/30 16:09:27
this could be used for common header values too ("
jeff.allen
2012/10/31 14:27:33
Let's benchmark that separately once this is check
| |
513 return h | |
514 } | |
515 } | |
516 | |
509 // Canonicalize: first letter upper case | 517 // Canonicalize: first letter upper case |
510 // and upper case after each dash. | 518 // and upper case after each dash. |
511 // (Host, User-Agent, If-Modified-Since). | 519 // (Host, User-Agent, If-Modified-Since). |
512 // MIME headers are ASCII only, so no Unicode issues. | 520 // MIME headers are ASCII only, so no Unicode issues. |
513 upper := true | 521 upper := true |
514 for i, v := range a { | 522 for i, v := range a { |
515 if v == ' ' { | 523 if v == ' ' { |
516 a[i] = '-' | 524 a[i] = '-' |
517 upper = true | 525 upper = true |
518 continue | 526 continue |
519 } | 527 } |
520 if upper && 'a' <= v && v <= 'z' { | 528 if upper && 'a' <= v && v <= 'z' { |
521 a[i] = v + 'A' - 'a' | 529 a[i] = v + 'A' - 'a' |
522 } | 530 } |
523 if !upper && 'A' <= v && v <= 'Z' { | 531 if !upper && 'A' <= v && v <= 'Z' { |
524 a[i] = v + 'a' - 'A' | 532 a[i] = v + 'a' - 'A' |
525 } | 533 } |
526 upper = v == '-' | 534 upper = v == '-' |
527 } | 535 } |
528 return string(a) | 536 return string(a) |
529 } | 537 } |
538 | |
539 const toLower = ('a' - 'A') | |
540 | |
541 func byteToKey(b byte) (int, bool) { | |
bradfitz
2012/10/30 16:09:27
does this get inlined?
jeff.allen
2012/10/31 14:27:33
-m says: ./reader.go:555: inlining call to byteToK
| |
542 if b == '-' { | |
543 return 26, true | |
544 } | |
545 if b > 'Z' { | |
546 b -= toLower | |
547 } | |
548 if b < 'A' || b > 'Z' { | |
549 return 0, false | |
550 } | |
551 return int(b - 'A'), true | |
552 } | |
553 | |
554 func searchHeaderTrie(header []byte) (string, bool) { | |
555 cur := uint16(0) | |
556 for _, b := range header { | |
557 k, ok := byteToKey(b) | |
558 if !ok || headerTrie[cur].keys[k] == 0 { | |
bradfitz
2012/10/30 16:09:27
eliminate the duplicate expression two lines below
jeff.allen
2012/10/31 14:27:33
Done.
| |
559 return "", false | |
560 } | |
561 cur = headerTrie[cur].keys[k] | |
562 } | |
563 return headerTrie[cur].str, true | |
564 } | |
OLD | NEW |