|
1 /* |
|
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * JDK-8019987: String trimRight and trimLeft could be defined. |
|
26 * |
|
27 * @test |
|
28 * @run |
|
29 */ |
|
30 |
|
31 var TESTSTRING = "abcde"; |
|
32 |
|
33 var SPACES = " "; |
|
34 var TESTSTRING_LEFT_SPACES = SPACES + TESTSTRING; |
|
35 var TESTSTRING_RIGHT_SPACES = TESTSTRING + SPACES; |
|
36 var TESTSTRING_BOTH_SPACES = SPACES + TESTSTRING + SPACES; |
|
37 var TESTSTRING_MIDDLE_SPACES = TESTSTRING + SPACES + TESTSTRING; |
|
38 |
|
39 var WHITESPACE = |
|
40 " \t" + // space and tab |
|
41 "\n\r" + // newline and return |
|
42 "\u2028" + // line separator |
|
43 "\u2029" + // paragraph separator |
|
44 "\u000b" + // tabulation line |
|
45 "\u000c" + // ff (ctrl-l) |
|
46 "\u00a0" + // Latin-1 space |
|
47 "\u1680" + // Ogham space mark |
|
48 "\u180e" + // separator, Mongolian vowel |
|
49 "\u2000" + // en quad |
|
50 "\u2001" + // em quad |
|
51 "\u2002" + // en space |
|
52 "\u2003" + // em space |
|
53 "\u2004" + // three-per-em space |
|
54 "\u2005" + // four-per-em space |
|
55 "\u2006" + // six-per-em space |
|
56 "\u2007" + // figure space |
|
57 "\u2008" + // punctuation space |
|
58 "\u2009" + // thin space |
|
59 "\u200a" + // hair space |
|
60 "\u202f" + // narrow no-break space |
|
61 "\u205f" + // medium mathematical space |
|
62 "\u3000" + // ideographic space |
|
63 "\ufeff"; // byte order mark |
|
64 var TESTSTRING_LEFT_WHITESPACE = WHITESPACE + TESTSTRING; |
|
65 var TESTSTRING_RIGHT_WHITESPACE = TESTSTRING + WHITESPACE; |
|
66 var TESTSTRING_BOTH_WHITESPACE = WHITESPACE + TESTSTRING + WHITESPACE; |
|
67 var TESTSTRING_MIDDLE_WHITESPACE = TESTSTRING + WHITESPACE + TESTSTRING; |
|
68 |
|
69 function escape(string) { |
|
70 var sb = new java.lang.StringBuilder(); |
|
71 sb.append("\""); |
|
72 |
|
73 for (var i = 0; i < string.length; i++) { |
|
74 var ch = string.charAt(i); |
|
75 |
|
76 switch (ch) { |
|
77 case '\\': |
|
78 sb.append("\\\\"); |
|
79 break; |
|
80 case '"': |
|
81 sb.append("\\\""); |
|
82 break; |
|
83 case '\'': |
|
84 sb.append("\\\'"); |
|
85 break; |
|
86 case '\b': |
|
87 sb.append("\\b"); |
|
88 break; |
|
89 case '\f': |
|
90 sb.append("\\f"); |
|
91 break; |
|
92 case '\n': |
|
93 sb.append("\\n"); |
|
94 break; |
|
95 case '\r': |
|
96 sb.append("\\r"); |
|
97 break; |
|
98 case '\t': |
|
99 sb.append("\\t"); |
|
100 break; |
|
101 default: |
|
102 var code = string.charCodeAt(i); |
|
103 |
|
104 if (code < 0x20 || code >= 0xFF) { |
|
105 sb.append("\\u"); |
|
106 |
|
107 var hex = java.lang.Integer.toHexString(code); |
|
108 for (var i = hex.length; i < 4; i++) { |
|
109 sb.append('0'); |
|
110 } |
|
111 sb.append(hex); |
|
112 } else { |
|
113 sb.append(ch); |
|
114 } |
|
115 |
|
116 break; |
|
117 } |
|
118 } |
|
119 |
|
120 sb.append("\""); |
|
121 |
|
122 return sb.toString(); |
|
123 } |
|
124 |
|
125 var count = 0; |
|
126 function test(expected, trimmed) { |
|
127 count++; |
|
128 if (trimmed != expected) { |
|
129 print(count + ": Expected: " + escape(expected) + ", found: " + escape(trimmed)); |
|
130 } |
|
131 } |
|
132 |
|
133 test("", SPACES.trim()); |
|
134 test("", SPACES.trimLeft()); |
|
135 test("", SPACES.trimRight()); |
|
136 |
|
137 test(TESTSTRING, TESTSTRING_LEFT_SPACES.trim()); |
|
138 test(TESTSTRING, TESTSTRING_LEFT_SPACES.trimLeft()); |
|
139 test(TESTSTRING_LEFT_SPACES, TESTSTRING_LEFT_SPACES.trimRight()); |
|
140 |
|
141 test(TESTSTRING, TESTSTRING_RIGHT_SPACES.trim()); |
|
142 test(TESTSTRING_RIGHT_SPACES, TESTSTRING_RIGHT_SPACES.trimLeft()); |
|
143 test(TESTSTRING, TESTSTRING_RIGHT_SPACES.trimRight()); |
|
144 |
|
145 test(TESTSTRING, TESTSTRING_BOTH_SPACES.trim()); |
|
146 test(TESTSTRING_RIGHT_SPACES, TESTSTRING_BOTH_SPACES.trimLeft()); |
|
147 test(TESTSTRING_LEFT_SPACES, TESTSTRING_BOTH_SPACES.trimRight()); |
|
148 |
|
149 test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trim()); |
|
150 test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trimLeft()); |
|
151 test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trimRight()); |
|
152 |
|
153 test("", WHITESPACE.trim()); |
|
154 test("", WHITESPACE.trimLeft()); |
|
155 test("", WHITESPACE.trimRight()); |
|
156 |
|
157 test(TESTSTRING, TESTSTRING_LEFT_WHITESPACE.trim()); |
|
158 test(TESTSTRING, TESTSTRING_LEFT_WHITESPACE.trimLeft()); |
|
159 test(TESTSTRING_LEFT_WHITESPACE, TESTSTRING_LEFT_WHITESPACE.trimRight()); |
|
160 |
|
161 test(TESTSTRING, TESTSTRING_RIGHT_WHITESPACE.trim()); |
|
162 test(TESTSTRING_RIGHT_WHITESPACE, TESTSTRING_RIGHT_WHITESPACE.trimLeft()); |
|
163 test(TESTSTRING, TESTSTRING_RIGHT_WHITESPACE.trimRight()); |
|
164 |
|
165 test(TESTSTRING, TESTSTRING_BOTH_WHITESPACE.trim()); |
|
166 test(TESTSTRING_RIGHT_WHITESPACE, TESTSTRING_BOTH_WHITESPACE.trimLeft()); |
|
167 test(TESTSTRING_LEFT_WHITESPACE, TESTSTRING_BOTH_WHITESPACE.trimRight()); |
|
168 |
|
169 test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trim()); |
|
170 test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trimLeft()); |
|
171 test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trimRight()); |