2 *******************************************************************************
4 * Copyright (C) 1999-2009, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 package com.ibm.icu.lang;
12 import com.ibm.icu.text.UTF16;
15 * <code>UScriptRun</code> is used to find runs of characters in
16 * the same script, as defined in the <code>UScript</code> class.
17 * It implements a simple iterator over an array of characters.
18 * The iterator will assign <code>COMMON</code> and <code>INHERITED</code>
19 * characters to the same script as the preceeding characters. If the
20 * COMMON and INHERITED characters are first, they will be assigned to
21 * the same script as the following characters.
23 * The iterator will try to match paired punctuation. If it sees an
24 * opening punctuation character, it will remember the script that
25 * was assigned to that character, and assign the same script to the
26 * matching closing punctuation.
28 * No attempt is made to combine related scripts into a single run. In
29 * particular, Hiragana, Katakana, and Han characters will appear in separate
32 * Here is an example of how to iterate over script runs:
34 * void printScriptRuns(char[] text)
36 * UScriptRun scriptRun = new UScriptRun(text);
38 * while (scriptRun.next()) {
39 * int start = scriptRun.getScriptStart();
40 * int limit = scriptRun.getScriptLimit();
41 * int script = scriptRun.getScriptCode();
43 * System.out.println("Script \"" + UScript.getName(script) + "\" from " +
44 * start + " to " + limit + ".");
50 * @deprecated This API is ICU internal only.
52 public final class UScriptRun
55 * Construct an empty <code>UScriptRun</code> object. The <code>next()</code>
56 * method will return <code>false</code> the first time it is called.
59 * @deprecated This API is ICU internal only.
63 char[] nullChars = null;
65 reset(nullChars, 0, 0);
69 * Construct a <code>UScriptRun</code> object which iterates over the
70 * characters in the given string.
72 * @param text the string of characters over which to iterate.
75 * @deprecated This API is ICU internal only.
77 public UScriptRun(String text)
83 * Construct a <code>UScriptRun</code> object which iterates over a subrange
84 * of the characetrs in the given string.
86 * @param text the string of characters over which to iterate.
87 * @param start the index of the first character over which to iterate
88 * @param count the number of characters over which to iterate
91 * @deprecated This API is ICU internal only.
93 public UScriptRun(String text, int start, int count)
95 reset(text, start, count);
99 * Construct a <code>UScriptRun</code> object which iterates over the given
102 * @param chars the array of characters over which to iterate.
105 * @deprecated This API is ICU internal only.
107 public UScriptRun(char[] chars)
113 * Construct a <code>UScriptRun</code> object which iterates over a subrange
114 * of the given characetrs.
116 * @param chars the array of characters over which to iterate.
117 * @param start the index of the first character over which to iterate
118 * @param count the number of characters over which to iterate
121 * @deprecated This API is ICU internal only.
123 public UScriptRun(char[] chars, int start, int count)
125 reset(chars, start, count);
130 * Reset the iterator to the start of the text.
133 * @deprecated This API is ICU internal only.
135 public final void reset()
137 // empty any old parenStack contents.
138 // NOTE: this is not the most efficient way
139 // to do this, but it's the easiest to write...
140 while (stackIsNotEmpty()) {
144 scriptStart = textStart;
145 scriptLimit = textStart;
146 scriptCode = UScript.INVALID_CODE;
151 textIndex = textStart;
155 * Reset the iterator to iterate over the given range of the text. Throws
156 * IllegalArgumentException if the range is outside of the bounds of the
159 * @param start the index of the new first character over which to iterate
160 * @param count the new number of characters over which to iterate.
161 * @exception IllegalArgumentException If invalid arguments are passed.
164 * @deprecated This API is ICU internal only.
166 public final void reset(int start, int count)
167 throws IllegalArgumentException
175 if (start < 0 || count < 0 || start > len - count) {
176 throw new IllegalArgumentException();
180 textLimit = start + count;
186 * Reset the iterator to iterate over <code>count</code> characters
187 * in <code>chars</code> starting at <code>start</code>. This allows
188 * clients to reuse an iterator.
190 * @param chars the new array of characters over which to iterate.
191 * @param start the index of the first character over which to iterate.
192 * @param count the number of characters over which to iterate.
195 * @deprecated This API is ICU internal only.
197 public final void reset(char[] chars, int start, int count)
200 chars = emptyCharArray;
209 * Reset the iterator to iterate over the characters
210 * in <code>chars</code>. This allows clients to reuse an iterator.
212 * @param chars the new array of characters over which to iterate.
215 * @deprecated This API is ICU internal only.
217 public final void reset(char[] chars)
222 length = chars.length;
225 reset(chars, 0, length);
229 * Reset the iterator to iterate over <code>count</code> characters
230 * in <code>text</code> starting at <code>start</code>. This allows
231 * clients to reuse an iterator.
233 * @param str the new string of characters over which to iterate.
234 * @param start the index of the first character over which to iterate.
235 * @param count the nuber of characters over which to iterate.
238 * @deprecated This API is ICU internal only.
240 public final void reset(String str, int start, int count)
245 chars = str.toCharArray();
248 reset(chars, start, count);
252 * Reset the iterator to iterate over the characters
253 * in <code>text</code>. This allows clients to reuse an iterator.
255 * @param str the new string of characters over which to iterate.
258 * @deprecated This API is ICU internal only.
260 public final void reset(String str)
265 length = str.length();
268 reset(str, 0, length);
274 * Get the starting index of the current script run.
276 * @return the index of the first character in the current script run.
279 * @deprecated This API is ICU internal only.
281 public final int getScriptStart()
287 * Get the index of the first character after the current script run.
289 * @return the index of the first character after the current script run.
292 * @deprecated This API is ICU internal only.
294 public final int getScriptLimit()
300 * Get the script code for the script of the current script run.
302 * @return the script code for the script of the current script run.
303 * @see com.ibm.icu.lang.UScript
306 * @deprecated This API is ICU internal only.
308 public final int getScriptCode()
314 * Find the next script run. Returns <code>false</code> if there
315 * isn't another run, returns <code>true</code> if there is.
317 * @return <code>false</code> if there isn't another run, <code>true</code> if there is.
320 * @deprecated This API is ICU internal only.
322 public final boolean next()
324 // if we've fallen off the end of the text, we're done
325 if (scriptLimit >= textLimit) {
329 scriptCode = UScript.COMMON;
330 scriptStart = scriptLimit;
334 while (textIndex < textLimit) {
335 int ch = UTF16.charAt(text, textStart, textLimit, textIndex - textStart);
336 int codePointCount = UTF16.getCharCount(ch);
337 int sc = UScript.getScript(ch);
338 int pairIndex = getPairIndex(ch);
340 textIndex += codePointCount;
342 // Paired character handling:
344 // if it's an open character, push it onto the stack.
345 // if it's a close character, find the matching open on the
346 // stack, and use that script code. Any non-matching open
347 // characters above it on the stack will be poped.
348 if (pairIndex >= 0) {
349 if ((pairIndex & 1) == 0) {
350 push(pairIndex, scriptCode);
352 int pi = pairIndex & ~1;
354 while (stackIsNotEmpty() && top().pairIndex != pi) {
358 if (stackIsNotEmpty()) {
359 sc = top().scriptCode;
364 if (sameScript(scriptCode, sc)) {
365 if (scriptCode <= UScript.INHERITED && sc > UScript.INHERITED) {
371 // if this character is a close paired character,
372 // pop the matching open character from the stack
373 if (pairIndex >= 0 && (pairIndex & 1) != 0) {
377 // We've just seen the first character of
378 // the next run. Back over it so we'll see
379 // it again the next time.
380 textIndex -= codePointCount;
385 scriptLimit = textIndex;
390 * Compare two script codes to see if they are in the same script. If one script is
391 * a strong script, and the other is INHERITED or COMMON, it will compare equal.
393 * @param scriptOne one of the script codes.
394 * @param scriptTwo the other script code.
395 * @return <code>true</code> if the two scripts are the same.
396 * @see com.ibm.icu.lang.UScript
398 private static boolean sameScript(int scriptOne, int scriptTwo)
400 return scriptOne <= UScript.INHERITED || scriptTwo <= UScript.INHERITED || scriptOne == scriptTwo;
404 * An internal class which holds entries on the paren stack.
406 private static final class ParenStackEntry
411 public ParenStackEntry(int thePairIndex, int theScriptCode)
413 pairIndex = thePairIndex;
414 scriptCode = theScriptCode;
418 private static final int mod(int sp)
420 return sp % PAREN_STACK_DEPTH;
423 private static final int inc(int sp, int count)
425 return mod(sp + count);
428 private static final int inc(int sp)
433 private static final int dec(int sp, int count)
435 return mod(sp + PAREN_STACK_DEPTH - count);
438 private static final int dec(int sp)
443 private static final int limitInc(int count)
445 if (count < PAREN_STACK_DEPTH) {
452 private final boolean stackIsEmpty()
454 return pushCount <= 0;
457 private final boolean stackIsNotEmpty()
459 return ! stackIsEmpty();
462 private final void push(int pairIndex, int scrptCode)
464 pushCount = limitInc(pushCount);
465 fixupCount = limitInc(fixupCount);
467 parenSP = inc(parenSP);
468 parenStack[parenSP] = new ParenStackEntry(pairIndex, scrptCode);
471 private final void pop()
474 if (stackIsEmpty()) {
478 parenStack[parenSP] = null;
480 if (fixupCount > 0) {
485 parenSP = dec(parenSP);
487 // If the stack is now empty, reset the stack
488 // pointers to their initial values.
489 if (stackIsEmpty()) {
494 private final ParenStackEntry top()
496 return parenStack[parenSP];
499 private final void syncFixup()
504 private final void fixup(int scrptCode)
506 int fixupSP = dec(parenSP, fixupCount);
508 while (fixupCount-- > 0) {
509 fixupSP = inc(fixupSP);
510 parenStack[fixupSP].scriptCode = scrptCode;
514 private char[] emptyCharArray = {};
518 private int textIndex;
519 private int textStart;
520 private int textLimit;
522 private int scriptStart;
523 private int scriptLimit;
524 private int scriptCode;
526 private static int PAREN_STACK_DEPTH = 32;
527 private static ParenStackEntry parenStack[] = new ParenStackEntry[PAREN_STACK_DEPTH];
528 private int parenSP = -1;
529 private int pushCount = 0;
530 private int fixupCount = 0;
533 * Find the highest bit that's set in a word. Uses a binary search through
536 * @param n the word in which to find the highest bit that's set.
537 * @return the bit number (counting from the low order bit) of the highest bit.
539 private static final byte highBit(int n)
576 * Search the pairedChars array for the given character.
578 * @param ch the character for which to search.
579 * @return the index of the character in the table, or -1 if it's not there.
581 private static int getPairIndex(int ch)
583 int probe = pairedCharPower;
586 if (ch >= pairedChars[pairedCharExtra]) {
587 index = pairedCharExtra;
590 while (probe > (1 << 0)) {
593 if (ch >= pairedChars[index + probe]) {
598 if (pairedChars[index] != ch) {
605 private static int pairedChars[] = {
606 0x0028, 0x0029, // ascii paired punctuation
610 0x00ab, 0x00bb, // guillemets
611 0x2018, 0x2019, // general punctuation
614 0x3008, 0x3009, // chinese paired punctuation
625 private static int pairedCharPower = 1 << highBit(pairedChars.length);
626 private static int pairedCharExtra = pairedChars.length - pairedCharPower;