/** * Set the iterator to analyze a new piece of text. This function resets the current iteration * position to the beginning of the text. * * @param newText An iterator over the text to analyze. */ @Override public void setText(CharacterIterator newText) { // Test iterator to see if we need to wrap it in a SafeCharIterator. // The correct behavior for CharacterIterators is to allow the // position to be set to the endpoint of the iterator. Many // CharacterIterators do not uphold this, so this is a workaround // to permit them to use this class. int end = newText.getEndIndex(); boolean goodIterator; try { newText.setIndex(end); // some buggy iterators throw an exception here goodIterator = newText.getIndex() == end; } catch (IllegalArgumentException e) { goodIterator = false; } if (goodIterator) { text = newText; } else { text = new SafeCharIterator(newText); } text.first(); cachedLastKnownBreak = BreakIterator.DONE; }
/** escape characters */ private void string(Object obj) { this.add('"'); CharacterIterator it = new StringCharacterIterator(obj.toString()); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c == '"') { this.add("\\\""); } else if (c == '\\') { this.add("\\\\"); } else if (c == '/') { this.add("\\/"); } else if (c == '\b') { this.add("\\b"); } else if (c == '\f') { this.add("\\f"); } else if (c == '\n') { this.add("\\n"); } else if (c == '\r') { this.add("\\r"); } else if (c == '\t') { this.add("\\t"); } else if (Character.isISOControl(c)) { this.unicode(c); } else { this.add(c); } } this.add('"'); }
protected String[] csvRowSplit(String row, char sep) { // remove trailing CRLF if (row.endsWith("\r\n")) { row = row.substring(0, row.length() - 2); } else if (row.endsWith("\n")) { row = row.substring(0, row.length() - 1); } // limit is necessary to prevent split from trimming the empty values at the end of the row // FIXME: split() only works if no values contain the separator string // String[] rowValues = row.split(Pattern.quote(sep), correctValues.length); List<String> rowValues = new Vector<String>(); StringBuffer aValue = new StringBuffer(); CharacterIterator i = new StringCharacterIterator(row); boolean insideQuote = false; for (char c = i.first(); c != CharacterIterator.DONE; c = i.next()) { if (c == sep && !insideQuote) { // value is finished rowValues.add(aValue.toString()); // clear buffer aValue.delete(0, aValue.length()); } else { aValue.append(c); } // if first or last quote met if (c == QUOTE) { insideQuote = !insideQuote; } } // add last value rowValues.add(aValue.toString()); return rowValues.toArray(new String[0]); }
/** * Sets the current iteration position to the beginning of the text. (i.e., the * CharacterIterator's starting offset). * * @return The offset of the beginning of the text. */ @Override public int first() { CharacterIterator t = getText(); t.first(); return t.getIndex(); }
/** * For the given string, returns the number of UTF-8 bytes required to encode the string. * * @param string text to encode * @return number of UTF-8 bytes required to encode */ public static int utf8Length(String string) { CharacterIterator iter = new StringCharacterIterator(string); char ch = iter.first(); int size = 0; while (ch != CharacterIterator.DONE) { if ((ch >= 0xD800) && (ch < 0xDC00)) { // surrogate pair? char trail = iter.next(); if ((trail > 0xDBFF) && (trail < 0xE000)) { // valid pair size += 4; } else { // invalid pair size += 3; iter.previous(); // rewind one } } else if (ch < 0x80) { size++; } else if (ch < 0x800) { size += 2; } else { // ch < 0x10000, that is, the largest char value size += 3; } ch = iter.next(); } return size; }
public static String parsePath(String uri, Map<String, String> patterns) { if (uri == null) { return null; } else if (StringUtils.isBlank(uri)) { return String.valueOf(SLASH); } CharacterIterator ci = new StringCharacterIterator(uri); StringBuilder pathBuffer = new StringBuilder(); char c = ci.first(); if (c == CharacterIterator.DONE) { return String.valueOf(SLASH); } do { if (c == OPEN) { String regexBuffer = cutParameter(ci, patterns); if (regexBuffer == null) { LOGGER.warn("Operation path \"" + uri + "\" contains syntax error."); return null; } pathBuffer.append(regexBuffer); } else { int length = pathBuffer.length(); if (!(c == SLASH && (length != 0 && pathBuffer.charAt(length - 1) == SLASH))) { pathBuffer.append(c); } } } while ((c = ci.next()) != CharacterIterator.DONE); return pathBuffer.toString(); }
/** {@inheritDoc} */ @Override public String decode(String encodedText) { if (encodedText == null) return null; if (encodedText.length() == 0) return encodedText; final StringBuilder result = new StringBuilder(); final CharacterIterator iter = new StringCharacterIterator(encodedText); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (c == ESCAPE_CHARACTER) { boolean foundEscapedCharacter = false; // Found the first character in a potential escape sequence, so grab the next two characters // ... char hexChar1 = iter.next(); char hexChar2 = hexChar1 != CharacterIterator.DONE ? iter.next() : CharacterIterator.DONE; if (hexChar2 != CharacterIterator.DONE) { // We found two more characters, but ensure they form a valid hexadecimal number ... int hexNum1 = Character.digit(hexChar1, 16); int hexNum2 = Character.digit(hexChar2, 16); if (hexNum1 > -1 && hexNum2 > -1) { foundEscapedCharacter = true; result.append((char) (hexNum1 * 16 + hexNum2)); } } if (!foundEscapedCharacter) { result.append(c); if (hexChar1 != CharacterIterator.DONE) result.append(hexChar1); if (hexChar2 != CharacterIterator.DONE) result.append(hexChar2); } } else { result.append(c); } } return result.toString(); }
public void setText(CharacterIterator newText) { this.charIter = newText; StringBuilder sb = new StringBuilder(); for (char c = newText.first(); c != CharacterIterator.DONE; c = newText.next()) { sb.append(c); } setTextImpl(this.addr, sb.toString()); }
/** * {@link #expression_}から{@link #giFormulae_}を構成します。 戦略<br> * ・一文字ずつ読み込んでアルファベットの大文字がきたら照合<br> * ・ここでキューに文字がある場合はそれを組み合わせて照合する。 ・Propertyに存在しない場合はキューに入れる(キューの上限は1) * ・アルファベット大文字以外の文字がきたらキューをクリアーして表示文字に追加 */ public void setup() { List<GiFormula> giFormulaList = new ArrayList<GiFormula>(); StringBuilder text = new StringBuilder(expression_.length()); char queue = CharacterIterator.DONE; CharacterIterator ip = new StringCharacterIterator(expression_); for (char c = ip.first(); c != CharacterIterator.DONE; c = ip.next()) { if (!Strings.isUppercaseRomanAlphabet(c)) { if (queue != CharacterIterator.DONE) { text.append(queue); queue = CharacterIterator.DONE; } text.append(c); continue; } StringBuilder id = new StringBuilder(2); // IDのサイズは高々2 if (queue != CharacterIterator.DONE) { id.append(queue); } id.append(c); SgfId sgfType = UEnum.find(SgfId.class, id.toString()); if ((sgfType == null) || !PropertyType.GAME_INFO.equals(sgfType.propertyType())) { if (queue != CharacterIterator.DONE) { text.append(queue); } queue = c; continue; } if (text.length() != 0) { giFormulaList.add(new GiLabelFormula(text.toString())); text.delete(0, text.length()); } GiTextFormula giTextFormula = new GiTextFormula(sgfType); String value = emptyMap_.get(sgfType); if (value != null) { giTextFormula.setEmpty(value); } List<GiEnum> giEnums = enumMap_.get(sgfType); if (giEnums != null) { for (GiEnum giEnum : giEnums) { String value2 = giEnum.getValue(); if (value2.startsWith("%")) { // $NON-NLS-1$ String key = value2.substring(1); value2 = messages_s_.get(key); giEnum.setValue(value2); } giTextFormula.addEnum(giEnum); } } giFormulaList.add(giTextFormula); queue = CharacterIterator.DONE; } if (text.length() != 0) { giFormulaList.add(new GiLabelFormula(text.toString())); } giFormulae_ = giFormulaList.toArray(new GiFormula[giFormulaList.size()]); }
/** * Return whether the supplied string contains any of the supplied characters. * * @param str the string to be examined; may not be null * @param chars the characters to be found within the supplied string; may be zero-length * @return true if the supplied string contains at least one of the supplied characters, or false * otherwise */ public static boolean containsAnyOf(String str, char... chars) { CharacterIterator iter = new StringCharacterIterator(str); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { for (char match : chars) { if (c == match) return true; } } return false; }
/** * Return whether this term contains any unescaped wildcard characters (e.g., one of '*', '?', * '%', or '_'). * * @return true if this term contains unescaped wildcard characters, or false otherwise */ public boolean containsWildcards() { if (this.value.length() == 0) return false; CharacterIterator iter = new StringCharacterIterator(this.value); boolean skipNext = false; for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (skipNext) { skipNext = false; continue; } if (c == '*' || c == '?' || c == '%' || c == '_') return true; if (c == '\\') skipNext = true; } return false; }
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(); }
protected String encode(String text, BitSet safeChars) { final StringBuilder result = new StringBuilder(); final CharacterIterator iter = new StringCharacterIterator(text); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (safeChars.get(c)) { // Safe character, so just pass through ... result.append(c); } else { // The character is not a safe character, and must be escaped ... result.append(ESCAPE_CHARACTER); result.append(Character.toLowerCase(Character.forDigit(c / 16, 16))); result.append(Character.toLowerCase(Character.forDigit(c % 16, 16))); } } return result.toString(); }
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 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; }
/** * Determine whether the supplied string represents a well-formed fully-qualified Java classname. * This utility method enforces no conventions (e.g., packages are all lowercase) nor checks * whether the class is available on the classpath. * * @param classname * @return true if the string is a fully-qualified class name */ public static boolean isFullyQualifiedClassname(String classname) { if (classname == null) return false; String[] parts = classname.split("[\\.]"); if (parts.length == 0) return false; for (String part : parts) { CharacterIterator iter = new StringCharacterIterator(part); // Check first character (there should at least be one character for each part) ... char c = iter.first(); if (c == CharacterIterator.DONE) return false; if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c)) return false; c = iter.next(); // Check the remaining characters, if there are any ... while (c != CharacterIterator.DONE) { if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c)) return false; c = iter.next(); } } return true; }
public void selectionDone(SelectionEvent evt) { if (!useUnixTextSelection) return; Object o = evt.getSelection(); if (!(o instanceof CharacterIterator)) return; CharacterIterator iter = (CharacterIterator) o; // first see if we can access the clipboard SecurityManager securityManager; securityManager = System.getSecurityManager(); if (securityManager != null) { try { securityManager.checkSystemClipboardAccess(); } catch (SecurityException e) { return; // Can't access clipboard. } } int sz = iter.getEndIndex() - iter.getBeginIndex(); if (sz == 0) return; char[] cbuff = new char[sz]; cbuff[0] = iter.first(); for (int i = 1; i < cbuff.length; ++i) { cbuff[i] = iter.next(); } final String strSel = new String(cbuff); // HACK: getSystemClipboard sometimes deadlocks on // linux when called from the AWT Thread. The Thread // creation prevents that. new Thread() { public void run() { Clipboard cb; cb = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection sel; sel = new StringSelection(strSel); cb.setContents(sel, sel); } }.start(); }
public World() { // Parse foobar float x = 0, y = 0; CharacterIterator it = new StringCharacterIterator(foobar); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next(), x++) { switch (ch) { case '+': walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f))); case '-': walls.add(new Wall(new Vector2f(x, y + 0.5f), new Vector2f(x + 1.0f, y + 0.5f))); break; case '|': walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f))); break; case '\n': x = -1; y++; break; } } singleton = this; }
/*--------------------------------------------------------------------------*/ private String revisePath(String in) { // This was "stolen" from com.izforge.izpack.util.SelfModifier StringBuffer sb = new StringBuffer(); CharacterIterator iter = new StringCharacterIterator(in); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (c == '%') { char c1 = iter.next(); if (c1 != CharacterIterator.DONE) { int i1 = Character.digit(c1, 16); char c2 = iter.next(); if (c2 != CharacterIterator.DONE) { int i2 = Character.digit(c2, 16); sb.append((char) ((i1 << 4) + i2)); } } } else { sb.append(c); } } String path = sb.toString(); return path; }
public static HashMap<Character, Bitmap> getImages() { BitmapRegionDecoder decoder = null; if (sImages != null) return sImages; InputStream is = sContext.getResources().openRawResource(EN); try { decoder = BitmapRegionDecoder.newInstance(is, false); } catch (IOException ex) { } int h = decoder.getHeight(); sImages = new HashMap<Character, Bitmap>(); Rect r = new Rect(0, 0, h, h); for (char c = ABC.first(); c != CharacterIterator.DONE; c = ABC.next(), r.offset(h, 0)) { Bitmap bmp = decoder.decodeRegion(r, null); sImages.put(c, bmp); } Log.d("getImages", "float=" + sContext.getResources().getDisplayMetrics().density); return sImages; }
/** * Decodes an Uri with % characters. * * @param uri String with the uri possibly containing % characters. * @return The decoded Uri */ private static String decodeUri(String uri) { if (uri.indexOf('%') == -1) { return uri; } StringBuffer sb = new StringBuffer(); CharacterIterator iter = new StringCharacterIterator(uri); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (c == '%') { char c1 = iter.next(); if (c1 != CharacterIterator.DONE) { int i1 = Character.digit(c1, 16); char c2 = iter.next(); if (c2 != CharacterIterator.DONE) { int i2 = Character.digit(c2, 16); sb.append((char) ((i1 << 4) + i2)); } } } else { sb.append(c); } } String path = sb.toString(); return path; }
public String[] parseLine(String line) throws IOException { final List<String> values = new ArrayList<String>(); int[] valueWidths = _configuration.getValueWidths(); if (line == null) { return null; } StringBuilder nextValue = new StringBuilder(); int valueIndex = 0; final CharacterIterator it = new StringCharacterIterator(line); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { nextValue.append(c); final int valueWidth; if (_configuration.isConstantValueWidth()) { valueWidth = _configuration.getFixedValueWidth(); } else { if (valueIndex >= valueWidths.length) { if (_configuration.isFailOnInconsistentLineWidth()) { String[] result = values.toArray(new String[values.size()]); throw new InconsistentValueWidthException(result, line, _rowNumber + 1); } else { // silently ignore the inconsistency break; } } valueWidth = _configuration.getValueWidth(valueIndex); } if (nextValue.length() == valueWidth) { // write the value values.add(nextValue.toString().trim()); nextValue = new StringBuilder(); valueIndex++; } } if (nextValue.length() > 0) { values.add(nextValue.toString().trim()); } String[] result = values.toArray(new String[values.size()]); if (!_configuration.isFailOnInconsistentLineWidth() && !_configuration.isConstantValueWidth()) { if (result.length != valueWidths.length) { String[] correctedResult = new String[valueWidths.length]; for (int i = 0; i < result.length && i < valueWidths.length; i++) { correctedResult[i] = result[i]; } result = correctedResult; } } if (_configuration.isFailOnInconsistentLineWidth()) { _rowNumber++; if (_configuration.isConstantValueWidth()) { if (line.length() % _configuration.getFixedValueWidth() != 0) { throw new InconsistentValueWidthException(result, line, _rowNumber); } } else { if (result.length != values.size()) { throw new InconsistentValueWidthException(result, line, _rowNumber); } if (line.length() != _expectedLineLength) { throw new InconsistentValueWidthException(result, line, _rowNumber); } } } return result; }