Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7533#discussion_r35181937
--- Diff:
unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java ---
@@ -352,6 +352,69 @@ 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");
+ }
+
+ private int indexEnd(int startCodePoint) {
+ int i = numBytes -1; // position in byte
+ int c = numChars() - 1; // position in character
--- End diff --
`numChars()` is actually very expensive. as the `indexEnd` (or
`lastIndexOf`) will be called multiple times for the `substring_index`, we need
to think about how to optimize that by removing the function calls.
---
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]