Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7533#discussion_r35395797
--- Diff:
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
@@ -352,6 +351,180 @@ 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) {
+ ByteType byteType = checkByteType(getByte(bytePos));
+ if (ByteType.FIRSTBYTE == byteType || ByteType.SINGLEBYTECHAR ==
byteType) {
+ return bytePos;
+ }
+ bytePos--;
+ }
+ throw new RuntimeException("Invalid UTF8 string: " + toString());
+ }
+
+ // Locate to the start position in byte for a given code point
+ private int firstByteIndex(int startByteIndex, int startPointIndex, int
codePoint) {
+ int i = startByteIndex; // position in byte
+ int c = startPointIndex; // 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(0, 0, 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) {
+ // Empty string always match
+ if (v.numBytes == 0) {
+ return startCodePoint;
+ }
+ return lastIndexOfInByte(v, lastByteIndex(startCodePoint));
+ }
+
+ private int lastIndexOfInByte(UTF8String v, int fromIndexInByte) {
+ if (numBytes == 0) {
+ return -1;
+ }
+ do {
+ int startByteIndex = fromIndexInByte - v.numBytes + 1;
+ if (startByteIndex < 0 ) {
+ return -1;
+ }
+ if (ByteArrayMethods.arrayEquals(
+ base, offset + startByteIndex, v.base, v.offset,
v.numBytes)) {
+ int count = 0; // count from right most to the match end in byte.
+ while (startByteIndex >= 0) {
+ count++;
+ startByteIndex = firstOfCurrentCodePoint(startByteIndex) - 1;
+ }
+ return count - 1;
+ }
+ fromIndexInByte = firstOfCurrentCodePoint(fromIndexInByte) - 1;
+ } while (fromIndexInByte >= 0);
+ return -1;
+ }
+
+ /**
+ * Finds the n-th last index within a String.
+ * This method uses {@link String#lastIndexOf(String)}.</p>
+ *
+ * @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
+ */
+ protected int lastOrdinalIndexOf(
+ UTF8String searchStr,
+ int ordinal) {
+ return doOrdinalIndexOf(searchStr, 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 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
+ */
+ protected int ordinalIndexOf(
+ UTF8String searchStr,
+ int ordinal) {
+ return doOrdinalIndexOf(searchStr, ordinal, false);
+ }
+
+ private int doOrdinalIndexOf(
+ UTF8String searchStr,
+ int ordinal,
+ boolean lastIndex) {
+ if (ordinal <= 0) {
+ return -1;
+ }
+ if (searchStr.numBytes == 0) {
+ return lastIndex ? numChars() : 0;
+ }
+ int found = 0;
+ int index = lastIndex ? numBytes : -1;
+ do {
+ if (lastIndex) {
+ index = lastIndexOfInByte(searchStr, index - 1);
+ } else {
+ index = indexOf(searchStr, index + 1);
+ }
+ if (index < 0) {
+ return index;
+ }
+ found += 1;
+ } while (found < ordinal);
+ return index;
+ }
+ /**
+ * Returns the substring from string str before count occurrences of the
delimiter delim.
+ * If count is positive, everything the left of the final delimiter
(counting from left) is
+ * returned. If count is negative, every to the right of the final
delimiter (counting from the
+ * right) is returned. substring_index performs a case-sensitive match
when searching for delim.
+ */
+ public UTF8String subStringIndex(UTF8String delim, int count) {
+ if (delim == null) {
--- End diff --
We dont' need to check the null value here, as it's done in the expression
side.
---
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]