@Test public void substring() { assertEquals(empty, fromString("hello").substring(0, 0)); assertEquals(fromString("el"), fromString("hello").substring(1, 3)); assertEquals(fromString("数"), fromString("数据砖头").substring(0, 1)); assertEquals(fromString("据砖"), fromString("数据砖头").substring(1, 3)); assertEquals(fromString("头"), fromString("数据砖头").substring(3, 5)); assertEquals(fromString("ߵ梷"), fromString("ߵ梷").substring(0, 2)); }
public BinaryString substring(int beginIndex, int endIndex) { ensureMaterialized(); if (endIndex <= beginIndex || beginIndex >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < beginIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < endIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringMultiSegs(beginIndex, endIndex); } }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public BinaryString substring(int beginIndex, int endIndex) { ensureMaterialized(); if (endIndex <= beginIndex || beginIndex >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < beginIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < endIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringMultiSegs(beginIndex, endIndex); } } }
BinaryString extends LazyBinaryFormat<String> implements Comparable<BinaryString> { public BinaryString substring(int beginIndex, int endIndex) { ensureMaterialized(); if (endIndex <= beginIndex || beginIndex >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < beginIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < endIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringMultiSegs(beginIndex, endIndex); } } 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 BinaryString substring(int beginIndex, int endIndex) { ensureMaterialized(); if (endIndex <= beginIndex || beginIndex >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < beginIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < endIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringMultiSegs(beginIndex, endIndex); } } 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 BinaryString substring(int beginIndex, int endIndex) { ensureMaterialized(); if (endIndex <= beginIndex || beginIndex >= sizeInBytes) { return EMPTY_UTF8; } if (inFirstSegment()) { MemorySegment segment = segments[0]; int i = 0; int c = 0; while (i < sizeInBytes && c < beginIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } int j = i; while (i < sizeInBytes && c < endIndex) { i += numBytesForFirstByte(segment.get(i + offset)); c += 1; } if (i > j) { byte[] bytes = new byte[i - j]; segment.get(offset + j, bytes, 0, i - j); return fromBytes(bytes); } else { return EMPTY_UTF8; } } else { return substringMultiSegs(beginIndex, endIndex); } } 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; }