mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8223780: String::translateEscapes (Preview)
Reviewed-by: abuckley, vromero, jlahoda, bchristi, igerasim, smarks
This commit is contained in:
parent
0777a86d68
commit
fead01f2c9
2 changed files with 244 additions and 0 deletions
|
@ -3011,6 +3011,146 @@ public final class String
|
|||
return outdent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string whose value is this string, with escape sequences
|
||||
* translated as if in a string literal.
|
||||
* <p>
|
||||
* Escape sequences are translated as follows;
|
||||
* <table class="plain">
|
||||
* <caption style="display:none">Translation</caption>
|
||||
* <thead>
|
||||
* <tr>
|
||||
* <th scope="col">Escape</th>
|
||||
* <th scope="col">Name</th>
|
||||
* <th scope="col">Translation</th>
|
||||
* </tr>
|
||||
* </thead>
|
||||
* <tr>
|
||||
* <td>{@code \u005Cb}</td>
|
||||
* <td>backspace</td>
|
||||
* <td>{@code U+0008}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005Ct}</td>
|
||||
* <td>horizontal tab</td>
|
||||
* <td>{@code U+0009}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005Cn}</td>
|
||||
* <td>line feed</td>
|
||||
* <td>{@code U+000A}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005Cf}</td>
|
||||
* <td>form feed</td>
|
||||
* <td>{@code U+000C}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005Cr}</td>
|
||||
* <td>carriage return</td>
|
||||
* <td>{@code U+000D}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005C"}</td>
|
||||
* <td>double quote</td>
|
||||
* <td>{@code U+0022}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005C'}</td>
|
||||
* <td>single quote</td>
|
||||
* <td>{@code U+0027}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005C\u005C}</td>
|
||||
* <td>backslash</td>
|
||||
* <td>{@code U+005C}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>{@code \u005C0 - \u005C377}</td>
|
||||
* <td>octal escape</td>
|
||||
* <td>code point equivalents</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @implNote
|
||||
* This method does <em>not</em> translate Unicode escapes such as "{@code \u005cu2022}".
|
||||
* Unicode escapes are translated by the Java compiler when reading input characters and
|
||||
* are not part of the string literal specification.
|
||||
*
|
||||
* @throws IllegalArgumentException when an escape sequence is malformed.
|
||||
*
|
||||
* @return String with escape sequences translated.
|
||||
*
|
||||
* @jls 3.10.7 Escape Sequences
|
||||
*
|
||||
* @since 13
|
||||
*
|
||||
* @deprecated This method is associated with text blocks, a preview language feature.
|
||||
* Text blocks and/or this method may be changed or removed in a future release.
|
||||
*/
|
||||
@Deprecated(forRemoval=true, since="13")
|
||||
public String translateEscapes() {
|
||||
if (isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
char[] chars = toCharArray();
|
||||
int length = chars.length;
|
||||
int from = 0;
|
||||
int to = 0;
|
||||
while (from < length) {
|
||||
char ch = chars[from++];
|
||||
if (ch == '\\') {
|
||||
ch = from < length ? chars[from++] : '\0';
|
||||
switch (ch) {
|
||||
case 'b':
|
||||
ch = '\b';
|
||||
break;
|
||||
case 'f':
|
||||
ch = '\f';
|
||||
break;
|
||||
case 'n':
|
||||
ch = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
ch = '\r';
|
||||
break;
|
||||
case 't':
|
||||
ch = '\t';
|
||||
break;
|
||||
case '\'':
|
||||
case '\"':
|
||||
case '\\':
|
||||
// as is
|
||||
break;
|
||||
case '0': case '1': case '2': case '3':
|
||||
case '4': case '5': case '6': case '7':
|
||||
int limit = Integer.min(from + (ch <= '3' ? 2 : 1), length);
|
||||
int code = ch - '0';
|
||||
while (from < limit) {
|
||||
ch = chars[from];
|
||||
if (ch < '0' || '7' < ch) {
|
||||
break;
|
||||
}
|
||||
from++;
|
||||
code = (code << 3) | (ch - '0');
|
||||
}
|
||||
ch = (char)code;
|
||||
break;
|
||||
default: {
|
||||
String msg = String.format(
|
||||
"Invalid escape sequence: \\%c \\\\u%04X",
|
||||
ch, (int)ch);
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
chars[to++] = ch;
|
||||
}
|
||||
|
||||
return new String(chars, 0, to);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method allows the application of a function to {@code this}
|
||||
* string. The function should expect a single String argument
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue