@Test public void testCalculateUTF8Offset() throws Exception { String tmp, padding = "padding", data = "零一二三四五六七八九", trailing = "trailing"; byte[] bytes = (padding + data + trailing).getBytes(); int ret, offset = padding.getBytes().length, length = data.getBytes().length; tmp = padding; for (int i = 0; i < data.length(); ++i) { ret = StringUtil.calculateUTF8Offset(bytes, offset, length, SortOrder.ASC, i); assertEquals(tmp.getBytes().length, ret); tmp = tmp + data.charAt(i); } for (int i = data.length(); i < data.length() + 10; ++i) { ret = StringUtil.calculateUTF8Offset(bytes, offset, length, SortOrder.ASC, i); assertEquals(-1, ret); } for (int i = -data.length() - 10; i < -data.length(); ++i) { ret = StringUtil.calculateUTF8Offset(bytes, offset, length, SortOrder.ASC, i); assertEquals(-1, ret); } tmp = padding; for (int i = -data.length(); i <= -1; ++i) { ret = StringUtil.calculateUTF8Offset(bytes, offset, length, SortOrder.ASC, i); assertEquals("i=" + i, tmp.getBytes().length, ret); tmp = tmp + data.charAt(i + data.length()); } }
public static int calculateUTF8Offset(byte[] bytes, int offset, int length, SortOrder sortOrder, int offsetInStr) { if (offsetInStr == 0) return offset; int ret, range = offset + length; if (offsetInStr > 0) { ret = offset; while (offsetInStr > 0) { ret = calculateNextCharOffset(bytes, ret, range, sortOrder); if (ret == -1) return -1; --offsetInStr; } } else { ret = offset + length; while (offsetInStr < 0) { ret = calculatePreCharOffset(bytes, ret, offset, sortOrder); if (ret < offset) return -1; ++offsetInStr; } } return ret; }
StringUtil { public static int calculateUTF8Offset(byte[] bytes, int offset, int length, SortOrder sortOrder, int offsetInStr) { if (offsetInStr == 0) return offset; int ret, range = offset + length; if (offsetInStr > 0) { ret = offset; while (offsetInStr > 0) { ret = calculateNextCharOffset(bytes, ret, range, sortOrder); if (ret == -1) return -1; --offsetInStr; } } else { ret = offset + length; while (offsetInStr < 0) { ret = calculatePreCharOffset(bytes, ret, offset, sortOrder); if (ret < offset) return -1; ++offsetInStr; } } return ret; } }
StringUtil { public static int calculateUTF8Offset(byte[] bytes, int offset, int length, SortOrder sortOrder, int offsetInStr) { if (offsetInStr == 0) return offset; int ret, range = offset + length; if (offsetInStr > 0) { ret = offset; while (offsetInStr > 0) { ret = calculateNextCharOffset(bytes, ret, range, sortOrder); if (ret == -1) return -1; --offsetInStr; } } else { ret = offset + length; while (offsetInStr < 0) { ret = calculatePreCharOffset(bytes, ret, offset, sortOrder); if (ret < offset) return -1; ++offsetInStr; } } return ret; } private  StringUtil(); }
StringUtil { public static int calculateUTF8Offset(byte[] bytes, int offset, int length, SortOrder sortOrder, int offsetInStr) { if (offsetInStr == 0) return offset; int ret, range = offset + length; if (offsetInStr > 0) { ret = offset; while (offsetInStr > 0) { ret = calculateNextCharOffset(bytes, ret, range, sortOrder); if (ret == -1) return -1; --offsetInStr; } } else { ret = offset + length; while (offsetInStr < 0) { ret = calculatePreCharOffset(bytes, ret, offset, sortOrder); if (ret < offset) return -1; ++offsetInStr; } } return ret; } private  StringUtil(); static String replaceChar(String value, char ch, CharSequence replacement); static String replace(String s, String[] src, String[] target); static int getBytesInChar(byte b, SortOrder sortOrder); static int calculateUTF8Length(byte[] bytes, int offset, int length, SortOrder sortOrder); static int calculateUTF8Offset(byte[] bytes, int offset, int length,
            SortOrder sortOrder, int offsetInStr); static int getByteLengthForUtf8SubStr(byte[] bytes, int offset, int length, SortOrder sortOrder); static boolean hasMultiByteChars(String s); static int getFirstNonBlankCharIdxFromStart(byte[] string, int offset, int length, SortOrder sortOrder); static int getFirstNonBlankCharIdxFromEnd(byte[] string, int offset, int length, SortOrder sortOrder); static byte[] toBytes(String input); static String escapeLike(String s); static int getUnpaddedCharLength(byte[] b, int offset, int length, SortOrder sortOrder); static byte[] padChar(byte[] value, Integer byteSize); static boolean equals(StringBuilder b1, StringBuilder b2); static byte[] lpad(byte[] str, int strOffset, int strLength, byte[] fill, int fillOffset, int fillLength,
        boolean invertFill, int strWithPaddingLen); static void fill(byte[] str, int strFromIdx, int strToIdx, byte[] fillArray, int fillFromIdx, int fillToIdx,
        boolean invertFill); static String escapeStringConstant(String pattern); static String escapeBackslash(String input); }
