@Test public void indexOf() { assertEquals(0, empty.indexOf(empty, 0)); assertEquals(-1, empty.indexOf(fromString("l"), 0)); assertEquals(0, fromString("hello").indexOf(empty, 0)); assertEquals(2, fromString("hello").indexOf(fromString("l"), 0)); assertEquals(3, fromString("hello").indexOf(fromString("l"), 3)); assertEquals(-1, fromString("hello").indexOf(fromString("a"), 0)); assertEquals(2, fromString("hello").indexOf(fromString("ll"), 0)); assertEquals(-1, fromString("hello").indexOf(fromString("ll"), 4)); assertEquals(1, fromString("数据砖头").indexOf(fromString("据砖"), 0)); assertEquals(-1, fromString("数据砖头").indexOf(fromString("数"), 3)); assertEquals(0, fromString("数据砖头").indexOf(fromString("数"), 0)); assertEquals(3, fromString("数据砖头").indexOf(fromString("头"), 0)); }
public int indexOf(BinaryString str, int fromIndex) { ensureMaterialized(); str.ensureMaterialized(); if (str.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { int byteIdx = 0; int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < fromIndex) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + str.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, str.segments, str.offset, str.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfMultiSegs(str, fromIndex); } }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public int indexOf(BinaryString str, int fromIndex) { ensureMaterialized(); str.ensureMaterialized(); if (str.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { int byteIdx = 0; int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < fromIndex) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + str.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, str.segments, str.offset, str.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfMultiSegs(str, fromIndex); } } }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public int indexOf(BinaryString str, int fromIndex) { ensureMaterialized(); str.ensureMaterialized(); if (str.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { int byteIdx = 0; int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < fromIndex) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + str.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, str.segments, str.offset, str.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfMultiSegs(str, fromIndex); } } BinaryString(); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes); private  BinaryString(String javaObject); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes, String javaObject); }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public int indexOf(BinaryString str, int fromIndex) { ensureMaterialized(); str.ensureMaterialized(); if (str.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { int byteIdx = 0; int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < fromIndex) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + str.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, str.segments, str.offset, str.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfMultiSegs(str, fromIndex); } } BinaryString(); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes); private  BinaryString(String javaObject); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes, String javaObject); static BinaryString fromAddress(
			MemorySegment[] segments, int offset, int numBytes); static BinaryString fromString(String str); static BinaryString fromBytes(byte[] bytes); static BinaryString fromBytes(byte[] bytes, int offset, int numBytes); static BinaryString blankString(int length); int numChars(); byte byteAt(int index); byte[] getBytes(); @Override boolean equals(Object o); @Override String toString(); @Override void materialize(); BinaryString copy(); @Override int compareTo(@Nonnull BinaryString other); BinaryString substring(int beginIndex, int endIndex); boolean contains(final BinaryString s); boolean startsWith(final BinaryString prefix); boolean endsWith(final BinaryString suffix); BinaryString trim(); int indexOf(BinaryString str, int fromIndex); BinaryString toUpperCase(); BinaryString toLowerCase(); }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public int indexOf(BinaryString str, int fromIndex) { ensureMaterialized(); str.ensureMaterialized(); if (str.sizeInBytes == 0) { return 0; } if (inFirstSegment()) { int byteIdx = 0; int charIdx = 0; while (byteIdx < sizeInBytes && charIdx < fromIndex) { byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } do { if (byteIdx + str.sizeInBytes > sizeInBytes) { return -1; } if (SegmentsUtil.equals(segments, offset + byteIdx, str.segments, str.offset, str.sizeInBytes)) { return charIdx; } byteIdx += numBytesForFirstByte(getByteOneSegment(byteIdx)); charIdx++; } while (byteIdx < sizeInBytes); return -1; } else { return indexOfMultiSegs(str, fromIndex); } } BinaryString(); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes); private  BinaryString(String javaObject); private  BinaryString(MemorySegment[] segments, int offset, int sizeInBytes, String javaObject); static BinaryString fromAddress(
			MemorySegment[] segments, int offset, int numBytes); static BinaryString fromString(String str); static BinaryString fromBytes(byte[] bytes); static BinaryString fromBytes(byte[] bytes, int offset, int numBytes); static BinaryString blankString(int length); int numChars(); byte byteAt(int index); byte[] getBytes(); @Override boolean equals(Object o); @Override String toString(); @Override void materialize(); BinaryString copy(); @Override int compareTo(@Nonnull BinaryString other); BinaryString substring(int beginIndex, int endIndex); boolean contains(final BinaryString s); boolean startsWith(final BinaryString prefix); boolean endsWith(final BinaryString suffix); BinaryString trim(); int indexOf(BinaryString str, int fromIndex); BinaryString toUpperCase(); BinaryString toLowerCase(); static final BinaryString EMPTY_UTF8; }