OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 wordCallback(text.substring(startWord, i)); | 114 wordCallback(text.substring(startWord, i)); |
115 startWord = -1; | 115 startWord = -1; |
116 } else if (startWord === -1) | 116 } else if (startWord === -1) |
117 startWord = i; | 117 startWord = i; |
118 } | 118 } |
119 if (startWord !== -1) | 119 if (startWord !== -1) |
120 wordCallback(text.substring(startWord)); | 120 wordCallback(text.substring(startWord)); |
121 }, | 121 }, |
122 | 122 |
123 /** | 123 /** |
124 * @param {string} source | |
125 * @param {number=} startIndex | |
126 * @param {number=} lastIndex | |
127 * @return {number} | |
128 */ | |
129 findBalancedCurlyBrackets: function(source, startIndex, lastIndex) | |
130 { | |
131 // The function is performance sensitive. | |
132 lastIndex = lastIndex || source.length; | |
133 startIndex = startIndex || 0; | |
134 var counter = 0; | |
135 | |
136 var index = startIndex; | |
137 while (index < lastIndex) { | |
138 // This part counts brackets until end of string or a double quote. | |
139 for (; index < lastIndex; ++index) { | |
140 var character = source[index]; | |
141 if (character === "\"") | |
142 break; | |
143 else if (character === "{") | |
144 ++counter; | |
145 else if (character === "}") { | |
146 if (--counter === 0) | |
147 return index + 1; | |
148 } | |
149 } | |
150 if (index === lastIndex) | |
151 return -1; | |
152 // This part seeks the closing double quote which could have even nu mber of back slashes prefix. | |
153 var regexp = WebInspector.TextUtils._ClosingDoubleQuoteRegexp; | |
154 regexp.lastIndex = index; | |
155 if (!regexp.test(source)) | |
156 return -1; | |
157 index = regexp.lastIndex; | |
158 } | |
159 return -1; | |
160 }, | |
161 | |
162 /** | |
163 * @param {string} line | 124 * @param {string} line |
164 * @return {string} | 125 * @return {string} |
165 */ | 126 */ |
166 lineIndent: function(line) | 127 lineIndent: function(line) |
167 { | 128 { |
168 var indentation = 0; | 129 var indentation = 0; |
169 while (indentation < line.length && WebInspector.TextUtils.isSpaceChar(l ine.charAt(indentation))) | 130 while (indentation < line.length && WebInspector.TextUtils.isSpaceChar(l ine.charAt(indentation))) |
170 ++indentation; | 131 ++indentation; |
171 return line.substr(0, indentation); | 132 return line.substr(0, indentation); |
172 }, | 133 }, |
(...skipping 11 matching lines...) Expand all Loading... | |
184 * @param {string} text | 145 * @param {string} text |
185 * @return {boolean} | 146 * @return {boolean} |
186 */ | 147 */ |
187 isLowerCase: function(text) | 148 isLowerCase: function(text) |
188 { | 149 { |
189 return text === text.toLowerCase(); | 150 return text === text.toLowerCase(); |
190 } | 151 } |
191 } | 152 } |
192 | 153 |
193 WebInspector.TextUtils._SpaceCharRegex = /\s/; | 154 WebInspector.TextUtils._SpaceCharRegex = /\s/; |
194 WebInspector.TextUtils._ClosingDoubleQuoteRegexp = /[^\\](?:\\\\)*"/g; | |
195 | 155 |
196 /** | 156 /** |
197 * @enum {string} | 157 * @enum {string} |
198 */ | 158 */ |
199 WebInspector.TextUtils.Indent = { | 159 WebInspector.TextUtils.Indent = { |
200 TwoSpaces: " ", | 160 TwoSpaces: " ", |
201 FourSpaces: " ", | 161 FourSpaces: " ", |
202 EightSpaces: " ", | 162 EightSpaces: " ", |
203 TabCharacter: "\t" | 163 TabCharacter: "\t" |
204 } | 164 } |
165 | |
166 /** | |
167 * @constructor | |
168 * @param {function(string)} callback | |
169 * @param {boolean=} findMultiple | |
170 */ | |
171 WebInspector.TextUtils.BalancedJSONTokenizer = function(callback, findMultiple) | |
172 { | |
173 this._callback = callback; | |
174 this._index = 0; | |
175 this._balance = 0; | |
176 this._buffer = ""; | |
177 this._findMultiple = findMultiple || false; | |
178 this._closingDoubleQuoteRegex = /[^\\](?:\\\\)*"/g; | |
179 } | |
180 | |
181 WebInspector.TextUtils.BalancedJSONTokenizer.prototype = { | |
182 /** | |
183 * @param {string} chunk | |
184 */ | |
185 write: function(chunk) | |
186 { | |
187 this._buffer += chunk; | |
188 var lastIndex = this._buffer.length; | |
189 // This does speed things up 5 times. | |
alph
2015/03/01 13:26:33
That's strange provided you're not using buffer in
pfeldman
2015/03/01 14:04:59
I was testing it with and without locals and was g
| |
190 var buffer = this._buffer; | |
191 for (var index = this._index; index < lastIndex; ++index) { | |
192 var character = this._buffer[index]; | |
193 if (character === "\"") { | |
194 this._closingDoubleQuoteRegex.lastIndex = index; | |
195 if (!this._closingDoubleQuoteRegex.test(this._buffer)) | |
196 break; | |
197 index = this._closingDoubleQuoteRegex.lastIndex - 1; | |
198 } else if (character === "{") { | |
199 ++this._balance; | |
200 } else if (character === "}") { | |
201 if (--this._balance === 0) { | |
202 this._lastBalancedIndex = index + 1; | |
203 if (!this._findMultiple) { | |
alph
2015/03/01 13:26:33
break;
pfeldman
2015/03/01 14:04:59
Done.
| |
204 this._reportBalanced(); | |
205 return; | |
206 } | |
207 } | |
208 } | |
209 } | |
210 this._index = index; | |
211 this._reportBalanced(); | |
212 }, | |
213 | |
214 _reportBalanced: function() | |
215 { | |
216 if (this._lastBalancedIndex) { | |
alph
2015/03/01 13:26:33
nit: if (!...) return;
pfeldman
2015/03/01 14:04:59
Done.
| |
217 this._callback(this._buffer.slice(0, this._lastBalancedIndex)); | |
218 this._buffer = this._buffer.slice(this._lastBalancedIndex); | |
219 this._index -= this._lastBalancedIndex; | |
220 this._lastBalancedIndex = 0; | |
221 } | |
222 }, | |
223 | |
224 /** | |
225 * @return {string} | |
226 */ | |
227 remainder: function() | |
228 { | |
229 return this._buffer; | |
230 } | |
231 } | |
OLD | NEW |