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 | |
190 while (this._index < lastIndex) { | |
alph
2015/03/01 11:30:13
this function seems to be performance sensitive ;-
pfeldman
2015/03/01 12:18:53
Done.
| |
191 // This part counts brackets until end of string or a double quote. | |
192 for (; this._index < lastIndex; ++this._index) { | |
193 var character = this._buffer[this._index]; | |
194 if (character === "\"") | |
195 break; | |
196 else if (character === "{") | |
197 ++this._balance; | |
198 else if (character === "}") { | |
199 if (--this._balance === 0) { | |
200 this._lastBalancedIndex = this._index + 1; | |
201 if (!this._findMultiple) { | |
alph
2015/03/01 11:30:13
That behavior seems strange.
You report a single b
pfeldman
2015/03/01 12:18:53
This is a "one shot" operation, there is no accumu
| |
202 this._reportBalanced(); | |
203 return; | |
204 } | |
205 } | |
206 } | |
207 } | |
208 | |
209 if (this._index === lastIndex) | |
210 break; | |
211 | |
212 // This part seeks the closing double quote which could have even nu mber of back slashes prefix. | |
alph
2015/03/01 11:30:13
Mind to move this part right under the if (charact
pfeldman
2015/03/01 12:18:53
This is a great idea! Done.
| |
213 this._closingDoubleQuoteRegex.lastIndex = this._index; | |
214 if (!this._closingDoubleQuoteRegex.test(this._buffer)) | |
215 break; | |
216 this._index = this._closingDoubleQuoteRegex.lastIndex; | |
217 } | |
218 this._reportBalanced(); | |
219 }, | |
220 | |
221 _reportBalanced: function() | |
222 { | |
223 if (this._lastBalancedIndex) { | |
224 this._callback(this._buffer.slice(0, this._lastBalancedIndex)); | |
225 this._buffer = this._buffer.slice(this._lastBalancedIndex); | |
226 this._index -= this._lastBalancedIndex; | |
227 this._lastBalancedIndex = 0; | |
228 } | |
229 }, | |
230 | |
231 /** | |
232 * @return {string} | |
233 */ | |
234 remainder: function() | |
235 { | |
236 return this._buffer; | |
237 } | |
238 } | |
OLD | NEW |