Java Code Examples for org.apache.lucene.util.UnicodeUtil#UTF8toUTF16

The following examples show how to use org.apache.lucene.util.UnicodeUtil#UTF8toUTF16 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowerFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef evaluate(Input<Object>... args) {
    Object stringValue = args[0].value();
    if (stringValue == null) {
        return null;
    }

    BytesRef inputByteRef = BytesRefs.toBytesRef(stringValue);

    char[] ref = new char[inputByteRef.length];
    int len = UnicodeUtil.UTF8toUTF16(inputByteRef.bytes, inputByteRef.offset, inputByteRef.length, ref);
    charUtils.toLowerCase(ref, 0, len);

    byte[] res = new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * len];
    len = UnicodeUtil.UTF16toUTF8(ref, 0, len, res);
    return new BytesRef(res, 0, len);
}
 
Example 2
Source File: UpperFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef evaluate(Input<Object>... args) {
    Object stringValue = args[0].value();
    if (stringValue == null) {
        return null;
    }

    BytesRef inputByteRef = BytesRefs.toBytesRef(stringValue);

    char[] ref = new char[inputByteRef.length];
    int len = UnicodeUtil.UTF8toUTF16(inputByteRef.bytes, inputByteRef.offset, inputByteRef.length, ref);
    charUtils.toUpperCase(ref, 0, len);

    byte[] res = new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * len];
    len = UnicodeUtil.UTF16toUTF8(ref, 0, len, res);
    return new BytesRef(res, 0, len);
}
 
Example 3
Source File: DaciukMihovAutomatonBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Build a minimal, deterministic automaton from a sorted list of {@link BytesRef} representing
 * strings in UTF-8. These strings must be binary-sorted.
 */
public static Automaton build(Collection<BytesRef> input) {
  final DaciukMihovAutomatonBuilder builder = new DaciukMihovAutomatonBuilder();
  
  char[] chars = new char[0];
  CharsRef ref = new CharsRef();
  for (BytesRef b : input) {
    chars = ArrayUtil.grow(chars, b.length);
    final int len = UnicodeUtil.UTF8toUTF16(b, chars);
    ref.chars = chars;
    ref.length = len;
    builder.add(ref);
  }
  
  Automaton.Builder a = new Automaton.Builder();
  convert(a,
      builder.complete(), 
      new IdentityHashMap<State,Integer>());

  return a.finish();
}
 
Example 4
Source File: PartitionName.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Read utf8 bytes for bwc, with 0 as `null` indicator
 */
private static String readValueFrom(StreamInput in) throws IOException {
    int length = in.readVInt() - 1;
    if (length == -1) {
        return null;
    }
    if (length == 0) {
        return "";
    }
    byte[] bytes = new byte[length];
    in.readBytes(bytes, 0, length);
    char[] chars = new char[length];
    int len = UnicodeUtil.UTF8toUTF16(bytes, 0, length, chars);
    return new String(chars, 0, len);
}
 
Example 5
Source File: LineContext.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
String sourceAsString() {
    if (rawSource != null) {
        char[] chars = new char[rawSource.length];
        int len = UnicodeUtil.UTF8toUTF16(rawSource, 0, rawSource.length, chars);
        return new String(chars, 0, len);
    }
    return null;
}
 
Example 6
Source File: RegexMatcher.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private static void UTF8toUTF16(BytesRef bytes, CharsRef charsRef) {
    if (charsRef.chars.length < bytes.length) {
        charsRef.chars = new char[bytes.length];
    }
    charsRef.length = UnicodeUtil.UTF8toUTF16(bytes, charsRef.chars);
}