StringUtil { public static int calculateUTF8Offset(byte[] bytes, int offset, int length, SortOrder sortOrder, int offsetInStr) { if (offsetInStr == 0) return offset; int ret, range = offset + length; if (offsetInStr > 0) { ret = offset; while (offsetInStr > 0) { ret = calculateNextCharOffset(bytes, ret, range, sortOrder); if (ret == -1) return -1; --offsetInStr; } } else { ret = offset + length; while (offsetInStr < 0) { ret = calculatePreCharOffset(bytes, ret, offset, sortOrder); if (ret < offset) return -1; ++offsetInStr; } } return ret; } private  StringUtil(); static String replaceChar(String value, char ch, CharSequence replacement); static String replace(String s, String[] src, String[] target); static int getBytesInChar(byte b, SortOrder sortOrder); static int calculateUTF8Length(byte[] bytes, int offset, int length, SortOrder sortOrder); static int calculateUTF8Offset(byte[] bytes, int offset, int length,
            SortOrder sortOrder, int offsetInStr); static int getByteLengthForUtf8SubStr(byte[] bytes, int offset, int length, SortOrder sortOrder); static boolean hasMultiByteChars(String s); static int getFirstNonBlankCharIdxFromStart(byte[] string, int offset, int length, SortOrder sortOrder); static int getFirstNonBlankCharIdxFromEnd(byte[] string, int offset, int length, SortOrder sortOrder); static byte[] toBytes(String input); static String escapeLike(String s); static int getUnpaddedCharLength(byte[] b, int offset, int length, SortOrder sortOrder); static byte[] padChar(byte[] value, Integer byteSize); static boolean equals(StringBuilder b1, StringBuilder b2); static byte[] lpad(byte[] str, int strOffset, int strLength, byte[] fill, int fillOffset, int fillLength,
        boolean invertFill, int strWithPaddingLen); static void fill(byte[] str, int strFromIdx, int strToIdx, byte[] fillArray, int fillFromIdx, int fillToIdx,
        boolean invertFill); static String escapeStringConstant(String pattern); static String escapeBackslash(String input); static final String EMPTY_STRING; static final byte SPACE_UTF8; static final byte INVERTED_SPACE_UTF8; final static char SINGLE_CHAR_WILDCARD; final static char SINGLE_CHAR_LIKE; final static char MULTI_CHAR_WILDCARD; final static char MULTI_CHAR_LIKE; final static String[] LIKE_ESCAPE_SEQS; final static String[] LIKE_UNESCAPED_SEQS; }