/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
*******************************************************************************
*
* Copyright (C) 1999-2003, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
*/
/**
* <code>ScriptRun</code> is used to find runs of characters in
* the same script, as defined in the <code>Script</code> class.
* It implements a simple iterator over an array of characters.
* The iterator will assign <code>COMMON</code> and <code>INHERITED</code>
* characters to the same script as the preceeding characters. If the
* COMMON and INHERITED characters are first, they will be assigned to
* the same script as the following characters.
*
* The iterator will try to match paired punctuation. If it sees an
* opening punctuation character, it will remember the script that
* was assigned to that character, and assign the same script to the
* matching closing punctuation.
*
* No attempt is made to combine related scripts into a single run. In
* particular, Hiragana, Katakana, and Han characters will appear in seperate
* runs.
* Here is an example of how to iterate over script runs:
* <pre>
* void printScriptRuns(char[] text)
* {
* ScriptRun scriptRun = new ScriptRun(text, 0, text.length);
*
* while (scriptRun.next()) {
* int start = scriptRun.getScriptStart();
* int limit = scriptRun.getScriptLimit();
* int script = scriptRun.getScriptCode();
*
* System.out.println("Script \"" + Script.getName(script) + "\" from " +
* start + " to " + limit + ".");
* }
* }
* </pre>
*
*/
public final class ScriptRun
{
private int textStart;
private int textLimit;
private int scriptLimit;
private int scriptCode;
private int parenSP;
public ScriptRun() {
// must call init later or we die.
}
/**
* Construct a <code>ScriptRun</code> object which iterates over a subrange
* of the given characetrs.
*
* @param chars the array of characters over which to iterate.
* @param start the index of the first character over which to iterate
* @param count the number of characters over which to iterate
*/
{
}
{
throw new IllegalArgumentException();
}
parenSP = 0;
}
/**
* Get the starting index of the current script run.
*
* @return the index of the first character in the current script run.
*/
public final int getScriptStart() {
return scriptStart;
}
/**
* Get the index of the first character after the current script run.
*
* @return the index of the first character after the current script run.
*/
public final int getScriptLimit() {
return scriptLimit;
}
/**
* Get the script code for the script of the current script run.
*
* @return the script code for the script of the current script run.
* @see #Script
*/
public final int getScriptCode() {
return scriptCode;
}
/**
* Find the next script run. Returns <code>false</code> if there
* isn't another run, returns <code>true</code> if there is.
*
* @return <code>false</code> if there isn't another run, <code>true</code> if there is.
*/
public final boolean next() {
// if we've fallen off the end of the text, we're done
if (scriptLimit >= textLimit) {
return false;
}
int ch;
// Paired character handling:
//
// if it's an open character, push it onto the stack.
// if it's a close character, find the matching open on the
// stack, and use that script code. Any non-matching open
// characters above it on the stack will be popped.
if (pairIndex >= 0) {
stack = new int[32];
}
} else if (parenSP > 0) {
if (parenSP >= 0) {
} else {
parenSP = 0;
}
}
}
}
scriptCode = sc;
// now that we have a final script code, fix any open
// characters we pushed before we knew the script code.
startSP += 2;
}
}
// if this character is a close paired character,
// pop it from the stack
parenSP -= 2;
}
} else {
// We've just seen the first character of
// the next run. Back over it so we'll see
// it again the next time.
// we're outta here
break;
}
}
return true;
}
static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;
private final int nextCodePoint() {
if (scriptLimit >= textLimit) {
return DONE;
}
++scriptLimit;
}
}
return ch;
}
if (ch >= 0) {
if (ch >= 0x10000) {
scriptLimit -= 2;
} else {
scriptLimit -= 1;
}
}
}
/**
* Compare two script codes to see if they are in the same script. If one script is
* a strong script, and the other is INHERITED or COMMON, it will compare equal.
*
* @param scriptOne one of the script codes.
* @param scriptTwo the other script code.
* @return <code>true</code> if the two scripts are the same.
* @see com.ibm.icu.lang.Script
*/
}
/**
* Find the highest bit that's set in a word. Uses a binary search through
* the bits.
*
* @param n the word in which to find the highest bit that's set.
* @return the bit number (counting from the low order bit) of the highest bit.
*/
private static final byte highBit(int n)
{
if (n <= 0) {
return -32;
}
byte bit = 0;
if (n >= 1 << 16) {
n >>= 16;
bit += 16;
}
if (n >= 1 << 8) {
n >>= 8;
bit += 8;
}
if (n >= 1 << 4) {
n >>= 4;
bit += 4;
}
if (n >= 1 << 2) {
n >>= 2;
bit += 2;
}
if (n >= 1 << 1) {
n >>= 1;
bit += 1;
}
return bit;
}
/**
* Search the pairedChars array for the given character.
*
* @param ch the character for which to search.
* @return the index of the character in the table, or -1 if it's not there.
*/
{
int probe = pairedCharPower;
int index = 0;
}
probe >>= 1;
}
}
index = -1;
}
return index;
}
// all common
private static int pairedChars[] = {
0x0028, 0x0029, // ascii paired punctuation // common
0x003c, 0x003e, // common
0x005b, 0x005d, // common
0x007b, 0x007d, // common
0x00ab, 0x00bb, // guillemets // common
0x2018, 0x2019, // general punctuation // common
0x201c, 0x201d, // common
0x2039, 0x203a, // common
0x3008, 0x3009, // chinese paired punctuation // common
0x300a, 0x300b,
0x300c, 0x300d,
0x300e, 0x300f,
0x3010, 0x3011,
0x3014, 0x3015,
0x3016, 0x3017,
0x3018, 0x3019,
0x301a, 0x301b
};
}