Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7533#discussion_r35333750
--- Diff:
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
@@ -352,6 +351,196 @@ public int indexOf(UTF8String v, int start) {
return -1;
}
+ private enum ByteType {FIRSTBYTE, MIDBYTE, SINGLEBYTECHAR};
+
+ private ByteType checkByteType(Byte b) {
+ int firstTwoBits = (b >>> 6) & 0x03;
+ if (firstTwoBits == 3) {
+ return ByteType.FIRSTBYTE;
+ } else if (firstTwoBits == 2) {
+ return ByteType.MIDBYTE;
+ } else {
+ return ByteType.SINGLEBYTECHAR;
+ }
+ }
+
+ /**
+ * Return the first byte position for a given byte which shared the same
code point.
+ * @param bytePos any byte within the code point
+ * @return the first byte position of a given code point, throw
exception if not a valid UTF8 str
+ */
+ private int firstOfCurrentCodePoint(int bytePos) {
+ while (bytePos >= 0) {
+ if (ByteType.FIRSTBYTE == checkByteType(getByte(bytePos))
+ || ByteType.SINGLEBYTECHAR == checkByteType(getByte(bytePos))) {
+ return bytePos;
+ }
+ bytePos--;
+ }
+ throw new RuntimeException("Invalid UTF8 string");
+ }
+
+ // Locate to the start position in byte for a given code point
+ private int firstByteIndex(int codePoint) {
+ int i = 0; // position in byte
+ int c = 0; // position in character
+ while (i < numBytes && c < codePoint) {
+ i += numBytesForFirstByte(getByte(i));
+ c += 1;
+ }
+ if (i > numBytes) {
+ throw new StringIndexOutOfBoundsException(codePoint);
+ }
+ return i;
+ }
+
+ // Locate to the last position in byte for a given code point
+ private int lastByteIndex(int codePoint) {
+ int i = firstByteIndex(codePoint);
+ return i + numBytesForFirstByte(getByte(i)) - 1;
+ }
+
+ /**
+ * Returns the index within this string of the last occurrence of the
+ * specified substring, searching backward starting at the specified
index.
+ * @param v the substring to search for.
+ * @param startCodePoint the index to start search from
+ * @return the index of the last occurrence of the specified substring,
+ * searching backward from the specified index,
+ * or {@code -1} if there is no such occurrence.
+ */
+ public int lastIndexOf(UTF8String v, int startCodePoint) {
+ return lastIndexOf(v, v.numChars(), startCodePoint);
+ }
+ public int lastIndexOf(UTF8String v, int vNumChars, int startCodePoint) {
+ if (v.numBytes == 0) {
+ return 0;
+ }
+ if (numBytes == 0) {
+ return -1;
+ }
+ int fromIndexEnd = lastByteIndex(startCodePoint);
+ do {
+ if (fromIndexEnd - v.numBytes + 1 < 0 ) {
+ return -1;
+ }
+ if (ByteArrayMethods.arrayEquals(
+ base, offset + fromIndexEnd - v.numBytes + 1, v.base, v.offset,
v.numBytes)) {
+ int count = 0; // count from right most to the match end in byte.
+ while (fromIndexEnd >= 0) {
+ count++;
+ fromIndexEnd = firstOfCurrentCodePoint(fromIndexEnd) - 1;
+ }
+ return count - vNumChars;
+ }
+ fromIndexEnd = firstOfCurrentCodePoint(fromIndexEnd) - 1;
+ } while (fromIndexEnd >= 0);
+ return -1;
+ }
+
+ /**
+ * Finds the n-th last index within a String.
+ * This method uses {@link String#lastIndexOf(String)}.</p>
+ *
+ * @param str the String to check, may be null
+ * @param searchStr the String to find, may be null
+ * @param ordinal the n-th last <code>searchStr</code> to find
+ * @return the n-th last index of the search String,
+ * <code>-1</code> if no match or <code>null</code> string input
+ */
+ public static int lastOrdinalIndexOf(
+ UTF8String str,
+ UTF8String searchStr,
+ int ordinal) {
+ if (str == null || searchStr == null) {
+ return -1;
+ }
+ return doOrdinalIndexOf(str, searchStr, searchStr.numChars(), ordinal,
true);
+ }
+
+ /**
+ * Finds the n-th index within a String, handling <code>null</code>.
+ * A <code>null</code> String will return <code>-1</code>
+ *
+ * @param str the String to check, may be null
+ * @param searchStr the String to find, may be null
+ * @param ordinal the n-th <code>searchStr</code> to find
+ * @return the n-th index of the search String,
+ * <code>-1</code> if no match or <code>null</code> string input
+ */
+ public static int ordinalIndexOf(
+ UTF8String str,
+ UTF8String searchStr,
+ int ordinal) {
+ if (str == null || searchStr == null) {
+ return -1;
+ }
+ return doOrdinalIndexOf(str, searchStr, searchStr.numChars(), ordinal,
false);
+ }
+
+ private static int doOrdinalIndexOf(
+ UTF8String str,
+ UTF8String searchStr,
+ int searchStrNumChars,
+ int ordinal,
+ boolean lastIndex) {
+ if (str == null || searchStr == null || ordinal <= 0) {
+ return -1;
+ }
+ // Only calc numChars when lastIndex == true sicnc the calculation is
expensive
+ int strNumChars = 0;
+ if (lastIndex) {
+ strNumChars = str.numChars();
+ }
+ if (searchStr.numBytes == 0) {
+ return lastIndex ? strNumChars : 0;
+ }
+ int found = 0;
+ int index = lastIndex ? strNumChars : 0;
+ do {
+ if (lastIndex) {
+ index = str.lastIndexOf(searchStr, searchStrNumChars, index - 1);
--- End diff --
It's will be great if we can share some code, but do we really need to
convert to the char position? I am wondering if we can search the delimiter
bytes directly, which seems more straightforward.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]