@Override public int length() { return value.codePointCount( 0, value.length() ); }
/** * Returns the number of Unicode code points in the specified text * range of this {@code CharSeq}. The text range begins at the * specified {@code beginIndex} and extends to the * {@code char} at index {@code endIndex - 1}. Thus the * length (in {@code char}s) of the text range is * {@code endIndex-beginIndex}. Unpaired surrogates within * the text range count as one code point each. * * @param beginIndex the index to the first {@code char} of * the text range. * @param endIndex the index after the last {@code char} of * the text range. * @return the number of Unicode code points in the specified text * range * @throws IndexOutOfBoundsException if the * {@code beginIndex} is negative, or {@code endIndex} * is larger than the length of this {@code CharSeq}, or * {@code beginIndex} is larger than {@code endIndex}. */ public int codePointCount(int beginIndex, int endIndex) { return back.codePointCount(beginIndex, endIndex); }
public StringBuilder drainTo(final StringBuilder b) { try { return b.append(string, idx + offs, offs + len); } finally { offset += string.codePointCount(idx + offs, offs + len); idx = len; } }
public StringBuilder drainTo(final StringBuilder b) { try { return b.append(string, idx + offs, offs + len); } finally { offset += string.codePointCount(idx + offs, offs + len); idx = len; } }
public String drainToString() { try { return string.substring(idx + offs, offs + len); } finally { offset += string.codePointCount(idx + offs, offs + len); idx = len; } } };
public String drainToString() { try { return string.substring(idx + offs, offs + len); } finally { offset += string.codePointCount(idx + offs, offs + len); idx = len; } } }
public int getCharacterLength() { return value.codePointCount(0, value.length()); }
final int[] result = new int[s.codePointCount(0, s.length())]; int index = 0; for (int i = 0; i < result.length; i++) {
@Override public int getCharacterLength() { String strippedValue = getStrippedValue(); return strippedValue.codePointCount(0, strippedValue.length()); }
public void dumpStringData(String s) throws IOException { System.out.println(" " + s); System.out.println(" String.length: " + s.length()); System.out.println("String.codePointCount: " + s.codePointCount(0, s.length())); System.out.println(" Utf8.size: " + Utf8.size(s)); System.out.println(" UTF-8 bytes: " + ByteString.encodeUtf8(s).hex()); System.out.println(); }
public static String enforceMaxLength(String val, int maxLength) { if (val == null) { return null; } String value = val; if (maxLength > 0) { int valLength = val.codePointCount(0, val.length()); if (valLength > maxLength) { // Truncate the excess chars to fit the character length. // Also make sure we take supplementary chars into account. value = val.substring(0, val.offsetByCodePoints(0, maxLength)); } } return value; }
/** * Checks whether a given String consists only of surrogate pairs. * @param word to be checked * @since 4.2 */ protected boolean isSurrogatePairCombination (String word) { if (word.length() > 1 && word.length() % 2 == 0 && word.codePointCount(0, word.length()) != word.length()) { // some symbols such as emojis (😂) have a string length that equals 2 boolean isSurrogatePairCombination = true; for (int i = 0; i < word.length() && isSurrogatePairCombination; i += 2) { isSurrogatePairCombination &= Character.isSurrogatePair(word.charAt(i), word.charAt(i + 1)); } return isSurrogatePairCombination; } return false; }
/** * 编码 * * @param text 文本 * @return 密文 */ public String encode(String text) { Assert.notNull(text, "Text should not be null."); text = text.toUpperCase(); final StringBuilder morseBuilder = new StringBuilder(); final int len = text.codePointCount(0, text.length()); for (int i = 0; i < len; i++) { int codePoint = text.codePointAt(i); String word = alphabets.get(codePoint); if (word == null) { word = Integer.toBinaryString(codePoint); } morseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split); } return morseBuilder.toString(); }
/** * 编码 * * @param text 文本 * @return 密文 */ public String encode(String text) { Assert.notNull(text, "Text should not be null."); text = text.toUpperCase(); final StringBuilder morseBuilder = new StringBuilder(); final int len = text.codePointCount(0, text.length()); for (int i = 0; i < len; i++) { int codePoint = text.codePointAt(i); String word = alphabets.get(codePoint); if (word == null) { word = Integer.toBinaryString(codePoint); } morseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split); } return morseBuilder.toString(); }
public static String getPaddedValue(String val, int maxLength) { if (val == null) { return null; } if (maxLength < 0) { return val; } int valLength = val.codePointCount(0, val.length()); if (valLength > maxLength) { return enforceMaxLength(val, maxLength); } if (maxLength > valLength) { // Make sure we pad the right amount of spaces; valLength is in terms of code points, // while StringUtils.rpad() is based on the number of java chars. int padLength = val.length() + (maxLength - valLength); val = StringUtils.rightPad(val, padLength); } return val; }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final String value = data.getInstance().getNode().textValue(); final int size = value.codePointCount(0, value.length()); if (size < intValue) report.error(newMsg(data, bundle, "err.common.minLength.tooShort") .putArgument("value", value).putArgument("found", size) .putArgument(keyword, intValue)); } }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final String value = data.getInstance().getNode().textValue(); final int size = value.codePointCount(0, value.length()); if (size > intValue) report.error(newMsg(data, bundle, "err.common.maxLength.tooLong") .putArgument("value", value).putArgument("found", size) .putArgument(keyword, intValue)); } }
public void testStringLength() throws Exception { int strLen = 20; int[] lengths = { 15, 20, 25 }; // Try with supplementary characters for (int idx1 = 0; idx1 < lengths.length; ++idx1) { // Create random test string int curLen = lengths[idx1]; String testString = createRandomSupplementaryCharString(curLen); assertEquals(curLen, testString.codePointCount(0, testString.length())); String enforcedString = HiveBaseChar.enforceMaxLength(testString, strLen); if (curLen <= strLen) { // No truncation needed assertEquals(testString, enforcedString); } else { // String should have been truncated. assertEquals(strLen, enforcedString.codePointCount(0, enforcedString.length())); } } assertNull(HiveBaseChar.enforceMaxLength(null, 0)); }
@Test public void nextString() { for ( int i = 0; i < ITERATIONS; i++ ) { TextValue textValue = randomValues.nextTextValue( 10, 20 ); String asString = textValue.stringValue(); int length = asString.codePointCount( 0, asString.length() ); assertThat( length, greaterThanOrEqualTo( 10 ) ); assertThat( length, lessThanOrEqualTo( 20 ) ); } }
public void testGetPaddedValue() { int strLen = 20; int[] lengths = { 15, 20, 25 }; for (int idx1 = 0; idx1 < lengths.length; ++idx1) { int curLen = lengths[idx1]; // Random test string String testString = createRandomSupplementaryCharString(curLen); assertEquals(curLen, testString.codePointCount(0, testString.length())); String paddedString = HiveBaseChar.getPaddedValue(testString, strLen); assertEquals(strLen, paddedString.codePointCount(0, paddedString.length())); } assertEquals("abc ", HiveBaseChar.getPaddedValue("abc", 10)); assertEquals("abc ", HiveBaseChar.getPaddedValue("abc ", 10)); assertNull(HiveBaseChar.getPaddedValue(null, 0)); } }