Transform { static int transformDictionaryWord(byte[] dst, int dstOffset, ByteBuffer data, int wordOffset, int len, Transform transform) { int offset = dstOffset; byte[] string = transform.prefix; int tmp = string.length; int i = 0; while (i < tmp) { dst[offset++] = string[i++]; } int op = transform.type; tmp = WordTransformType.getOmitFirst(op); if (tmp > len) { tmp = len; } wordOffset += tmp; len -= tmp; len -= WordTransformType.getOmitLast(op); i = len; while (i > 0) { dst[offset++] = data.get(wordOffset++); i--; } if (op == UPPERCASE_ALL || op == UPPERCASE_FIRST) { int uppercaseOffset = offset - len; if (op == UPPERCASE_FIRST) { len = 1; } while (len > 0) { tmp = dst[uppercaseOffset] & 0xFF; if (tmp < 0xc0) { if (tmp >= 'a' && tmp <= 'z') { dst[uppercaseOffset] ^= (byte) 32; } uppercaseOffset += 1; len -= 1; } else if (tmp < 0xe0) { dst[uppercaseOffset + 1] ^= (byte) 32; uppercaseOffset += 2; len -= 2; } else { dst[uppercaseOffset + 2] ^= (byte) 5; uppercaseOffset += 3; len -= 3; } } } string = transform.suffix; tmp = string.length; i = 0; while (i < tmp) { dst[offset++] = string[i++]; } return offset - dstOffset; } Transform(String prefix, int type, String suffix);   }
@Test public void testTrimAll() { byte[] output = new byte[2]; byte[] input = {119, 111, 114, 100}; Transform transform = new Transform("[", WordTransformType.OMIT_FIRST_5, "]"); Transform.transformDictionaryWord( output, 0, ByteBuffer.wrap(input), 0, input.length, transform); byte[] expectedOutput = {91, 93}; assertArrayEquals(expectedOutput, output); }
@Test public void testCapitalize() { byte[] output = new byte[8]; byte[] input = {113, -61, -90, -32, -92, -86}; Transform transform = new Transform("[", WordTransformType.UPPERCASE_ALL, "]"); Transform.transformDictionaryWord( output, 0, ByteBuffer.wrap(input), 0, input.length, transform); byte[] expectedOutput = {91, 81, -61, -122, -32, -92, -81, 93}; assertArrayEquals(expectedOutput, output); }
@Test public void testAllTransforms() { byte[] testWord = {111, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; byte[] output = new byte[2259]; int offset = 0; for (int i = 0; i < Transform.TRANSFORMS.length; ++i) { offset += Transform.transformDictionaryWord( output, offset, ByteBuffer.wrap(testWord), 0, testWord.length, Transform.TRANSFORMS[i]); output[offset++] = -1; } assertEquals(output.length, offset); assertEquals(8929191060211225186L, crc64(output)); }