OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
mathiasb
2014/02/10 09:30:01
Quick drive-by comment: please use the new/shorter
Yang
2014/02/10 10:48:27
I'm not aware we started using this license header
| |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 var whitespaces = [ | |
29 // WhiteSpace defined in ECMA-262 5.1, 7.2 | |
30 0x0009, // Tab TAB | |
31 0x000B, // Vertical Tab VT | |
32 0x000C, // Form Feed FF | |
33 0x0020, // Space SP | |
34 0x00A0, // No-break space NBSP | |
35 0xFEFF, // Byte Order Mark BOM | |
36 | |
37 // LineTerminator defined in ECMA-262 5.1, 7.3 | |
38 0x000A, // Line Feed LF | |
39 0x000D, // Carriage Return CR | |
40 0x2028, // Line Separator LS | |
41 0x2029, // Paragraph Separator PS | |
42 | |
43 // Unicode 6.3.0 whitespaces (category 'Zs') | |
44 0x1680, // Ogham Space Mark | |
45 0x180E, // Mongolian Vowel Separator | |
46 0x2000, // EN QUAD | |
47 0x2001, // EM QUAD | |
48 0x2002, // EN SPACE | |
49 0x2003, // EM SPACE | |
50 0x2004, // THREE-PER-EM SPACE | |
51 0x2005, // FOUR-PER-EM SPACE | |
52 0x2006, // SIX-PER-EM SPACE | |
53 0x2007, // FIGURE SPACE | |
54 0x2008, // PUNCTUATION SPACE | |
55 0x2009, // THIN SPACE | |
56 0x200A, // HAIR SPACE | |
57 0x2028, // LINE SEPARATOR | |
58 0x2029, // PARAGRAPH SEPARATOR | |
59 0x202F, // NARROW NO-BREAK SPACE | |
60 0x205F, // MEDIUM MATHEMATICAL SPACE | |
61 0x3000, // IDEOGRAPHIC SPACE | |
62 ]; | |
63 | |
64 // Add single twobyte char to force twobyte representation. | |
65 // Interestingly, snowman is not "white" space :) | |
66 var twobyte = "\u2603"; | |
67 var onebyte = "\u007E"; | |
68 var twobytespace = "\u2000"; | |
69 var onebytespace = "\u0020"; | |
70 | |
71 function is_whitespace(c) { | |
72 return whitespaces.indexOf(c.charCodeAt(0)) > -1; | |
73 } | |
74 | |
75 function test_regexp(str) { | |
76 var pos_match = str.match(/\s/); | |
77 var neg_match = str.match(/\S/); | |
78 var test_char = str[0]; | |
79 var postfix = str[1]; | |
80 if (is_whitespace(test_char)) { | |
81 assertEquals(test_char, pos_match[0]); | |
82 assertEquals(postfix, neg_match[0]); | |
83 } else { | |
84 assertEquals(test_char, neg_match[0]); | |
85 assertNull(pos_match); | |
86 } | |
87 } | |
88 | |
89 function test_trim(c, infix) { | |
90 var str = c + c + c + infix + c; | |
91 if (is_whitespace(c)) { | |
92 assertEquals(infix, str.trim()); | |
93 } else { | |
94 assertEquals(str, str.trim()); | |
95 } | |
96 } | |
97 | |
98 function test_parseInt(c, postfix) { | |
99 // Skip if prefix is a digit. | |
100 if (c >= "0" && c <= "9") return; | |
101 var str = c + c + "123" + postfix; | |
102 if (is_whitespace(c)) { | |
103 assertEquals(123, parseInt(str)); | |
104 } else { | |
105 assertEquals(NaN, parseInt(str)); | |
106 } | |
107 } | |
108 | |
109 function test_eval(c, content) { | |
110 if (!is_whitespace(c)) return; | |
111 var str = c + c + "'" + content + "'" + c + c; | |
112 assertEquals(content, eval(str)); | |
113 } | |
114 | |
115 function test_stringtonumber(c, postfix) { | |
116 // Skip if prefix is a digit. | |
117 if (c >= "0" && c <= "9") return; | |
118 var result = 1 + Number(c + "123" + c + postfix); | |
119 if (is_whitespace(c)) { | |
120 assertEquals(124, result); | |
121 } else { | |
122 assertEquals(NaN, result); | |
123 } | |
124 } | |
125 | |
126 for (var i = 0; i < 0x10000; i++) { | |
127 c = String.fromCharCode(i); | |
128 test_regexp(c + onebyte); | |
129 test_regexp(c + twobyte); | |
130 test_trim(c, onebyte + "trim"); | |
131 test_trim(c, twobyte + "trim"); | |
132 test_parseInt(c, onebyte); | |
133 test_parseInt(c, twobyte); | |
134 test_eval(c, onebyte); | |
135 test_eval(c, twobyte); | |
136 test_stringtonumber(c, onebytespace); | |
137 test_stringtonumber(c, twobytespace); | |
138 } | |
OLD | NEW |