/*** Get the value associated with a pair of surrogates.* @param lead a lead surrogate* @param trail a trail surrogate*/publicfinalint getSurrogateValue(char lead, char trail)
{
if (!UTF16.isLeadSurrogate(lead) || !UTF16.isTrailSurrogate(trail)) {
thrownew IllegalArgumentException(
"Argument characters do not form a supplementary character");
}
// get fold position for the next trail surrogateint offset = getSurrogateOffset(lead, trail);
// get the real data from the folded lead/trail unitsif (offset > 0) {
return m_data_[offset];
}
// return m_initialValue_ if there is an errorreturn m_initialValue_;
}

/** * Returns the current codepoint * @return current codepoint */@Override
publicint currentCodePoint(){
// cannot use charAt due to it different// behaviour when index is pointing at a// trail surrogate, check for surrogatesint ch = current();
if(UTF16.isLeadSurrogate((char)ch)){
// advance the index to get the next code point next();
// due to post increment semantics current() after next()// actually returns the next char which is what we wantint ch2 = current();
// current should never change the current index so back off previous();
if(UTF16.isTrailSurrogate((char)ch2)){
// we found a surrogate pairreturn Character.toCodePoint((char)ch, (char)ch2);
}
}
return ch;
}

/** * Returns the codepoint at the current index. If the current index is invalid, DONE is returned. If the current * index points to a lead surrogate, and there is a following trail surrogate, then the code point is returned. * Otherwise, the code unit at index is returned. Index is not changed. * * @return current codepoint * @stable ICU 2.4 */publicint currentCodePoint() {
int ch = current();
if (UTF16.isLeadSurrogate((char) ch)) {
// advance the index to get the// next code point next();
// due to post increment semantics// current() after next() actually// returns the char we wantint ch2 = current();
// current should never change// the current index so back off previous();
if (UTF16.isTrailSurrogate((char) ch2)) {
// we found a surrogate pair// return the codepointreturn Character.toCodePoint((char) ch, (char) ch2);
}
}
return ch;
}

/** * Returns the code point at index, and increments to the next code point (post-increment semantics). If index does * not point to a valid surrogate pair, the behavior is the same as <code>next()</code>. Otherwise the iterator is * incremented past the surrogate pair, and the code point represented by the pair is returned. * * @return the next codepoint in text, or DONE if the index is at the limit of the text. * @stable ICU 2.4 */@Override
publicint nextCodePoint() {
int ch1 = next();
if (UTF16.isLeadSurrogate((char) ch1)) {
int ch2 = next();
if (UTF16.isTrailSurrogate((char) ch2)) {
return Character.toCodePoint((char) ch1, (char) ch2);
} elseif (ch2 != DONE) {
// unmatched surrogate so back out previous();
}
}
return ch1;
}

/** * Retreat to the start of the previous code point in the text, and return it (pre-decrement semantics). If the * index is not preceeded by a valid surrogate pair, the behavior is the same as <code>previous()</code>. Otherwise * the iterator is decremented to the start of the surrogate pair, and the code point represented by the pair is * returned. * * @return the previous code point in the text, or DONE if the new index is before the start of the text. * @stable ICU 2.4 */publicint previousCodePoint() {
int ch1 = previous();
if (UTF16.isTrailSurrogate((char) ch1)) {
int ch2 = previous();
if (UTF16.isLeadSurrogate((char) ch2)) {
return Character.toCodePoint((char) ch2, (char) ch1);
} elseif (ch2 != DONE) {
// unmatched trail surrogate so back out next();
}
}
return ch1;
}