StringUtil cleanup. NewLine char initializing simplified and code in static section simplified.
Motivation: NewLine initializing is complex, with unnecessary allocations and non-standard. Static section is overloaded with StringBuilders for simple "s" + "s" concatenation pattern that compiler optimizes perfectly. Modifications: NewLine initializing replaced with standard System.getProperty("line.separator"). Removed StringBuilders in static section. Result: Less complex code.
This commit is contained in:
parent
7a4a27574f
commit
e842b2d6a9
@ -16,17 +16,16 @@
|
|||||||
package io.netty.util.internal;
|
package io.netty.util.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
import static io.netty.util.internal.ObjectUtil.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String utility class.
|
* String utility class.
|
||||||
*/
|
*/
|
||||||
public final class StringUtil {
|
public final class StringUtil {
|
||||||
|
|
||||||
public static final String NEWLINE;
|
public static final String NEWLINE = System.getProperty("line.separator");
|
||||||
public static final char DOUBLE_QUOTE = '\"';
|
public static final char DOUBLE_QUOTE = '\"';
|
||||||
public static final char COMMA = ',';
|
public static final char COMMA = ',';
|
||||||
public static final char LINE_FEED = '\n';
|
public static final char LINE_FEED = '\n';
|
||||||
@ -43,47 +42,28 @@ public final class StringUtil {
|
|||||||
private static final char PACKAGE_SEPARATOR_CHAR = '.';
|
private static final char PACKAGE_SEPARATOR_CHAR = '.';
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Determine the newline character of the current platform.
|
|
||||||
String newLine;
|
|
||||||
|
|
||||||
Formatter formatter = new Formatter();
|
|
||||||
try {
|
|
||||||
newLine = formatter.format("%n").toString();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Should not reach here, but just in case.
|
|
||||||
newLine = "\n";
|
|
||||||
} finally {
|
|
||||||
formatter.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
NEWLINE = newLine;
|
|
||||||
|
|
||||||
// Generate the lookup table that converts a byte into a 2-digit hexadecimal integer.
|
// Generate the lookup table that converts a byte into a 2-digit hexadecimal integer.
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 10; i ++) {
|
for (i = 0; i < 10; i ++) {
|
||||||
StringBuilder buf = new StringBuilder(2);
|
BYTE2HEX_PAD[i] = "0" + i;
|
||||||
buf.append('0');
|
|
||||||
buf.append(i);
|
|
||||||
BYTE2HEX_PAD[i] = buf.toString();
|
|
||||||
BYTE2HEX_NOPAD[i] = String.valueOf(i);
|
BYTE2HEX_NOPAD[i] = String.valueOf(i);
|
||||||
}
|
}
|
||||||
for (; i < 16; i ++) {
|
for (; i < 16; i ++) {
|
||||||
StringBuilder buf = new StringBuilder(2);
|
|
||||||
char c = (char) ('a' + i - 10);
|
char c = (char) ('a' + i - 10);
|
||||||
buf.append('0');
|
BYTE2HEX_PAD[i] = "0" + c;
|
||||||
buf.append(c);
|
|
||||||
BYTE2HEX_PAD[i] = buf.toString();
|
|
||||||
BYTE2HEX_NOPAD[i] = String.valueOf(c);
|
BYTE2HEX_NOPAD[i] = String.valueOf(c);
|
||||||
}
|
}
|
||||||
for (; i < BYTE2HEX_PAD.length; i ++) {
|
for (; i < BYTE2HEX_PAD.length; i ++) {
|
||||||
StringBuilder buf = new StringBuilder(2);
|
String str = Integer.toHexString(i);
|
||||||
buf.append(Integer.toHexString(i));
|
|
||||||
String str = buf.toString();
|
|
||||||
BYTE2HEX_PAD[i] = str;
|
BYTE2HEX_PAD[i] = str;
|
||||||
BYTE2HEX_NOPAD[i] = str;
|
BYTE2HEX_NOPAD[i] = str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private StringUtil() {
|
||||||
|
// Unused.
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits the specified {@link String} with the specified delimiter. This operation is a simplified and optimized
|
* Splits the specified {@link String} with the specified delimiter. This operation is a simplified and optimized
|
||||||
* version of {@link String#split(String)}.
|
* version of {@link String#split(String)}.
|
||||||
@ -473,8 +453,4 @@ public final class StringUtil {
|
|||||||
private static boolean isDoubleQuote(char c) {
|
private static boolean isDoubleQuote(char c) {
|
||||||
return c == DOUBLE_QUOTE;
|
return c == DOUBLE_QUOTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private StringUtil() {
|
|
||||||
// Unused.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user