decompositioniterator.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,062 行 · 第 1/5 页
JAVA
1,062 行
result = parsedStr.charAt(0);
}
}
prevChar = result; // for previous()
return result;
}
/**
* Gets the previous character in the string.
* @return the previous character
*/
public char previous() {
// TODO: right now, this only handles a single-character backup.
// That's all that is used by CollationElementIterator at the moment,
// but when we expose this class in the API or add a previous method
// to CollationElementIterator, we'll need to flesh this method out.
backedUp = true;
return prevChar;
}
/**
* Resets the cursor to the beginning of the string.
*/
public void reset() {
sIndex = getBeginIndex;
pIndex = 0;
decomposing = false; // LAURA
backedUp = false; // LAURA
}
// ============================================================
// package private (These need to be made public for searching)
// ============================================================
/**
* Sets the offset of the currently processed character in the source string.
* @param newOffset the new offset
*/
final void setOffset(int newOffset)
{
if (getBeginIndex <= newOffset && newOffset < getEndIndex) {
sIndex = newOffset;
pIndex = 0;
decomposing = false;
backedUp = false;
} else {
throw new IndexOutOfBoundsException(
"DecompositionIterator new offset out of bounds.");
}
}
/**
* Get the current offset of the character in the processed source string.
* @return The offset of the accessed character
*/
final int getOffset()
{
return sIndex;
}
/**
* Get the decomposition mode.
* @return The decomposition mode.
* @see Collator
* @see RuleBasedCollator
*/
public int getDecomposition()
{
return decmpMode;
}
// -------------------------------------------------------------
// private
// -------------------------------------------------------------
/**
* Decomposes a character into a string
* If the source can't be decomposed, return "".
* @param source the character to be decomposed with
* @param mode the decomposition mode
* @return the decomposed string
* @see java.text.Collator
* @see java.text.Collator#getDecomposition
* @see java.text.Collator#setDecomposition
*/
static String decompose(char source, int mode)
{
if (mode == Collator.NO_DECOMPOSITION) {
// Extract the substring from the source directly if the
// decomposition mode is NO_DECOMPOSITION.
StringBuffer tmp = new StringBuffer();
tmp.append(source);
return tmp.toString();
}
int limit = (mode == Collator.CANONICAL_DECOMPOSITION) ?
maximumCanonical : SHORT_MAX_VALUE;
int index = startOffsets.elementAt(source);
if (index >= limit) return "";
StringBuffer result = new StringBuffer();
while (true) {
char ch = contents.charAt(index++);
if (ch == '\u0000') break;
result.append(ch);
}
fixCanonical(result);
return result.toString();
}
/**
* Decomposes a StringBuffer in place
* @param source the string to be decomposed
* @param mode the decomposition mode
* @see java.text.Collator
* @see java.text.Collator#getDecomposition
* @see java.text.Collator#setDecomposition
*/
static void decompose(StringBuffer source, int mode) {
int start = 0;
int end = source.length();
if (mode == Collator.NO_DECOMPOSITION) {
String temp = source.toString().substring(start, end);
source.setLength(0);
source.append(temp);
}
int limit = (mode == Collator.CANONICAL_DECOMPOSITION) ?
maximumCanonical : SHORT_MAX_VALUE;
StringBuffer result = new StringBuffer();
for (int i = start; i < end; ++i) {
char ch = source.charAt(i);
int index = startOffsets.elementAt(ch);
if (index >= limit) result.append(ch);
else while (true) {
ch = contents.charAt(index++);
if (ch == '\u0000') break;
result.append(ch);
}
}
source.setLength(0);
source.append(result.toString());
}
/**
* Decomposes string into string
* If the source can't be decomposed, return "".
* @param source the string to be decomposed with
* @param mode the decomposition mode
* @return the decomposed string
* @see java.text.Collator
* @see java.text.Collator#getDecomposition
* @see java.text.Collator#setDecomposition
*/
static String decompose(String source, int mode)
{
int start = 0;
int end = source.length();
if (mode == Collator.NO_DECOMPOSITION) {
return source.substring(start, end);
}
int limit = (mode == Collator.CANONICAL_DECOMPOSITION) ?
maximumCanonical : SHORT_MAX_VALUE;
StringBuffer result = new StringBuffer();
for (int i = start; i < end; ++i) {
char ch = source.charAt(i);
int index = startOffsets.elementAt(ch);
if (index >= limit) result.append(ch);
else while (true) {
ch = contents.charAt(index++);
if (ch == '\u0000') break;
result.append(ch);
}
}
return result.toString();
}
/**
* Use to allocate array for using decompose in a tight loop.
* @return the maximum decomposition result characters for output
*/
static int getMaximumDecomposition() {
return maximumDecomposition;
}
/**
* Fixes the sorting sequence of non-spacing characters according to
* their combining class. The algorithm is listed on p.3-11 in the
* Unicode Standard 2.0. The table of combining classes is on p.4-2
* in the Unicode Standard 2.0.
* @param result the string to fix.
*/
private static void fixCanonical(StringBuffer result) {
int i = result.length() - 1;
byte lastType;
byte currentType = canonicals.elementAt(result.charAt(i));
for (--i; i >= 0; --i) {
lastType = currentType;
currentType = canonicals.elementAt(result.charAt(i));
// a swap is presumed to be rare (and a double-swap very rare),
// so don't worry about efficiency here.
if (currentType > lastType && lastType != BASE) {
// swap characters
char temp = result.charAt(i);
result.setCharAt(i, result.charAt(i+1));
result.setCharAt(i+1, temp);
// if not at end, backup (one further, to compensate for for-loop)
if (i < result.length() - 2)
i += 2;
// reset type, since we swapped.
currentType = canonicals.elementAt(result.charAt(i));
}
}
}
//-----------------------------------------------------------
// privates
//-----------------------------------------------------------
//should be in Short class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?