Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/TranslateEscapes.java
41149 views
1
/*
2
* Copyright (c) 2019, 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
* @test
26
* @bug 8223780
27
* @summary This exercises String#translateEscapes patterns and limits.
28
* @compile TranslateEscapes.java
29
* @run main TranslateEscapes
30
*/
31
32
public class TranslateEscapes {
33
public static void main(String... arg) {
34
test1();
35
test2();
36
test3();
37
test4();
38
}
39
40
/*
41
* Standard escapes.
42
*/
43
static void test1() {
44
verifyEscape("b", '\b');
45
verifyEscape("f", '\f');
46
verifyEscape("n", '\n');
47
verifyEscape("r", '\r');
48
verifyEscape("s", '\s');
49
verifyEscape("t", '\t');
50
verifyEscape("'", '\'');
51
verifyEscape("\"", '\"');
52
verifyEscape("\\", '\\');
53
}
54
55
/*
56
* Octal escapes.
57
*/
58
static void test2() {
59
verifyOctalEscape("0", 0);
60
verifyOctalEscape("3", 03);
61
verifyOctalEscape("7", 07);
62
verifyOctalEscape("07", 07);
63
verifyOctalEscape("17", 017);
64
verifyOctalEscape("27", 027);
65
verifyOctalEscape("37", 037);
66
verifyOctalEscape("377", 0377);
67
68
verifyOctalEscape("777", 077);
69
verifyOctalEscape("78", 07);
70
}
71
72
/*
73
* Exceptions.
74
*/
75
static void test3() {
76
exceptionThrown("+");
77
exceptionThrown("q");
78
}
79
80
/*
81
* Escape line terminator.
82
*/
83
static void test4() {
84
verifyLineTerminator("\n");
85
verifyLineTerminator("\r\n");
86
verifyLineTerminator("\r");
87
}
88
89
static void verifyEscape(String string, char ch) {
90
String escapes = "\\" + string;
91
if (escapes.translateEscapes().charAt(0) != ch) {
92
System.err.format("\"%s\" not escape \"%s\"'%n", string, escapes);
93
throw new RuntimeException();
94
}
95
}
96
97
static void verifyOctalEscape(String string, int octal) {
98
String escapes = "\\" + string;
99
if (escapes.translateEscapes().charAt(0) != octal) {
100
System.err.format("\"%s\" not octal %o%n", string, octal);
101
throw new RuntimeException();
102
}
103
}
104
105
static void exceptionThrown(String string) {
106
String escapes = "\\" + string;
107
try {
108
escapes.translateEscapes();
109
System.err.format("escape not thrown for %s%n", string);
110
throw new RuntimeException();
111
112
} catch (IllegalArgumentException ex) {
113
// okay
114
}
115
}
116
117
static void verifyLineTerminator(String string) {
118
String escapes = "\\" + string;
119
if (!escapes.translateEscapes().isEmpty()) {
120
System.err.format("escape for line terminator not handled %s%n",
121
string.replace("\n", "\\n").replace("\r", "\\r"));
122
throw new RuntimeException();
123
}
124
}
125
}
126
127