1// Copyright 2014 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5var whitespaces = [ 6 // WhiteSpace defined in ECMA-262 5.1, 7.2 7 0x0009, // Tab TAB 8 0x000B, // Vertical Tab VT 9 0x000C, // Form Feed FF 10 0x0020, // Space SP 11 0x00A0, // No-break space NBSP 12 0xFEFF, // Byte Order Mark BOM 13 14 // LineTerminator defined in ECMA-262 5.1, 7.3 15 0x000A, // Line Feed LF 16 0x000D, // Carriage Return CR 17 0x2028, // Line Separator LS 18 0x2029, // Paragraph Separator PS 19 20 // Unicode 6.3.0 whitespaces (category 'Zs') 21 0x1680, // Ogham Space Mark 22 0x180E, // Mongolian Vowel Separator 23 0x2000, // EN QUAD 24 0x2001, // EM QUAD 25 0x2002, // EN SPACE 26 0x2003, // EM SPACE 27 0x2004, // THREE-PER-EM SPACE 28 0x2005, // FOUR-PER-EM SPACE 29 0x2006, // SIX-PER-EM SPACE 30 0x2007, // FIGURE SPACE 31 0x2008, // PUNCTUATION SPACE 32 0x2009, // THIN SPACE 33 0x200A, // HAIR SPACE 34 0x2028, // LINE SEPARATOR 35 0x2029, // PARAGRAPH SEPARATOR 36 0x202F, // NARROW NO-BREAK SPACE 37 0x205F, // MEDIUM MATHEMATICAL SPACE 38 0x3000, // IDEOGRAPHIC SPACE 39]; 40 41// Add single twobyte char to force twobyte representation. 42// Interestingly, snowman is not "white" space :) 43var twobyte = "\u2603"; 44var onebyte = "\u007E"; 45var twobytespace = "\u2000"; 46var onebytespace = "\u0020"; 47 48function is_whitespace(c) { 49 return whitespaces.indexOf(c.charCodeAt(0)) > -1; 50} 51 52function test_regexp(str) { 53 var pos_match = str.match(/\s/); 54 var neg_match = str.match(/\S/); 55 var test_char = str[0]; 56 var postfix = str[1]; 57 if (is_whitespace(test_char)) { 58 assertEquals(test_char, pos_match[0]); 59 assertEquals(postfix, neg_match[0]); 60 } else { 61 assertEquals(test_char, neg_match[0]); 62 assertNull(pos_match); 63 } 64} 65 66function test_trim(c, infix) { 67 var str = c + c + c + infix + c; 68 if (is_whitespace(c)) { 69 assertEquals(infix, str.trim()); 70 } else { 71 assertEquals(str, str.trim()); 72 } 73} 74 75function test_parseInt(c, postfix) { 76 // Skip if prefix is a digit. 77 if (c >= "0" && c <= "9") return; 78 var str = c + c + "123" + postfix; 79 if (is_whitespace(c)) { 80 assertEquals(123, parseInt(str)); 81 } else { 82 assertEquals(NaN, parseInt(str)); 83 } 84} 85 86function test_eval(c, content) { 87 if (!is_whitespace(c)) return; 88 var str = c + c + "'" + content + "'" + c + c; 89 assertEquals(content, eval(str)); 90} 91 92function test_stringtonumber(c, postfix) { 93 // Skip if prefix is a digit. 94 if (c >= "0" && c <= "9") return; 95 var result = 1 + Number(c + "123" + c + postfix); 96 if (is_whitespace(c)) { 97 assertEquals(124, result); 98 } else { 99 assertEquals(NaN, result); 100 } 101} 102 103for (var i = 0; i < 0x10000; i++) { 104 c = String.fromCharCode(i); 105 test_regexp(c + onebyte); 106 test_regexp(c + twobyte); 107 test_trim(c, onebyte + "trim"); 108 test_trim(c, twobyte + "trim"); 109 test_parseInt(c, onebyte); 110 test_parseInt(c, twobyte); 111 test_eval(c, onebyte); 112 test_eval(c, twobyte); 113 test_stringtonumber(c, onebytespace); 114 test_stringtonumber(c, twobytespace); 115} 116