/** * Append a single UTF-32 value to the end of a StringBuffer. If a validity check is required, * use {@link com.ibm.icu.lang.UCharacter#isLegal(int)} on char32 before * calling. * * @param target The buffer to append to * @param char32 Value to append. * @return the updated StringBuffer * @exception IllegalArgumentException Thrown when char32 does not lie within the range of the Unicode codepoints * @stable ICU 2.1 */ public static StringBuffer append(StringBuffer target, int char32) { // Check for irregular values if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException("Illegal codepoint: " + Integer.toHexString(char32)); } // Write the UTF-16 values if (char32 >= SUPPLEMENTARY_MIN_VALUE) { target.append(getLeadSurrogate(char32)); target.append(getTrailSurrogate(char32)); } else { target.append((char) char32); } return target; }
/** * Adds a codepoint to offset16 position of the argument char array. * * @param target Char array to be append with the new code point * @param limit UTF16 offset which the codepoint will be appended. * @param char32 Code point to be appended * @return offset after char32 in the array. * @exception IllegalArgumentException Thrown if there is not enough space for the append, or when char32 does not * lie within the range of the Unicode codepoints. * @stable ICU 2.1 */ public static int append(char[] target, int limit, int char32) { // Check for irregular values if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException("Illegal codepoint"); } // Write the UTF-16 values if (char32 >= SUPPLEMENTARY_MIN_VALUE) { target[limit++] = getLeadSurrogate(char32); target[limit++] = getTrailSurrogate(char32); } else { target[limit++] = (char) char32; } return limit; }
/** * <p> * Converts argument code point and returns a String object representing the code point's value * in UTF16 format. * </p> * <p> * This method does not check for the validity of the codepoint, the results are not guaranteed * if a invalid codepoint is passed as argument. * </p> * <p> * The result is a string whose length is 1 for non-supplementary code points, 2 otherwise. * </p> * * @param ch * code point * @return string representation of the code point */ private static String toString(int ch) { if (ch < SUPPLEMENTARY_MIN_VALUE) { return String.valueOf((char) ch); } StringBuilder result = new StringBuilder(); result.append(getLeadSurrogate(ch)); result.append(getTrailSurrogate(ch)); return result.toString(); } }
/** * TODO: create Appendable version of UTF16.append(buf, c), * maybe in new class Appendables? * @throws IOException */ private static void appendCodePoint(Appendable app, int c) { assert 0 <= c && c <= 0x10ffff; try { if (c <= 0xffff) { app.append((char) c); } else { app.append(UTF16.getLeadSurrogate(c)).append(UTF16.getTrailSurrogate(c)); } } catch (IOException e) { throw new ICUUncheckedIOException(e); } }
private CoderResult toUWriteCodePoint(int c, CharBuffer target, IntBuffer offsets, int sourceIndex) { CoderResult cr = CoderResult.UNDERFLOW; int tBeginIndex = target.position(); if (target.hasRemaining()) { if (c <= 0xffff) { target.put((char) c); c = UConverterConstants.U_SENTINEL; } else /* c is a supplementary code point */{ target.put(UTF16.getLeadSurrogate(c)); c = UTF16.getTrailSurrogate(c); if (target.hasRemaining()) { target.put((char) c); c = UConverterConstants.U_SENTINEL; } } /* write offsets */ if (offsets != null) { offsets.put(sourceIndex); if ((tBeginIndex + 1) < target.position()) { offsets.put(sourceIndex); } } } /* write overflow from c */ if (c >= 0) { charErrorBufferLength = UTF16.append(charErrorBufferArray, 0, c); cr = CoderResult.OVERFLOW; } return cr; }
char32 = UTF16.getTrailSurrogate(char32); if (target.hasRemaining()) { target.put((char) char32);
target.put(UTF16.getTrailSurrogate(c)); if(offsets!=null){ offsets.put(sourceIndex); offsets.put(sourceIndex); charErrorBufferArray[0] = UTF16.getTrailSurrogate(c); charErrorBufferLength = 1; cr = CoderResult.OVERFLOW;
private final CoderResult encodeChar(CharBuffer source, ByteBuffer target, IntBuffer offsets, char ch) { int sourceIndex = source.position() - 1; CoderResult cr; if (UTF16.isSurrogate(ch)) { cr = handleSurrogates(source, ch); if (cr != null) return cr; char trail = UTF16.getTrailSurrogate(fromUChar32); fromUChar32 = 0; // 4 bytes temp[0 ^ endianXOR] = (byte) (ch >>> 8); temp[1 ^ endianXOR] = (byte) (ch); temp[2 ^ endianXOR] = (byte) (trail >>> 8); temp[3 ^ endianXOR] = (byte) (trail); cr = fromUWriteBytes(this, temp, 0, 4, target, offsets, sourceIndex); } else { // 2 bytes temp[0 ^ endianXOR] = (byte) (ch >>> 8); temp[1 ^ endianXOR] = (byte) (ch); cr = fromUWriteBytes(this, temp, 0, 2, target, offsets, sourceIndex); } return (cr.isUnderflow() ? null : cr); } }
sb.setCharAt(starter+1, UTF16.getTrailSurrogate(composite)); } else { sb.setCharAt(starter, (char)c); sb.insert(starter+1, UTF16.getTrailSurrogate(composite)); ++p; } else {
/** * Traverses the trie from the current state for the * one or two UTF-16 code units for this input code point. * @param cp A Unicode code point 0..0x10ffff. * @return The match/value Result. * @stable ICU 4.8 */ public Result nextForCodePoint(int cp) { return cp<=0xffff ? next(cp) : (next(UTF16.getLeadSurrogate(cp)).hasNext() ? next(UTF16.getTrailSurrogate(cp)) : Result.NO_MATCH); }
/** * Traverses the trie from the initial state for the * one or two UTF-16 code units for this input code point. * Equivalent to reset().nextForCodePoint(cp). * @param cp A Unicode code point 0..0x10ffff. * @return The match/value Result. * @stable ICU 4.8 */ public Result firstForCodePoint(int cp) { return cp<=0xffff ? first(cp) : (first(UTF16.getLeadSurrogate(cp)).hasNext() ? next(UTF16.getTrailSurrogate(cp)) : Result.NO_MATCH); }
dest.insert(codeUnitIndex+1, UTF16.getTrailSurrogate(n));
m_nextBlockIndex_ ++; if (UTF16.getTrailSurrogate(m_nextCodepoint_) != UTF16.TRAIL_SURROGATE_MIN_VALUE) {