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:
Dmitriy Dumanskiy 2016-08-01 01:36:46 +03:00 committed by Norman Maurer
parent 7a4a27574f
commit e842b2d6a9

View File

@ -16,17 +16,16 @@
package io.netty.util.internal;
import java.io.IOException;
import java.util.Formatter;
import java.util.List;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.*;
/**
* String utility class.
*/
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 COMMA = ',';
public static final char LINE_FEED = '\n';
@ -43,47 +42,28 @@ public final class StringUtil {
private static final char PACKAGE_SEPARATOR_CHAR = '.';
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.
int i;
for (i = 0; i < 10; i ++) {
StringBuilder buf = new StringBuilder(2);
buf.append('0');
buf.append(i);
BYTE2HEX_PAD[i] = buf.toString();
BYTE2HEX_PAD[i] = "0" + i;
BYTE2HEX_NOPAD[i] = String.valueOf(i);
}
for (; i < 16; i ++) {
StringBuilder buf = new StringBuilder(2);
char c = (char) ('a' + i - 10);
buf.append('0');
buf.append(c);
BYTE2HEX_PAD[i] = buf.toString();
BYTE2HEX_PAD[i] = "0" + c;
BYTE2HEX_NOPAD[i] = String.valueOf(c);
}
for (; i < BYTE2HEX_PAD.length; i ++) {
StringBuilder buf = new StringBuilder(2);
buf.append(Integer.toHexString(i));
String str = buf.toString();
String str = Integer.toHexString(i);
BYTE2HEX_PAD[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
* version of {@link String#split(String)}.
@ -473,8 +453,4 @@ public final class StringUtil {
private static boolean isDoubleQuote(char c) {
return c == DOUBLE_QUOTE;
}
private StringUtil() {
// Unused.
}
}