diff options
author | Martin Polden <mpolden@mpolden.no> | 2020-10-05 11:09:33 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2020-10-05 11:10:03 +0200 |
commit | 17246bdb35ff824c4fa424134e4c5cd7732f0b14 (patch) | |
tree | 26d920b3641159b8a86cba78465e287a592af434 /vespajlib/src/main/java/com/yahoo/text | |
parent | ea9dc5a16e3caed2c395c522dbb9a2a94006ce8f (diff) |
Remove custom Utf8.toBytes implementation
`String` optimizations have caught up.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/text')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/Utf8.java | 28 |
1 files changed, 5 insertions, 23 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/Utf8.java b/vespajlib/src/main/java/com/yahoo/text/Utf8.java index a8a0adf1a7d..b81f0447d04 100644 --- a/vespajlib/src/main/java/com/yahoo/text/Utf8.java +++ b/vespajlib/src/main/java/com/yahoo/text/Utf8.java @@ -106,45 +106,27 @@ public final class Utf8 { } /** - * Will try an optimistic approach to utf8 encoding. - * That is 4.6x faster that the brute encode for ascii, not accounting for reduced memory footprint and GC. + * Encode a UTF-8 string. * * @param string The string to encode. * @return Utf8 encoded array */ public static byte[] toBytes(String string) { - byte [] utf8 = toBytesAscii(string); - return utf8 != null ? utf8 : string.getBytes(StandardCharsets.UTF_8); + // This is just wrapper for String::getBytes. Pre-Java 9 this had an more efficient approach for ASCII-only strings. + return string.getBytes(StandardCharsets.UTF_8); } /** * Decode a UTF-8 string. * - * @param utf8 The string to encode. + * @param utf8 The bytes to decode. * @return Utf8 encoded array */ public static String toString(byte [] utf8) { - // This is just wrapper for String::new now. Pre-Java 9 this had an more efficient approach for ASCII strings. + // This is just wrapper for String::new. Pre-Java 9 this had an more efficient approach for ASCII-onlu strings. return new String(utf8, StandardCharsets.UTF_8); } /** - * If String is purely ascii 7bit it will encode it as a byte array. - * @param str The string to encode - * @return Utf8 encoded array - */ - private static byte[] toBytesAscii(final CharSequence str) { - byte [] utf8 = new byte[str.length()]; - for (int i=0; i < utf8.length; i++) { - char c = str.charAt(i); - if ((c < 0) || (c >= 0x80)) { - return null; - } - utf8[i] = (byte)c; - } - return utf8; - } - - /** * Utility method as toBytes(String). * * @param str |