Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/io/airlift/slice/SliceUtf8.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,56 @@ public static Slice toLowerCase(Slice utf8)
return translateCodePoints(utf8, LOWER_CODE_POINTS);
}

public static Slice toTitleCase(Slice utf8)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add documentation for how this implementation works and maybe reference which rule set this comes from.

{
int length = utf8.length();
Slice newUtf8 = Slices.allocate(length);

int position = 0;
int upperPosition = 0;
boolean upperNext = true;
while (position < length) {
int codePoint = tryGetCodePointAt(utf8, position);
if (codePoint >= 0) {
int upperCodePoint = LOWER_CODE_POINTS[codePoint];
if (upperNext) {
upperCodePoint = UPPER_CODE_POINTS[codePoint];
upperNext = false;
}

if (WHITESPACE_CODE_POINTS[codePoint]) {
upperNext = true;
}

// grow slice if necessary
int nextUpperPosition = upperPosition + lengthOfCodePoint(upperCodePoint);
if (nextUpperPosition > length) {
newUtf8 = Slices.ensureSize(newUtf8, nextUpperPosition);
}

// write new byte
setCodePointAt(upperCodePoint, newUtf8, upperPosition);

position += lengthOfCodePoint(codePoint);
upperPosition = nextUpperPosition;
}
else {
int skipLength = -codePoint;

// grow slice if necessary
int nextUpperPosition = upperPosition + skipLength;
if (nextUpperPosition > length) {
newUtf8 = Slices.ensureSize(newUtf8, nextUpperPosition);
}

copyUtf8SequenceUnsafe(utf8, position, newUtf8, upperPosition, skipLength);
position += skipLength;
upperPosition = nextUpperPosition;
}
}
return newUtf8.slice(0, upperPosition);
}

private static Slice translateCodePoints(Slice utf8, int[] codePointTranslationMap)
{
int length = utf8.length();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/airlift/slice/TestSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOfByteArray;
import static io.airlift.slice.SliceUtf8.toTitleCase;
import static io.airlift.slice.Slices.EMPTY_SLICE;
import static io.airlift.slice.Slices.utf8Slice;
import static java.lang.Double.doubleToLongBits;
Expand Down Expand Up @@ -196,6 +197,17 @@ public void testUtf8Conversion()
assertThat(utf8Slice(s).toStringUtf8()).isEqualTo(s);
}

@Test
public void testUtf8TitleCaseConversion()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe copy some of the text from the toUpper tests.

{
String s = "apple \u2603 snowman";
Slice slice = Slices.copiedBuffer(s, UTF_8);

assertThat(toTitleCase(utf8Slice(s))).isEqualTo(toTitleCase(slice));
assertThat(toTitleCase(slice).toStringUtf8()).isEqualTo("Apple \u2603 Snowman");
assertThat(utf8Slice(s).toStringUtf8()).isEqualTo(s);
}

@SuppressWarnings("CharUsedInArithmeticContext")
private static void assertToStrings(Slice slice, int index)
{
Expand Down