private String escapeWindowsJvmOpt(String jvmOpts) { boolean wasOnBackslash = false; StringBuilder escapedJvmOpt = new StringBuilder(); CharacterIterator it = new StringCharacterIterator(jvmOpts); //argument quoting: // - " must be encoded as \" // - % must be encoded as %% // - pathological case: \" must be encoded as \\\", but other than that, \ MUST NOT be quoted // - other characters (including ') will not be quoted // - use a state machine rather than regexps for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { String repl = Character.toString(ch); if (ch == '%') { repl = "%%"; } else if (ch == '"') { repl = (wasOnBackslash ? '\\' : "") + "\\\""; } wasOnBackslash = ch == '\\'; escapedJvmOpt.append(repl); } return escapedJvmOpt.toString(); }
private void skipWhiteSpace() { do { if (Character.isWhitespace(c)) ; else if (c == '/') { next(); if (c == '*') { // skip multiline comments while (c != CharacterIterator.DONE) if (next() == '*' && next() == '/') break; if (c == CharacterIterator.DONE) throw new MalformedJsonException("Unterminated comment while parsing JSON string."); } else if (c == '/') while (c != '\n' && c != CharacterIterator.DONE) next(); else { previous(); break; } } else break; } while (next() != CharacterIterator.DONE); }
public static int readHexValue(CharacterIterator i, int maxchars) { int accumul = 0; for (int cntr = 0; cntr < maxchars; cntr++) { final char c = i.current(); if (c == CharacterIterator.DONE || !isHexDigit(c)) { break; } accumul = (accumul << 4) | hexValueOf(c); i.next(); } return accumul; }
public static int readIntegerValue(CharacterIterator i) { char ch = i.current(); if (ch == '-') { return readDecimalValue(i, 10); } if (ch == '0') { ch = i.next(); if (ch == 'x' || ch == 'X') { i.next(); return readHexValue(i, 8); } else if (ch == 'b' || ch == 'B') { i.next(); return readBinaryValue(i, 32); } else { return readOctalValue(i, 11); } } return readDecimalValue(i, 10); }
/** * Calculate break positions eagerly parallel to reading text. */ public void setText(CharacterIterator ci) { int begin = ci.getBeginIndex(); text = new char[ci.getEndIndex() - begin]; int[] breaks0 = new int[text.length + 1]; int brIx = 0; breaks0[brIx++] = begin; int charIx = 0; boolean inWs = false; for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) { text[charIx] = c; boolean ws = Character.isWhitespace(c); if (inWs && !ws) { breaks0[brIx++] = charIx + begin; } inWs = ws; charIx++; } if (text.length > 0) { breaks0[brIx++] = text.length + begin; } System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx); }
/** * Advances the iterator one step backwards. * @return The position of the last boundary position before the * current iteration position */ @Override public int previous() { CharacterIterator text = getText(); // if we have cached break positions and we're still in the range // covered by them, just move one step backward in the cache if (cachedBreakPositions != null && positionInCache > 0) { --positionInCache; text.setIndex(cachedBreakPositions[positionInCache]); return cachedBreakPositions[positionInCache]; } // otherwise, dump the cache and use the inherited previous() method to move // backward. This may fill up the cache with new break positions, in which // case we have to mark our position in the cache else { cachedBreakPositions = null; int result = super.previous(); if (cachedBreakPositions != null) { positionInCache = cachedBreakPositions.length - 2; } return result; } }
private boolean valid(String input) { if ("".equals(input)) return true; boolean ret = true; it = new StringCharacterIterator(input); c = it.first(); col = 1; if (!value()) { ret = error("value", 1); } else { skipWhiteSpace(); if (c != CharacterIterator.DONE) { ret = error("end", col); } } return ret; }
private boolean literal(String text) { CharacterIterator ci = new StringCharacterIterator(text); char t = ci.first(); if (c != t) return false; int start = col; boolean ret = true; for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) { if (t != nextCharacter()) { ret = false; break; } } nextCharacter(); if (!ret) error("literal " + text, start); return ret; }
private boolean string() { if (c != '"') return false; int start = col; boolean escaped = false; for (nextCharacter(); c != CharacterIterator.DONE; nextCharacter()) { if (!escaped && c == '\\') { escaped = true; } else if (escaped) { if (!escape()) { return false; } escaped = false; } else if (c == '"') { nextCharacter(); return true; } } return error("quoted string", start); }
/** * Returns true if the specified position is a boundary position. As a side * effect, leaves the iterator pointing to the first boundary position at * or after "offset". * @param offset the offset to check. * @return True if "offset" is a boundary position. */ @Override public boolean isBoundary(int offset) { CharacterIterator text = getText(); checkOffset(offset, text); if (offset == text.getBeginIndex()) { return true; } // to check whether this is a boundary, we can use following() on the // position before the specified one and return true if the position we // get back is the one the user specified else { return following(offset - 1) == offset; } }
private void readWord() throws IOException { for (;;) { int c = -1; if (ci != null) { c = ci.current(); ci.next(); } if (in != null) { c = in.read(); } if (c < 0 || c == '=' || c == CharacterIterator.DONE) { padWord(); eof = true; return; } c = b64d(c); if (c >= 0) { word <<= 6; word |= c; count++; if (count > 3) { count = 3; return; } } } }
public static String readDecimalString(CharacterIterator i, int maxchars) { final StringBuffer buf = new StringBuffer(); if (peekAndEat(i, '-')) { buf.append('-'); } for (int cntr = 0; cntr < maxchars; cntr++) { final char c = i.current(); if (!Character.isDigit(c)) { break; } buf.append(c); i.next(); } return buf.toString(); }
@Override String format( AttributedCharacterIterator iterator, String preExponent) { int copyFromOffset = 0; StringBuilder result = new StringBuilder(); for ( iterator.first(); iterator.current() != CharacterIterator.DONE; ) { Map<Attribute, Object> attributeSet = iterator.getAttributes(); if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) { append( iterator, copyFromOffset, iterator.getRunStart(NumberFormat.Field.EXPONENT_SYMBOL), result); copyFromOffset = iterator.getRunLimit(NumberFormat.Field.EXPONENT_SYMBOL); iterator.setIndex(copyFromOffset); result.append(preExponent); result.append(beginMarkup); } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) { int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT); append( iterator, copyFromOffset, limit, result); copyFromOffset = limit; iterator.setIndex(copyFromOffset); result.append(endMarkup); } else { iterator.next(); } } append(iterator, copyFromOffset, iterator.getEndIndex(), result); return result.toString(); }
/** * Sets the {@link RuleBasedCollator} to be used for language-specific searching. * <p> * The iterator's position will not be changed by this method. * @param collator to use for this <tt>StringSearch</tt> * @throws IllegalArgumentException thrown when collator is null * @see #getCollator * @stable ICU 2.0 */ public void setCollator(RuleBasedCollator collator) { if (collator == null) { throw new IllegalArgumentException("Collator can not be null"); } collator_ = collator; ceMask_ = getMask(collator_.getStrength()); ULocale collLocale = collator.getLocale(ULocale.VALID_LOCALE); search_.internalBreakIter_ = BreakIterator.getCharacterInstance(collLocale == null ? ULocale.ROOT : collLocale); search_.internalBreakIter_.setText((CharacterIterator)search_.text().clone()); // We need to create a clone toShift_ = collator.isAlternateHandlingShifted(); variableTop_ = collator.getVariableTop(); textIter_ = new CollationElementIterator(pattern_.text_, collator); utilIter_ = new CollationElementIterator(pattern_.text_, collator); // initialize() _after_ setting the iterators for the new collator. initialize(); }
int matchesIgnoreCase(CharacterIterator iterator, int start, int limit) { int plength = this.pattern.length; if (plength == 0) return start; int index = start+plength; while (index <= limit) { int pindex = plength; int nindex = index+1; char ch; do { char ch1 = ch = iterator.setIndex(--index); char ch2 = this.pattern[--pindex]; if (ch1 != ch2) { ch1 = Character.toUpperCase(ch1); ch2 = Character.toUpperCase(ch2); if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) break; } if (pindex == 0) return index; } while (pindex > 0); index += this.shiftTable[ch % this.shiftTable.length]+1; if (index < nindex) index = nindex; } return -1; }
private static final void checkIteratorSubranges(AttributedCharacterIterator iterator, int[] expectedLimits) throws Exception { int previous = 0; char c = iterator.first(); for (int i = 0; i < expectedLimits.length; i++) { if (iterator.getRunStart() != previous || iterator.getRunLimit() != expectedLimits[i]) { throwException(iterator, "run boundaries are not as expected: " + iterator.getRunStart() + ", " + iterator.getRunLimit()); } previous = expectedLimits[i]; c = iterator.setIndex(previous); } if (c != CharacterIterator.DONE) { throwException(iterator, "iterator's run sequence doesn't end with DONE"); } }
public static int readDecimalValue(CharacterIterator i, int maxchars) { final StringBuffer buf = new StringBuffer(); if (peekAndEat(i, '-')) { buf.append('-'); } for (int cntr = 0; cntr < maxchars; cntr++) { final char c = i.current(); if (!Character.isDigit(c)) { break; } buf.append(c); i.next(); } return Integer.parseInt(buf.toString()); }
/** * * @return -1 if <var>iterator</var> does not contain this pattern. */ public int matches(CharacterIterator iterator, int start, int limit) { if (this.ignoreCase) return this.matchesIgnoreCase(iterator, start, limit); int plength = this.pattern.length; if (plength == 0) return start; int index = start+plength; while (index <= limit) { int pindex = plength; int nindex = index+1; char ch; do { if ((ch = iterator.setIndex(--index)) != this.pattern[--pindex]) break; if (pindex == 0) return index; } while (pindex > 0); index += this.shiftTable[ch % this.shiftTable.length]+1; if (index < nindex) index = nindex; } return -1; }
/** * @see UCharacterIterator#getText(char[]) */ @Override public int getText(char[] fillIn, int offset){ int length =iterator.getEndIndex() - iterator.getBeginIndex(); int currentIndex = iterator.getIndex(); if(offset < 0 || offset + length > fillIn.length){ throw new IndexOutOfBoundsException(Integer.toString(length)); } for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) { fillIn[offset++] = ch; } iterator.setIndex(currentIndex); return length; }
/** * Indicates whether or not this {@code Font} can display the * text specified by the {@code iter} starting at * {@code start} and ending at {@code limit}. * * @param iter a {@link CharacterIterator} object * @param start the specified starting offset into the specified * {@code CharacterIterator}. * @param limit the specified ending offset into the specified * {@code CharacterIterator}. * @return an offset into {@code iter} that points * to the first character in {@code iter} that this * {@code Font} cannot display; or {@code -1} if * this {@code Font} can display all characters in * {@code iter}. * @since 1.2 */ public int canDisplayUpTo(CharacterIterator iter, int start, int limit) { Font2D font2d = getFont2D(); char c = iter.setIndex(start); for (int i = start; i < limit; i++, c = iter.next()) { if (font2d.canDisplay(c)) { continue; } if (!Character.isHighSurrogate(c)) { return i; } char c2 = iter.next(); // c2 could be CharacterIterator.DONE which is not a low surrogate. if (!Character.isLowSurrogate(c2)) { return i; } if (!font2d.canDisplay(Character.toCodePoint(c, c2))) { return i; } i++; } return -1; }
public int prev() { char cp2 = iter.previous(); if (cp2 != CharacterIterator.DONE) { if (Character.isLowSurrogate(cp2)) { char cp1 = iter.previous(); if (Character.isHighSurrogate(cp1)) { return Character.toCodePoint(cp1, cp2); } iter.next(); } return cp2; } return DONE; }
/** * Return <tt>aText</tt> with all <tt>'<'</tt> and <tt>'>'</tt> characters * replaced by their escaped equivalents. */ @ApiMethod @Comment(value = "Return text with all '<' and '>' characters replaced by their escaped equivalents.") public static String toDisableTags(String text) { final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(text); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else { //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); }
/** * @see UCharacterIterator#previous() */ public int previous() { int i = iterator.previous(); if(i==CharacterIterator.DONE){ return DONE; } return i; }
public void runTest(Object ctx, int numReps) { TCContext tcctx = (TCContext)ctx; final Font font = tcctx.font; final CharacterIterator ci = tcctx.ci; final FontRenderContext frc = tcctx.frc; GlyphVector gv; do { gv = font.createGlyphVector(frc, ci); } while (--numReps >= 0); }
public Object read(CharacterIterator ci, int start) { it = ci; switch (start) { case FIRST: c = it.first(); break; case CURRENT: c = it.current(); break; case NEXT: c = it.next(); break; } return read(); }
/** * Creates a clone of this iterator. Clones the underlying character iterator. * @see UCharacterIterator#clone() */ public Object clone(){ try { CharacterIteratorWrapper result = (CharacterIteratorWrapper) super.clone(); result.iterator = (CharacterIterator)this.iterator.clone(); return result; } catch (CloneNotSupportedException e) { return null; // only invoked if bad underlying character iterator } }
public static boolean peekAndEat(CharacterIterator i, char c) { final char r = i.current(); if (r == c) { i.next(); return true; } return false; }
@Override public Object clone() { SafeCharIterator copy = null; try { copy = (SafeCharIterator) super.clone(); } catch(CloneNotSupportedException e) { throw new Error("Clone not supported: " + e); } CharacterIterator copyOfBase = (CharacterIterator) base.clone(); copy.base = copyOfBase; return copy; }
/** * Get string from @iter until end of string or until terminator is * encountered. * * @param iter Character iterator. */ private static String getStringUntil(CharacterIterator iter, char terminator) { StringBuilder sb = new StringBuilder(); for (char currentChar = iter.current(); currentChar != CharacterIterator.DONE && currentChar != terminator; currentChar = iter.next()) { sb.append(currentChar); } return sb.toString(); }
public static void skipWhiteSpace(CharacterIterator i) { while (true) { final char c = i.current(); if (c != ' ' && c != '\n' && c != '\t') { break; } i.next(); } }