Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: sdk/lib/io/http_parser.dart

Issue 209443005: Add optimized _OneByteString.toLowerCase. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Optimize toLowerCase. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 // Global constants. 7 // Global constants.
8 class _Const { 8 class _Const {
9 // Bytes for "HTTP". 9 // Bytes for "HTTP".
10 static const HTTP = const [72, 84, 84, 80]; 10 static const HTTP = const [72, 84, 84, 80];
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 } 540 }
541 _state = _State.HEADER_START; 541 _state = _State.HEADER_START;
542 break; 542 break;
543 543
544 case _State.HEADER_START: 544 case _State.HEADER_START:
545 _headers = new _HttpHeaders(version); 545 _headers = new _HttpHeaders(version);
546 if (byte == _CharCode.CR) { 546 if (byte == _CharCode.CR) {
547 _state = _State.HEADER_ENDING; 547 _state = _State.HEADER_ENDING;
548 } else { 548 } else {
549 // Start of new header field. 549 // Start of new header field.
550 _headerField.add(_toLowerCase(byte)); 550 _headerField.add(_ASCII.toLowerCaseByte(byte));
551 _state = _State.HEADER_FIELD; 551 _state = _State.HEADER_FIELD;
552 } 552 }
553 break; 553 break;
554 554
555 case _State.HEADER_FIELD: 555 case _State.HEADER_FIELD:
556 if (byte == _CharCode.COLON) { 556 if (byte == _CharCode.COLON) {
557 _state = _State.HEADER_VALUE_START; 557 _state = _State.HEADER_VALUE_START;
558 } else { 558 } else {
559 if (!_isTokenChar(byte)) { 559 if (!_isTokenChar(byte)) {
560 throw new HttpException("Invalid header field name"); 560 throw new HttpException("Invalid header field name");
561 } 561 }
562 _headerField.add(_toLowerCase(byte)); 562 _headerField.add(_ASCII.toLowerCaseByte(byte));
563 } 563 }
564 break; 564 break;
565 565
566 case _State.HEADER_VALUE_START: 566 case _State.HEADER_VALUE_START:
567 if (byte == _CharCode.CR) { 567 if (byte == _CharCode.CR) {
568 _state = _State.HEADER_VALUE_FOLDING_OR_ENDING; 568 _state = _State.HEADER_VALUE_FOLDING_OR_ENDING;
569 } else if (byte != _CharCode.SP && byte != _CharCode.HT) { 569 } else if (byte != _CharCode.SP && byte != _CharCode.HT) {
570 // Start of new header value. 570 // Start of new header value.
571 _headerValue.add(byte); 571 _headerValue.add(byte);
572 _state = _State.HEADER_VALUE; 572 _state = _State.HEADER_VALUE;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 } else { 608 } else {
609 _headers._add(headerField, headerValue); 609 _headers._add(headerField, headerValue);
610 } 610 }
611 _headerField.clear(); 611 _headerField.clear();
612 _headerValue.clear(); 612 _headerValue.clear();
613 613
614 if (byte == _CharCode.CR) { 614 if (byte == _CharCode.CR) {
615 _state = _State.HEADER_ENDING; 615 _state = _State.HEADER_ENDING;
616 } else { 616 } else {
617 // Start of new header field. 617 // Start of new header field.
618 _headerField.add(_toLowerCase(byte)); 618 _headerField.add(_ASCII.toLowerCaseByte(byte));
619 _state = _State.HEADER_FIELD; 619 _state = _State.HEADER_FIELD;
620 } 620 }
621 } 621 }
622 break; 622 break;
623 623
624 case _State.HEADER_ENDING: 624 case _State.HEADER_ENDING:
625 _expect(byte, _CharCode.LF); 625 _expect(byte, _CharCode.LF);
626 _headers._mutable = false; 626 _headers._mutable = false;
627 627
628 _transferLength = _headers.contentLength; 628 _transferLength = _headers.contentLength;
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 start = index + 1; 926 start = index + 1;
927 } else if (headerValue[index] == " " || headerValue[index] == "\t") { 927 } else if (headerValue[index] == " " || headerValue[index] == "\t") {
928 start++; 928 start++;
929 } 929 }
930 index++; 930 index++;
931 } 931 }
932 tokens.add(headerValue.substring(start, index)); 932 tokens.add(headerValue.substring(start, index));
933 return tokens; 933 return tokens;
934 } 934 }
935 935
936 int _toLowerCase(int byte) {
937 final int aCode = "A".codeUnitAt(0);
938 final int zCode = "Z".codeUnitAt(0);
939 final int delta = "a".codeUnitAt(0) - aCode;
940 return (aCode <= byte && byte <= zCode) ? byte + delta : byte;
941 }
942
943 // expected should already be lowercase. 936 // expected should already be lowercase.
944 bool _caseInsensitiveCompare(List<int> expected, List<int> value) { 937 bool _caseInsensitiveCompare(List<int> expected, List<int> value) {
945 if (expected.length != value.length) return false; 938 if (expected.length != value.length) return false;
946 for (int i = 0; i < expected.length; i++) { 939 for (int i = 0; i < expected.length; i++) {
947 if (expected[i] != _toLowerCase(value[i])) return false; 940 if (expected[i] != _ASCII.toLowerCaseByte(value[i])) return false;
948 } 941 }
949 return true; 942 return true;
950 } 943 }
951 944
952 int _expect(int val1, int val2) { 945 int _expect(int val1, int val2) {
953 if (val1 != val2) { 946 if (val1 != val2) {
954 throw new HttpException("Failed to parse HTTP"); 947 throw new HttpException("Failed to parse HTTP");
955 } 948 }
956 } 949 }
957 950
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 } 1025 }
1033 } 1026 }
1034 1027
1035 void _reportError(error, [stackTrace]) { 1028 void _reportError(error, [stackTrace]) {
1036 if (_socketSubscription != null) _socketSubscription.cancel(); 1029 if (_socketSubscription != null) _socketSubscription.cancel();
1037 _state = _State.FAILURE; 1030 _state = _State.FAILURE;
1038 _controller.addError(error, stackTrace); 1031 _controller.addError(error, stackTrace);
1039 _controller.close(); 1032 _controller.close();
1040 } 1033 }
1041 } 1034 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698