OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
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 // Whitespaces 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 // Unicode whitespaces | |
37 0x000A, // Line Feed LF | |
38 0x000D, // Carriage Return CR | |
39 0x0085, // Next Line NEL | |
40 0x1680, // Ogham Space Mark | |
41 0x180E, // Mongolian Vowel Separator | |
42 0x2000, // EN QUAD | |
43 0x2001, // EM QUAD | |
44 0x2002, // EN SPACE | |
45 0x2003, // EM SPACE | |
46 0x2004, // THREE-PER-EM SPACE | |
47 0x2005, // FOUR-PER-EM SPACE | |
48 0x2006, // SIX-PER-EM SPACE | |
49 0x2007, // FIGURE SPACE | |
50 0x2008, // PUNCTUATION SPACE | |
51 0x2009, // THIN SPACE | |
52 0x200A, // HAIR SPACE | |
53 0x2028, // LINE SEPARATOR | |
54 0x2029, // PARAGRAPH SEPARATOR | |
55 0x202F, // NARROW NO-BREAK SPACE | |
56 0x205F, // MEDIUM MATHEMATICAL SPACE | |
57 0x3000, // IDEOGRAPHIC SPACE | |
58 ]; | |
59 | |
60 // Add single twobyte char to force twobyte representation. | |
61 // Interestingly, snowman is not "white" space :) | |
62 var twobyte = "\u2603"; | |
63 var onebyte = "\u007E"; | |
64 var twobytespace = "\u2000"; | |
65 var onebytespace = "\u0020"; | |
66 | |
67 function is_whitespace(c) { | |
68 return whitespaces.indexOf(c.charCodeAt(0)) > -1; | |
69 } | |
70 | |
71 function test_regexp(str) { | |
72 var pos_match = str.match(/\s/); | |
73 var neg_match = str.match(/\S/); | |
74 var test_char = str[0]; | |
75 var postfix = str[1]; | |
76 if (is_whitespace(test_char)) { | |
77 assertEquals(test_char, pos_match[0]); | |
78 assertEquals(postfix, neg_match[0]); | |
79 } else { | |
80 assertEquals(test_char, neg_match[0]); | |
81 assertNull(pos_match); | |
82 } | |
83 } | |
84 | |
85 function test_trim(c, infix) { | |
86 var str = c + c + c + infix + c; | |
87 if (is_whitespace(c)) { | |
88 assertEquals(infix, str.trim()); | |
89 } else { | |
90 assertEquals(str, str.trim()); | |
91 } | |
92 } | |
93 | |
94 function test_parseInt(c, postfix) { | |
95 // Skip if prefix is a digit. | |
96 if (c >= "0" && c <= 9) return; | |
97 var str = c + c + "123" + postfix; | |
98 if (is_whitespace(c)) { | |
99 assertEquals(123, parseInt(str)); | |
100 } else { | |
101 assertEquals(NaN, parseInt(str)); | |
102 } | |
103 } | |
104 | |
105 function test_eval(c, content) { | |
106 if (!is_whitespace(c)) return; | |
107 var str = c + c + "'" + content + "'" + c + c; | |
108 assertEquals(content, eval(str)); | |
109 } | |
110 | |
111 function test_stringtonumber(c, postfix) { | |
112 // Skip if prefix is a digit. | |
113 if (c >= "0" && c <= 9) return; | |
114 var result = 1 + Number(c + "123" + c + postfix); | |
115 if (is_whitespace(c)) { | |
116 assertEquals(124, result); | |
117 } else { | |
118 assertEquals(NaN, result); | |
119 } | |
120 } | |
121 | |
122 for (var i = 0; i < 0x10000; i++) { | |
123 c = String.fromCharCode(i); | |
124 test_regexp(c + onebyte); | |
125 test_regexp(c + twobyte); | |
126 test_trim(c, onebyte + "trim"); | |
127 test_trim(c, twobyte + "trim"); | |
128 test_parseInt(c, onebyte); | |
129 test_parseInt(c, twobyte); | |
130 test_eval(c, onebyte); | |
131 test_eval(c, twobyte); | |
132 test_stringtonumber(c, onebytespace); | |
133 test_stringtonumber(c, twobytespace); | |
134 } | |
OLD | NEW |