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 var buffer = this._buffer; |
| 190 for (var index = this._index; index < lastIndex; ++index) { |
| 191 var character = buffer[index]; |
| 192 if (character === "\"") { |
| 193 this._closingDoubleQuoteRegex.lastIndex = index; |
| 194 if (!this._closingDoubleQuoteRegex.test(buffer)) |
| 195 break; |
| 196 index = this._closingDoubleQuoteRegex.lastIndex - 1; |
| 197 } else if (character === "{") { |
| 198 ++this._balance; |
| 199 } else if (character === "}") { |
| 200 if (--this._balance === 0) { |
| 201 this._lastBalancedIndex = index + 1; |
| 202 if (!this._findMultiple) |
| 203 break; |
| 204 } |
| 205 } |
| 206 } |
| 207 this._index = index; |
| 208 this._reportBalanced(); |
| 209 }, |
| 210 |
| 211 _reportBalanced: function() |
| 212 { |
| 213 if (!this._lastBalancedIndex) |
| 214 return; |
| 215 this._callback(this._buffer.slice(0, this._lastBalancedIndex)); |
| 216 this._buffer = this._buffer.slice(this._lastBalancedIndex); |
| 217 this._index -= this._lastBalancedIndex; |
| 218 this._lastBalancedIndex = 0; |
| 219 }, |
| 220 |
| 221 /** |
| 222 * @return {string} |
| 223 */ |
| 224 remainder: function() |
| 225 { |
| 226 return this._buffer; |
| 227 } |
| 228 } |
OLD | NEW |