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 |
11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 * distribution. | 13 * distribution. |
14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 * | 17 * |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 Common.TextUtils = { | 30 TextUtils.TextUtils = { |
31 /** | 31 /** |
32 * @param {string} char | 32 * @param {string} char |
33 * @return {boolean} | 33 * @return {boolean} |
34 */ | 34 */ |
35 isStopChar: function(char) { | 35 isStopChar: function(char) { |
36 return (char > ' ' && char < '0') || (char > '9' && char < 'A') || (char > '
Z' && char < '_') || | 36 return (char > ' ' && char < '0') || (char > '9' && char < 'A') || (char > '
Z' && char < '_') || |
37 (char > '_' && char < 'a') || (char > 'z' && char <= '~'); | 37 (char > '_' && char < 'a') || (char > 'z' && char <= '~'); |
38 }, | 38 }, |
39 | 39 |
40 /** | 40 /** |
41 * @param {string} char | 41 * @param {string} char |
42 * @return {boolean} | 42 * @return {boolean} |
43 */ | 43 */ |
44 isWordChar: function(char) { | 44 isWordChar: function(char) { |
45 return !Common.TextUtils.isStopChar(char) && !Common.TextUtils.isSpaceChar(c
har); | 45 return !TextUtils.TextUtils.isStopChar(char) && !TextUtils.TextUtils.isSpace
Char(char); |
46 }, | 46 }, |
47 | 47 |
48 /** | 48 /** |
49 * @param {string} char | 49 * @param {string} char |
50 * @return {boolean} | 50 * @return {boolean} |
51 */ | 51 */ |
52 isSpaceChar: function(char) { | 52 isSpaceChar: function(char) { |
53 return Common.TextUtils._SpaceCharRegex.test(char); | 53 return TextUtils.TextUtils._SpaceCharRegex.test(char); |
54 }, | 54 }, |
55 | 55 |
56 /** | 56 /** |
57 * @param {string} word | 57 * @param {string} word |
58 * @return {boolean} | 58 * @return {boolean} |
59 */ | 59 */ |
60 isWord: function(word) { | 60 isWord: function(word) { |
61 for (var i = 0; i < word.length; ++i) { | 61 for (var i = 0; i < word.length; ++i) { |
62 if (!Common.TextUtils.isWordChar(word.charAt(i))) | 62 if (!TextUtils.TextUtils.isWordChar(word.charAt(i))) |
63 return false; | 63 return false; |
64 } | 64 } |
65 return true; | 65 return true; |
66 }, | 66 }, |
67 | 67 |
68 /** | 68 /** |
69 * @param {string} char | 69 * @param {string} char |
70 * @return {boolean} | 70 * @return {boolean} |
71 */ | 71 */ |
72 isOpeningBraceChar: function(char) { | 72 isOpeningBraceChar: function(char) { |
73 return char === '(' || char === '{'; | 73 return char === '(' || char === '{'; |
74 }, | 74 }, |
75 | 75 |
76 /** | 76 /** |
77 * @param {string} char | 77 * @param {string} char |
78 * @return {boolean} | 78 * @return {boolean} |
79 */ | 79 */ |
80 isClosingBraceChar: function(char) { | 80 isClosingBraceChar: function(char) { |
81 return char === ')' || char === '}'; | 81 return char === ')' || char === '}'; |
82 }, | 82 }, |
83 | 83 |
84 /** | 84 /** |
85 * @param {string} char | 85 * @param {string} char |
86 * @return {boolean} | 86 * @return {boolean} |
87 */ | 87 */ |
88 isBraceChar: function(char) { | 88 isBraceChar: function(char) { |
89 return Common.TextUtils.isOpeningBraceChar(char) || Common.TextUtils.isClosi
ngBraceChar(char); | 89 return TextUtils.TextUtils.isOpeningBraceChar(char) || TextUtils.TextUtils.i
sClosingBraceChar(char); |
90 }, | 90 }, |
91 | 91 |
92 /** | 92 /** |
93 * @param {string} text | 93 * @param {string} text |
94 * @param {function(string):boolean} isWordChar | 94 * @param {function(string):boolean} isWordChar |
95 * @param {function(string)} wordCallback | 95 * @param {function(string)} wordCallback |
96 */ | 96 */ |
97 textToWords: function(text, isWordChar, wordCallback) { | 97 textToWords: function(text, isWordChar, wordCallback) { |
98 var startWord = -1; | 98 var startWord = -1; |
99 for (var i = 0; i < text.length; ++i) { | 99 for (var i = 0; i < text.length; ++i) { |
100 if (!isWordChar(text.charAt(i))) { | 100 if (!isWordChar(text.charAt(i))) { |
101 if (startWord !== -1) | 101 if (startWord !== -1) |
102 wordCallback(text.substring(startWord, i)); | 102 wordCallback(text.substring(startWord, i)); |
103 startWord = -1; | 103 startWord = -1; |
104 } else if (startWord === -1) { | 104 } else if (startWord === -1) { |
105 startWord = i; | 105 startWord = i; |
106 } | 106 } |
107 } | 107 } |
108 if (startWord !== -1) | 108 if (startWord !== -1) |
109 wordCallback(text.substring(startWord)); | 109 wordCallback(text.substring(startWord)); |
110 }, | 110 }, |
111 | 111 |
112 /** | 112 /** |
113 * @param {string} line | 113 * @param {string} line |
114 * @return {string} | 114 * @return {string} |
115 */ | 115 */ |
116 lineIndent: function(line) { | 116 lineIndent: function(line) { |
117 var indentation = 0; | 117 var indentation = 0; |
118 while (indentation < line.length && Common.TextUtils.isSpaceChar(line.charAt
(indentation))) | 118 while (indentation < line.length && TextUtils.TextUtils.isSpaceChar(line.cha
rAt(indentation))) |
119 ++indentation; | 119 ++indentation; |
120 return line.substr(0, indentation); | 120 return line.substr(0, indentation); |
121 }, | 121 }, |
122 | 122 |
123 /** | 123 /** |
124 * @param {string} text | 124 * @param {string} text |
125 * @return {boolean} | 125 * @return {boolean} |
126 */ | 126 */ |
127 isUpperCase: function(text) { | 127 isUpperCase: function(text) { |
128 return text === text.toUpperCase(); | 128 return text === text.toUpperCase(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 matches.push({value: match, position: startIndex + result.index, regexIn
dex: regexIndex}); | 177 matches.push({value: match, position: startIndex + result.index, regexIn
dex: regexIndex}); |
178 currentIndex = result.index + match.length; | 178 currentIndex = result.index + match.length; |
179 } | 179 } |
180 var stringAfterMatches = text.substring(currentIndex); | 180 var stringAfterMatches = text.substring(currentIndex); |
181 if (stringAfterMatches) | 181 if (stringAfterMatches) |
182 doSplit(stringAfterMatches, regexIndex + 1, startIndex + currentIndex); | 182 doSplit(stringAfterMatches, regexIndex + 1, startIndex + currentIndex); |
183 } | 183 } |
184 } | 184 } |
185 }; | 185 }; |
186 | 186 |
187 Common.TextUtils._SpaceCharRegex = /\s/; | 187 TextUtils.TextUtils._SpaceCharRegex = /\s/; |
188 | 188 |
189 /** | 189 /** |
190 * @enum {string} | 190 * @enum {string} |
191 */ | 191 */ |
192 Common.TextUtils.Indent = { | 192 TextUtils.TextUtils.Indent = { |
193 TwoSpaces: ' ', | 193 TwoSpaces: ' ', |
194 FourSpaces: ' ', | 194 FourSpaces: ' ', |
195 EightSpaces: ' ', | 195 EightSpaces: ' ', |
196 TabCharacter: '\t' | 196 TabCharacter: '\t' |
197 }; | 197 }; |
198 | 198 |
199 /** | 199 /** |
200 * @unrestricted | 200 * @unrestricted |
201 */ | 201 */ |
202 Common.TextUtils.BalancedJSONTokenizer = class { | 202 TextUtils.TextUtils.BalancedJSONTokenizer = class { |
203 /** | 203 /** |
204 * @param {function(string)} callback | 204 * @param {function(string)} callback |
205 * @param {boolean=} findMultiple | 205 * @param {boolean=} findMultiple |
206 */ | 206 */ |
207 constructor(callback, findMultiple) { | 207 constructor(callback, findMultiple) { |
208 this._callback = callback; | 208 this._callback = callback; |
209 this._index = 0; | 209 this._index = 0; |
210 this._balance = 0; | 210 this._balance = 0; |
211 this._buffer = ''; | 211 this._buffer = ''; |
212 this._findMultiple = findMultiple || false; | 212 this._findMultiple = findMultiple || false; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 * @return {string} | 264 * @return {string} |
265 */ | 265 */ |
266 remainder() { | 266 remainder() { |
267 return this._buffer; | 267 return this._buffer; |
268 } | 268 } |
269 }; | 269 }; |
270 | 270 |
271 /** | 271 /** |
272 * @interface | 272 * @interface |
273 */ | 273 */ |
274 Common.TokenizerFactory = function() {}; | 274 TextUtils.TokenizerFactory = function() {}; |
275 | 275 |
276 Common.TokenizerFactory.prototype = { | 276 TextUtils.TokenizerFactory.prototype = { |
277 /** | 277 /** |
278 * @param {string} mimeType | 278 * @param {string} mimeType |
279 * @return {function(string, function(string, ?string, number, number))} | 279 * @return {function(string, function(string, ?string, number, number))} |
280 */ | 280 */ |
281 createTokenizer(mimeType) {} | 281 createTokenizer(mimeType) {} |
282 }; | 282 }; |
OLD | NEW |