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

Side by Side Diff: lib/compiler/implementation/string_validator.dart

Issue 11233035: Use == instead of identical for integers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix typos. Created 8 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // Check the validity of string literals. 5 // Check the validity of string literals.
6 6
7 library stringvalidator; 7 library stringvalidator;
8 8
9 import "dart2jslib.dart"; 9 import "dart2jslib.dart";
10 import "tree/tree.dart"; 10 import "tree/tree.dart";
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 static StringQuoting quotingFromString(SourceString sourceString) { 47 static StringQuoting quotingFromString(SourceString sourceString) {
48 Iterator<int> source = sourceString.iterator(); 48 Iterator<int> source = sourceString.iterator();
49 bool raw = false; 49 bool raw = false;
50 int quoteLength = 1; 50 int quoteLength = 1;
51 int quoteChar = source.next(); 51 int quoteChar = source.next();
52 if (quoteChar === $r) { 52 if (quoteChar === $r) {
53 raw = true; 53 raw = true;
54 quoteChar = source.next(); 54 quoteChar = source.next();
55 } 55 }
56 assert(identical(quoteChar, $SQ) || identical(quoteChar, $DQ)); 56 assert(quoteChar == $SQ || quoteChar == $DQ);
57 // String has at least one quote. Check it if has three. 57 // String has at least one quote. Check it if has three.
58 // If it only have two, the string must be an empty string literal, 58 // If it only have two, the string must be an empty string literal,
59 // and end after the second quote. 59 // and end after the second quote.
60 bool multiline = false; 60 bool multiline = false;
61 if (source.hasNext() && identical(source.next(), quoteChar) && source.hasNex t()) { 61 if (source.hasNext() && source.next() == quoteChar && source.hasNext()) {
62 int code = source.next(); 62 int code = source.next();
63 assert(identical(code, quoteChar)); // If not, there is a bug in the pars er. 63 assert(code == quoteChar); // If not, there is a bug in the parser.
64 quoteLength = 3; 64 quoteLength = 3;
65 // Check if a multiline string starts with a newline (CR, LF or CR+LF). 65 // Check if a multiline string starts with a newline (CR, LF or CR+LF).
66 if (source.hasNext()) { 66 if (source.hasNext()) {
67 code = source.next(); 67 code = source.next();
68 if (identical(code, $CR)) { 68 if (code == $CR) {
69 quoteLength += 1; 69 quoteLength += 1;
70 if (source.hasNext() && identical(source.next(), $LF)) { 70 if (source.hasNext() && source.next() == $LF) {
71 quoteLength += 1; 71 quoteLength += 1;
72 } 72 }
73 } else if (identical(code, $LF)) { 73 } else if (code == $LF) {
74 quoteLength += 1; 74 quoteLength += 1;
75 } 75 }
76 } 76 }
77 } 77 }
78 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); 78 return StringQuoting.getQuoting(quoteChar, raw, quoteLength);
79 } 79 }
80 80
81 void stringParseError(String message, Token token, int offset) { 81 void stringParseError(String message, Token token, int offset) {
82 listener.cancel("$message @ $offset", token : token); 82 listener.cancel("$message @ $offset", token : token);
83 } 83 }
84 84
85 /** 85 /**
86 * Validates the escape sequences and special characters of a string literal. 86 * Validates the escape sequences and special characters of a string literal.
87 * Returns a DartString if valid, and null if not. 87 * Returns a DartString if valid, and null if not.
88 */ 88 */
89 DartString validateString(Token token, 89 DartString validateString(Token token,
90 int startOffset, 90 int startOffset,
91 SourceString string, 91 SourceString string,
92 StringQuoting quoting) { 92 StringQuoting quoting) {
93 // We only need to check for invalid x and u escapes, for line 93 // We only need to check for invalid x and u escapes, for line
94 // terminators in non-multiline strings, and for invalid Unicode 94 // terminators in non-multiline strings, and for invalid Unicode
95 // scalar values (either directly or as u-escape values). 95 // scalar values (either directly or as u-escape values).
96 int length = 0; 96 int length = 0;
97 int index = startOffset; 97 int index = startOffset;
98 bool containsEscape = false; 98 bool containsEscape = false;
99 for(Iterator<int> iter = string.iterator(); iter.hasNext(); length++) { 99 for(Iterator<int> iter = string.iterator(); iter.hasNext(); length++) {
100 index++; 100 index++;
101 int code = iter.next(); 101 int code = iter.next();
102 if (identical(code, $BACKSLASH)) { 102 if (code == $BACKSLASH) {
103 if (quoting.raw) continue; 103 if (quoting.raw) continue;
104 containsEscape = true; 104 containsEscape = true;
105 if (!iter.hasNext()) { 105 if (!iter.hasNext()) {
106 stringParseError("Incomplete escape sequence",token, index); 106 stringParseError("Incomplete escape sequence",token, index);
107 return null; 107 return null;
108 } 108 }
109 index++; 109 index++;
110 code = iter.next(); 110 code = iter.next();
111 if (identical(code, $x)) { 111 if (code == $x) {
112 for (int i = 0; i < 2; i++) { 112 for (int i = 0; i < 2; i++) {
113 if (!iter.hasNext()) { 113 if (!iter.hasNext()) {
114 stringParseError("Incomplete escape sequence", token, index); 114 stringParseError("Incomplete escape sequence", token, index);
115 return null; 115 return null;
116 } 116 }
117 index++; 117 index++;
118 code = iter.next(); 118 code = iter.next();
119 if (!isHexDigit(code)) { 119 if (!isHexDigit(code)) {
120 stringParseError("Invalid character in escape sequence", 120 stringParseError("Invalid character in escape sequence",
121 token, index); 121 token, index);
122 return null; 122 return null;
123 } 123 }
124 } 124 }
125 // A two-byte hex escape can't generate an invalid value. 125 // A two-byte hex escape can't generate an invalid value.
126 continue; 126 continue;
127 } else if (identical(code, $u)) { 127 } else if (code == $u) {
128 int escapeStart = index - 1; 128 int escapeStart = index - 1;
129 index++; 129 index++;
130 code = iter.next(); 130 code = iter.next();
131 int value = 0; 131 int value = 0;
132 if (code == $OPEN_CURLY_BRACKET) { 132 if (code == $OPEN_CURLY_BRACKET) {
133 // expect 1-6 hex digits. 133 // expect 1-6 hex digits.
134 int count = 0; 134 int count = 0;
135 index++; 135 index++;
136 code = iter.next(); 136 code = iter.next();
137 do { 137 do {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 } 178 }
179 } 179 }
180 // String literal successfully validated. 180 // String literal successfully validated.
181 if (quoting.raw || !containsEscape) { 181 if (quoting.raw || !containsEscape) {
182 // A string without escapes could just as well have been raw. 182 // A string without escapes could just as well have been raw.
183 return new DartString.rawString(string, length); 183 return new DartString.rawString(string, length);
184 } 184 }
185 return new DartString.escapedString(string, length); 185 return new DartString.escapedString(string, length);
186 } 186 }
187 } 187 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698