@Test
  // Test basic right trim and truncate to vector.
  public void testRightTrimAndTruncateBytesSlice()  {
      // This first section repeats the tests of testRightTrimWithOffset with a large maxLength parameter.
      // (i.e. too large to have an effect).
      int largeMaxLength = 100;

      int resultLen;
      // Nothing to trim (ASCII).
      Assert.assertTrue(StringExpr.characterCount(blue, 0, blue.length) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blue, 0, blue.length, largeMaxLength);
      Assert.assertTrue(resultLen == blue.length);
      Assert.assertTrue(StringExpr.characterCount(blue, 0, resultLen) == 4);

      Assert.assertTrue(StringExpr.characterCount(redgreen, 0, redgreen.length) == 8);
      resultLen = StringExpr.rightTrimAndTruncate(redgreen, 0, redgreen.length, largeMaxLength);
      Assert.assertTrue(resultLen == redgreen.length);

      Assert.assertTrue(StringExpr.characterCount(ascii_sentence, 0, ascii_sentence.length) == 31);
      resultLen = StringExpr.rightTrimAndTruncate(ascii_sentence, 0, ascii_sentence.length, largeMaxLength);
      Assert.assertTrue(resultLen == ascii_sentence.length);

      Assert.assertTrue(StringExpr.characterCount(blanksLeft, 0, blanksLeft.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksLeft, 0, blanksLeft.length, largeMaxLength);
      Assert.assertTrue(resultLen == blanksLeft.length);

      // Simple trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, blanksRight.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 0, blanksRight.length, largeMaxLength);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, resultLen) == 3);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, blanksBoth.length) == 7);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 0, blanksBoth.length, largeMaxLength);
      Assert.assertTrue(resultLen == 5);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, resultLen) == 5);
     
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, blankString.length) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 0, blankString.length, largeMaxLength);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, blankRanges.length) == 30);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 0, blankRanges.length, largeMaxLength);
      Assert.assertTrue(resultLen == blankRanges.length - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, resultLen) == 26);

      // Offset trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, blanksRight.length - 1) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 1, blanksRight.length - 1, largeMaxLength);
      Assert.assertTrue(resultLen == 2);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, resultLen) == 2);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, blanksBoth.length - 4) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 4, blanksBoth.length - 4, largeMaxLength);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, resultLen) == 1);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, blanksBoth.length - 5) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 5, blanksBoth.length -5, largeMaxLength);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankString, 1, blankString.length - 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 1, blankString.length - 1, largeMaxLength);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 1, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 4, blankRanges.length - 4) == 26);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 4, blankRanges.length - 4, largeMaxLength);
      Assert.assertTrue(resultLen == blankRanges.length - 4 -4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 4, resultLen) == 22);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, blankRanges.length - 6) == 24);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 6, blankRanges.length- 6, largeMaxLength);
      Assert.assertTrue(resultLen == blankRanges.length - 6 - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, resultLen) == 20);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, blankRanges.length - 7) == 23);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, blankRanges.length - 7, largeMaxLength);
      Assert.assertTrue(resultLen == blankRanges.length - 7 - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 19);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, 8 - 7) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, 8 - 7, largeMaxLength);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 0);

      // Multi-byte trims.
      byte[] multiByte = new byte[100];

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 4) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 4, largeMaxLength);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 1);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 5) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 5, largeMaxLength);
      Assert.assertTrue(resultLen == 4);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 2);

      addMultiByteCharRightPadded1_3(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 9) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 9, largeMaxLength);
      Assert.assertTrue(resultLen == 8);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 3);

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 1, largeMaxLength);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 0);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 2) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 2, largeMaxLength);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, resultLen) == 1);

      byte[] sentenceOne = new byte[100];
      int sentenceOneLen = addMultiByteCharSentenceOne(sentenceOne, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOneLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen - 3) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen - 3, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOneLen - 3);

      byte[] sentenceTwo = new byte[100];
      int sentenceTwoLen = addMultiByteCharSentenceTwo(sentenceTwo, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen- 5) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen - 5, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoLen - 5);

      int start;

      // Left pad longer strings with multi-byte characters.
      byte[] sentenceOnePaddedLeft = new byte[100];
      start = addPads(sentenceOnePaddedLeft, 0, 3);
      int sentenceOnePaddedLeftLen = addMultiByteCharSentenceOne(sentenceOnePaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen) == 3 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOnePaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3) == 3 + 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOnePaddedLeftLen - 3);

      byte[] sentenceTwoPaddedLeft = new byte[100];
      start = addPads(sentenceTwoPaddedLeft, 0, 2);
      int sentenceTwoPaddedLeftLen = addMultiByteCharSentenceTwo(sentenceTwoPaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen) == 2 + 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoPaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5) == 2 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoPaddedLeftLen - 5);

      // Right pad longer strings with multi-byte characters.
      byte[] sentenceOnePaddedRight = new byte[100];
      start = addMultiByteCharSentenceOne(sentenceOnePaddedRight, 0);
      int sentenceOnePaddedRightLen = addPads(sentenceOnePaddedRight, start, 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen) == 10 + 4);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOnePaddedRightLen - 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceOnePaddedRightLen - 3 - 4);

      byte[] sentenceTwoPaddedRight = new byte[100];
      start = addMultiByteCharSentenceTwo(sentenceTwoPaddedRight, 0);
      int sentenceTwoPaddedRightLen = addPads(sentenceTwoPaddedRight, start, 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen) == 13 + 1);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoPaddedRightLen - 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceTwoPaddedRightLen - 5 - 1);

      // Multi-byte characters with blank ranges.
      byte[] sentenceBlankRanges = new byte[100];
      int sentenceBlankRangesLen = addMultiByteCharSentenceBlankRanges(sentenceBlankRanges, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen) == 17);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceBlankRangesLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3) == 16);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3, largeMaxLength);
      Assert.assertTrue(resultLen == sentenceBlankRangesLen - 3);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, 17) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 7, 17, largeMaxLength);
      Assert.assertTrue(resultLen == 12);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, resultLen) == 8);

      // This next section repeats the tests of testRightTrimWithOffset with a maxLength parameter that is
      // exactly the number of current characters in the string.  This shouldn't affect the trim.

      // Nothing to trim (ASCII).
      Assert.assertTrue(StringExpr.characterCount(blue, 0, blue.length) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blue, 0, blue.length, 4);
      Assert.assertTrue(resultLen == blue.length);
      Assert.assertTrue(StringExpr.characterCount(blue, 0, resultLen) == 4);

      Assert.assertTrue(StringExpr.characterCount(redgreen, 0, redgreen.length) == 8);
      resultLen = StringExpr.rightTrimAndTruncate(redgreen, 0, redgreen.length, 8);
      Assert.assertTrue(resultLen == redgreen.length);

      Assert.assertTrue(StringExpr.characterCount(ascii_sentence, 0, ascii_sentence.length) == 31);
      resultLen = StringExpr.rightTrimAndTruncate(ascii_sentence, 0, ascii_sentence.length, 31);
      Assert.assertTrue(resultLen == ascii_sentence.length);

      Assert.assertTrue(StringExpr.characterCount(blanksLeft, 0, blanksLeft.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksLeft, 0, blanksLeft.length, 5);
      Assert.assertTrue(resultLen == blanksLeft.length);

      // Truncate everything and nothing to trim
      Assert.assertTrue(StringExpr.characterCount(blanksLeft, 0, blanksLeft.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksLeft, 0, blanksLeft.length, 0);
      Assert.assertTrue(resultLen == 0);

      // Simple trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, blanksRight.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 0, blanksRight.length, 5);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, resultLen) == 3);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, blanksBoth.length) == 7);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 0, blanksBoth.length, 7);
      Assert.assertTrue(resultLen == 5);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, resultLen) == 5);
     
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, blankString.length) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 0, blankString.length, 2);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, blankRanges.length) == 30);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 0, blankRanges.length, 30);
      Assert.assertTrue(resultLen == blankRanges.length - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, resultLen) == 26);

      // Offset trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, blanksRight.length - 1) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 1, blanksRight.length - 1, 4);
      Assert.assertTrue(resultLen == 2);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, resultLen) == 2);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, blanksBoth.length - 4) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 4, blanksBoth.length - 4, 3);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, resultLen) == 1);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, blanksBoth.length - 5) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 5, blanksBoth.length -5, 2);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankString, 1, blankString.length - 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 1, blankString.length - 1, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 1, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 4, blankRanges.length - 4) == 26);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 4, blankRanges.length - 4, 26);
      Assert.assertTrue(resultLen == blankRanges.length - 4 -4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 4, resultLen) == 22);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, blankRanges.length - 6) == 24);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 6, blankRanges.length- 6, 24);
      Assert.assertTrue(resultLen == blankRanges.length - 6 - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, resultLen) == 20);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, blankRanges.length - 7) == 23);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, blankRanges.length - 7, 23);
      Assert.assertTrue(resultLen == blankRanges.length - 7 - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 19);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, 8 - 7) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, 8 - 7, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 0);

      // Multi-byte trims.
      multiByte = new byte[100];

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 4) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 4, 2);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 1);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 5) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 5, 3);
      Assert.assertTrue(resultLen == 4);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 2);

      addMultiByteCharRightPadded1_3(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 9) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 9, 4);
      Assert.assertTrue(resultLen == 8);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 3);

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 1, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 0);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 2) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 2, 2);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, resultLen) == 1);

      sentenceOne = new byte[100];
      sentenceOneLen = addMultiByteCharSentenceOne(sentenceOne, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen, 10);
      Assert.assertTrue(resultLen == sentenceOneLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen - 3) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen - 3, 9);
      Assert.assertTrue(resultLen == sentenceOneLen - 3);

      sentenceTwo = new byte[100];
      sentenceTwoLen = addMultiByteCharSentenceTwo(sentenceTwo, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen, 13);
      Assert.assertTrue(resultLen == sentenceTwoLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen- 5) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen - 5, 10);
      Assert.assertTrue(resultLen == sentenceTwoLen - 5);

      // Left pad longer strings with multi-byte characters.
      sentenceOnePaddedLeft = new byte[100];
      start = addPads(sentenceOnePaddedLeft, 0, 3);
      sentenceOnePaddedLeftLen = addMultiByteCharSentenceOne(sentenceOnePaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen) == 3 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen, 3 + 10);
      Assert.assertTrue(resultLen == sentenceOnePaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3) == 3 + 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3, 3 + 9);
      Assert.assertTrue(resultLen == sentenceOnePaddedLeftLen - 3);

      sentenceTwoPaddedLeft = new byte[100];
      start = addPads(sentenceTwoPaddedLeft, 0, 2);
      sentenceTwoPaddedLeftLen = addMultiByteCharSentenceTwo(sentenceTwoPaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen) == 2 + 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen, 2 + 13);
      Assert.assertTrue(resultLen == sentenceTwoPaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5) == 2 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5, 2 + 10);
      Assert.assertTrue(resultLen == sentenceTwoPaddedLeftLen - 5);

      // Right pad longer strings with multi-byte characters.
      sentenceOnePaddedRight = new byte[100];
      start = addMultiByteCharSentenceOne(sentenceOnePaddedRight, 0);
      sentenceOnePaddedRightLen = addPads(sentenceOnePaddedRight, start, 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen) == 10 + 4);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen, 10 + 4);
      Assert.assertTrue(resultLen == sentenceOnePaddedRightLen - 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4, 9);
      Assert.assertTrue(resultLen == sentenceOnePaddedRightLen - 3 - 4);

      sentenceTwoPaddedRight = new byte[100];
      start = addMultiByteCharSentenceTwo(sentenceTwoPaddedRight, 0);
      sentenceTwoPaddedRightLen = addPads(sentenceTwoPaddedRight, start, 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen) == 13 + 1);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen, 13 + 1);
      Assert.assertTrue(resultLen == sentenceTwoPaddedRightLen - 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1, 10);
      Assert.assertTrue(resultLen == sentenceTwoPaddedRightLen - 5 - 1);

      // Multi-byte characters with blank ranges.
      sentenceBlankRanges = new byte[100];
      sentenceBlankRangesLen = addMultiByteCharSentenceBlankRanges(sentenceBlankRanges, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen) == 17);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen, 17);
      Assert.assertTrue(resultLen == sentenceBlankRangesLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3) == 16);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3, 16);
      Assert.assertTrue(resultLen == sentenceBlankRangesLen - 3);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, 17) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 7, 17, largeMaxLength);
      Assert.assertTrue(resultLen == 12);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, resultLen) == 8);

      // This next section repeats the tests of testRightTrimWithOffset with a maxLength parameter that is
      // less than the number of current characters in the string and thus affects the trim.

      // Nothing to trim (ASCII).
      Assert.assertTrue(StringExpr.characterCount(blue, 0, blue.length) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blue, 0, blue.length, 3);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(blue, 0, resultLen) == 3);

      Assert.assertTrue(StringExpr.characterCount(redgreen, 0, redgreen.length) == 8);
      resultLen = StringExpr.rightTrimAndTruncate(redgreen, 0, redgreen.length, 6);
      Assert.assertTrue(resultLen == 6);

      Assert.assertTrue(StringExpr.characterCount(ascii_sentence, 0, ascii_sentence.length) == 31);
      resultLen = StringExpr.rightTrimAndTruncate(ascii_sentence, 0, ascii_sentence.length, 30);
      Assert.assertTrue(resultLen == 30);

      Assert.assertTrue(StringExpr.characterCount(blanksLeft, 0, blanksLeft.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksLeft, 0, blanksLeft.length, 1);
      Assert.assertTrue(resultLen == 0);

      // Simple trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, blanksRight.length) == 5);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 0, blanksRight.length, 4);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 0, resultLen) == 3);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, blanksBoth.length) == 7);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 0, blanksBoth.length, 6);
      Assert.assertTrue(resultLen == 5);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 0, resultLen) == 5);
     
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, blankString.length) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 0, blankString.length, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 0, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, blankRanges.length) == 30);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 0, blankRanges.length, 19);
      Assert.assertTrue(resultLen == 15);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 0, resultLen) == 15);

      // Offset trims.
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, blanksRight.length - 1) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(blanksRight, 1, blanksRight.length - 1, 3);
      Assert.assertTrue(resultLen == 2);
      Assert.assertTrue(StringExpr.characterCount(blanksRight, 1, resultLen) == 2);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, blanksBoth.length - 4) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 4, blanksBoth.length - 4, 2);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 4, resultLen) == 1);

      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, blanksBoth.length - 5) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(blanksBoth, 5, blanksBoth.length -5, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blanksBoth, 5, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankString, 1, blankString.length - 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankString, 1, blankString.length - 1, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankString, 1, resultLen) == 0);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 3, 6) == 6);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 3, 6, 5);
      Assert.assertTrue(resultLen == 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 3, resultLen) == 4);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, blankRanges.length - 6) == 24);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 6, blankRanges.length- 6, 22);
      Assert.assertTrue(resultLen == blankRanges.length - 6 - 4);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 6, resultLen) == 20);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, blankRanges.length - 7) == 23);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, blankRanges.length - 7, 10);
      Assert.assertTrue(resultLen == 8);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 8);

      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, 8 - 7) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(blankRanges, 7, 8 - 7, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(blankRanges, 7, resultLen) == 0);

      // Multi-byte trims.
      multiByte = new byte[100];

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 4) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 4, 1);
      Assert.assertTrue(resultLen == 3);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 1);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 5) == 3);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 5, 2);
      Assert.assertTrue(resultLen == 4);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 2);

      addMultiByteCharRightPadded1_3(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 9) == 4);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 0, 9, 3);
      Assert.assertTrue(resultLen == 8);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 3);

      addMultiByteCharRightPadded1_1(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 1) == 1);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 1, 1);
      Assert.assertTrue(resultLen == 0);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, resultLen) == 0);

      addMultiByteCharRightPadded1_2(multiByte);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, 2) == 2);
      resultLen = StringExpr.rightTrimAndTruncate(multiByte, 3, 2, 1);
      Assert.assertTrue(resultLen == 1);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 3, resultLen) == 1);

      sentenceOne = new byte[100];
      sentenceOneLen = addMultiByteCharSentenceOne(sentenceOne, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen, 7);
      Assert.assertTrue(resultLen == sentenceOneLen - 9);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne, 0, sentenceOneLen - 3) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOne, 0, sentenceOneLen - 3, 6);
      Assert.assertTrue(resultLen == 13);

      sentenceTwo = new byte[100];
      sentenceTwoLen = addMultiByteCharSentenceTwo(sentenceTwo, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen, 13);
      Assert.assertTrue(resultLen == sentenceTwoLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo, 0, sentenceTwoLen- 5) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwo, 0, sentenceTwoLen - 5, 10);
      Assert.assertTrue(resultLen == sentenceTwoLen - 5);

      // Left pad longer strings with multi-byte characters.
      sentenceOnePaddedLeft = new byte[100];
      start = addPads(sentenceOnePaddedLeft, 0, 3);
      sentenceOnePaddedLeftLen = addMultiByteCharSentenceOne(sentenceOnePaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen) == 3 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen, 3 + 8);
      Assert.assertTrue(resultLen == sentenceOnePaddedLeftLen - 6);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3) == 3 + 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedLeft, 0, sentenceOnePaddedLeftLen - 3, 3 + 6);
      Assert.assertTrue(resultLen == 16);

      sentenceTwoPaddedLeft = new byte[100];
      start = addPads(sentenceTwoPaddedLeft, 0, 2);
      sentenceTwoPaddedLeftLen = addMultiByteCharSentenceTwo(sentenceTwoPaddedLeft, start);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen) == 2 + 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen, 7);
      Assert.assertTrue(resultLen == 10);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5) == 2 + 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedLeft, 0, sentenceTwoPaddedLeftLen - 5, 6);
      Assert.assertTrue(resultLen == 10);

      // Right pad longer strings with multi-byte characters.
      sentenceOnePaddedRight = new byte[100];
      start = addMultiByteCharSentenceOne(sentenceOnePaddedRight, 0);
      sentenceOnePaddedRightLen = addPads(sentenceOnePaddedRight, start, 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen) == 10 + 4);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen, 10);
      Assert.assertTrue(resultLen == sentenceOnePaddedRightLen - 4);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4) == 9);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceOnePaddedRight, 0, sentenceOnePaddedRightLen - 3 - 4, 7);
      Assert.assertTrue(resultLen == 17);

      sentenceTwoPaddedRight = new byte[100];
      start = addMultiByteCharSentenceTwo(sentenceTwoPaddedRight, 0);
      sentenceTwoPaddedRightLen = addPads(sentenceTwoPaddedRight, start, 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen) == 13 + 1);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen, 13);
      Assert.assertTrue(resultLen == sentenceTwoPaddedRightLen - 1);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1) == 10);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceTwoPaddedRight, 0, sentenceTwoPaddedRightLen - 5 - 1, 4);
      Assert.assertTrue(resultLen == 8);

      // Multi-byte characters with blank ranges.
      sentenceBlankRanges = new byte[100];
      sentenceBlankRangesLen = addMultiByteCharSentenceBlankRanges(sentenceBlankRanges, 0);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen) == 17);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen, 4);
      Assert.assertTrue(resultLen == 7);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3) == 16);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 0, sentenceBlankRangesLen - 3, 6);
      Assert.assertTrue(resultLen == 11);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 4, 12) == 8);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 4, 12, 6);
      Assert.assertTrue(resultLen == 7);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 4, resultLen) == 5);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, 17) == 13);
      resultLen = StringExpr.rightTrimAndTruncate(sentenceBlankRanges, 7, 17, 11);
      Assert.assertTrue(resultLen == 12);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges, 7, resultLen) == 8);
  }
Test vectorized expression and filter evaluation for strings.
ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorStringExpressions.java