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