@Test public void asciiSizeInBytes() throws Exception { long value = Long.MIN_VALUE; assertTrue(StringUtils.asciiSizeInBytes(value) == String.valueOf(value).length()); benchmark.doSomething("OKIO的方案:", () -> StringUtils.asciiSizeInBytes(value)); benchmark.doSomething("成龙的方案:", () -> String.valueOf(value).length()); }
public static int asciiSizeInBytes(long v) { if (v == 0) { return 1; } if (v == Long.MIN_VALUE) { return 20; } boolean negative = false; if (v < 0) { v = -v; negative = true; } int width = v < 100000000L ? v < 10000L ? v < 100L ? v < 10L ? 1 : 2 : v < 1000L ? 3 : 4 : v < 1000000L ? v < 100000L ? 5 : 6 : v < 10000000L ? 7 : 8 : v < 1000000000000L ? v < 10000000000L ? v < 1000000000L ? 9 : 10 : v < 100000000000L ? 11 : 12 : v < 1000000000000000L ? v < 10000000000000L ? 13 : v < 10000000000000L ? 14 : 15 : v < 100000000000000000L ? v < 10000000000000000L ? 16 : 17 : v < 1000000000000000000L ? 18 : 19; return negative ? width + 1 : width; }
StringUtils { public static int asciiSizeInBytes(long v) { if (v == 0) { return 1; } if (v == Long.MIN_VALUE) { return 20; } boolean negative = false; if (v < 0) { v = -v; negative = true; } int width = v < 100000000L ? v < 10000L ? v < 100L ? v < 10L ? 1 : 2 : v < 1000L ? 3 : 4 : v < 1000000L ? v < 100000L ? 5 : 6 : v < 10000000L ? 7 : 8 : v < 1000000000000L ? v < 10000000000L ? v < 1000000000L ? 9 : 10 : v < 100000000000L ? 11 : 12 : v < 1000000000000000L ? v < 10000000000000L ? 13 : v < 10000000000000L ? 14 : 15 : v < 100000000000000000L ? v < 10000000000000000L ? 16 : 17 : v < 1000000000000000000L ? 18 : 19; return negative ? width + 1 : width; } }
StringUtils { public static int asciiSizeInBytes(long v) { if (v == 0) { return 1; } if (v == Long.MIN_VALUE) { return 20; } boolean negative = false; if (v < 0) { v = -v; negative = true; } int width = v < 100000000L ? v < 10000L ? v < 100L ? v < 10L ? 1 : 2 : v < 1000L ? 3 : 4 : v < 1000000L ? v < 100000L ? 5 : 6 : v < 10000000L ? 7 : 8 : v < 1000000000000L ? v < 10000000000L ? v < 1000000000L ? 9 : 10 : v < 100000000000L ? 11 : 12 : v < 1000000000000000L ? v < 10000000000000L ? 13 : v < 10000000000000L ? 14 : 15 : v < 100000000000000000L ? v < 10000000000000000L ? 16 : 17 : v < 1000000000000000000L ? 18 : 19; return negative ? width + 1 : width; }  }
StringUtils { public static int asciiSizeInBytes(long v) { if (v == 0) { return 1; } if (v == Long.MIN_VALUE) { return 20; } boolean negative = false; if (v < 0) { v = -v; negative = true; } int width = v < 100000000L ? v < 10000L ? v < 100L ? v < 10L ? 1 : 2 : v < 1000L ? 3 : 4 : v < 1000000L ? v < 100000L ? 5 : 6 : v < 10000000L ? 7 : 8 : v < 1000000000000L ? v < 10000000000L ? v < 1000000000L ? 9 : 10 : v < 100000000000L ? 11 : 12 : v < 1000000000000000L ? v < 10000000000000L ? 13 : v < 10000000000000L ? 14 : 15 : v < 100000000000000000L ? v < 10000000000000000L ? 16 : 17 : v < 1000000000000000000L ? 18 : 19; return negative ? width + 1 : width; }  static boolean isEmpty(final String text); static boolean isNotEmpty(final String text); static boolean isBlank(final String text); static boolean isNotBlank(final String text); static int length(final String text); static String[] split(final String str, final String separatorChars); static String lowerWord(CharSequence cs, char c); static int asciiSizeInBytes(long v); static String join(String... strings); static String build(String delimiter, String prefix, String suffix, String... strings); static byte[] utf8Bytes(CharSequence str); static byte[] bytes(CharSequence str); static byte[] bytes(CharSequence str, Charset charset); static String format(String str, Object... arguments); static String readString(InputStream inputStream); static String readString(InputStream inputStream, Charset charset); static String readString(Reader reader); }
StringUtils { public static int asciiSizeInBytes(long v) { if (v == 0) { return 1; } if (v == Long.MIN_VALUE) { return 20; } boolean negative = false; if (v < 0) { v = -v; negative = true; } int width = v < 100000000L ? v < 10000L ? v < 100L ? v < 10L ? 1 : 2 : v < 1000L ? 3 : 4 : v < 1000000L ? v < 100000L ? 5 : 6 : v < 10000000L ? 7 : 8 : v < 1000000000000L ? v < 10000000000L ? v < 1000000000L ? 9 : 10 : v < 100000000000L ? 11 : 12 : v < 1000000000000000L ? v < 10000000000000L ? 13 : v < 10000000000000L ? 14 : 15 : v < 100000000000000000L ? v < 10000000000000000L ? 16 : 17 : v < 1000000000000000000L ? 18 : 19; return negative ? width + 1 : width; }  static boolean isEmpty(final String text); static boolean isNotEmpty(final String text); static boolean isBlank(final String text); static boolean isNotBlank(final String text); static int length(final String text); static String[] split(final String str, final String separatorChars); static String lowerWord(CharSequence cs, char c); static int asciiSizeInBytes(long v); static String join(String... strings); static String build(String delimiter, String prefix, String suffix, String... strings); static byte[] utf8Bytes(CharSequence str); static byte[] bytes(CharSequence str); static byte[] bytes(CharSequence str, Charset charset); static String format(String str, Object... arguments); static String readString(InputStream inputStream); static String readString(InputStream inputStream, Charset charset); static String readString(Reader reader); static final String EMPTY; static final String SPACE; static final String LF; static final String CR; static final String COMMA; static final String[] EMPTY_STRING_ARRAY; }