public Text evaluate(Text s) {
    if (s == null) {
      return null;
    }

    // set() will only allocate memory if the buffer of result is smaller than
    // s.getLength() and will never resize the buffer down.
    result.set(s);

    // Now do an in-place reversal in result.getBytes(). First, reverse every
    // character, then reverse the whole string.
    byte[] data = result.getBytes();
    int prev = 0; // The index where the current char starts
    for (int i = 1; i < result.getLength(); i++) {
      if (GenericUDFUtils.isUtfStartByte(data[i])) {
        reverse(data, prev, i - 1);
        prev = i;
      }
    }
    reverse(data, prev, result.getLength() - 1);
    reverse(data, 0, result.getLength() - 1);

    return result;
  }
Reverse a portion of an array in-place.

@param arr
         The array where the data will be reversed.
@param first
         The beginning of the portion (inclusive).
@param last
         The end of the portion (inclusive).
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFReverse.java