Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
  /** 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('"');
  }
Beispiel #3
0
 private static Object parseValue(CharacterIterator it) {
   switch (it.current()) {
     case '{':
       return parseObject(it);
     case '[':
       return parseArray(it);
     case '"':
       return parseString(it);
     case '-':
     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9':
       return parseNumber(it);
     case 't':
       parseText(Boolean.TRUE.toString(), it);
       return Boolean.TRUE;
     case 'f':
       parseText(Boolean.FALSE.toString(), it);
       return Boolean.FALSE;
     case 'n':
       parseText(NULL, it);
       return null;
   }
   throw error(
       "Bad JSON starting character '" + it.current() + "'", it); // $NON-NLS-1$ //$NON-NLS-2$;
 }
 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();
 }
Beispiel #5
0
 /** {@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();
 }
  /**
   * 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();
  }
Beispiel #7
0
  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]);
  }
  /**
   * 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;
  }
Beispiel #9
0
 public int next() {
   int i = iterator.current();
   iterator.next();
   if (i == CharacterIterator.DONE) {
     return DONE;
   }
   return i;
 }
 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());
 }
Beispiel #11
0
 /**
  * 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;
 }
 /** Returns the position of next character. */
 private int getNextIndex() {
   int index = text.getIndex() + getCurrentCodePointCount();
   int endIndex = text.getEndIndex();
   if (index > endIndex) {
     return endIndex;
   } else {
     return index;
   }
 }
 /** Returns next character */
 int getNext() {
   int index = text.getIndex();
   int endIndex = text.getEndIndex();
   if (index == endIndex || (index += getCurrentCodePointCount()) >= endIndex) {
     return CharacterIterator.DONE;
   }
   text.setIndex(index);
   return getCurrent();
 }
Beispiel #14
0
  /**
   * {@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()]);
  }
  /**
   * Sets the current iteration position to the end of the text. (i.e., the CharacterIterator's
   * ending offset).
   *
   * @return The text's past-the-end offset.
   */
  @Override
  public int last() {
    CharacterIterator t = getText();

    // I'm not sure why, but t.last() returns the offset of the last character,
    // rather than the past-the-end offset
    t.setIndex(t.getEndIndex());
    return t.getIndex();
  }
 /**
  * Sets the iterator to refer to the last boundary position before the specified position.
  *
  * @offset The position to begin searching for a break from.
  * @return The position of the last boundary before the starting position.
  */
 @Override
 public int preceding(int offset) {
   // if we start by updating the current iteration position to the
   // position specified by the caller, we can just use previous()
   // to carry out this operation
   CharacterIterator text = getText();
   checkOffset(offset, text);
   text.setIndex(offset);
   return previous();
 }
Beispiel #17
0
 public static Tile[] fromPuzzleString(String puzzleAsString) {
   Tile[] puzzle = new Tile[puzzleAsString.length()];
   CharacterIterator charIterator = new StringCharacterIterator(puzzleAsString);
   for (int y = 0; y < 9; ++y) {
     for (int x = 0; x < 9; ++x) {
       puzzle[y * 9 + x] = new Tile(x, y, charIterator.current() - '0');
       charIterator.next();
     }
   }
   return puzzle;
 }
 /** Returns the count of next character. */
 private int getCurrentCodePointCount() {
   char c1 = text.current();
   if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) {
     char c2 = text.next();
     text.previous();
     if (Character.isLowSurrogate(c2)) {
       return 2;
     }
   }
   return 1;
 }
 /** Returns current character */
 int getCurrent() {
   char c1 = text.current();
   if (Character.isHighSurrogate(c1) && text.getIndex() < text.getEndIndex()) {
     char c2 = text.next();
     text.previous();
     if (Character.isLowSurrogate(c2)) {
       return Character.toCodePoint(c1, c2);
     }
   }
   return (int) c1;
 }
Beispiel #20
0
 protected void selected(CharPos start, CharPos end) {
   StringBuilder buf = new StringBuilder();
   synchronized (msgs) {
     boolean sel = false;
     for (Message msg : msgs) {
       if (!(msg.text() instanceof RichText)) continue;
       RichText rt = (RichText) msg.text();
       RichText.Part part = null;
       if (sel) {
         part = rt.parts;
       } else if (msg == start.msg) {
         sel = true;
         for (part = rt.parts; part != null; part = part.next) {
           if (part == start.part) break;
         }
       }
       if (sel) {
         for (; part != null; part = part.next) {
           if (!(part instanceof RichText.TextPart)) continue;
           RichText.TextPart tp = (RichText.TextPart) part;
           CharacterIterator iter = tp.ti();
           int sch;
           if (tp == start.part) sch = tp.start + start.ch.getInsertionIndex();
           else sch = tp.start;
           int ech;
           if (tp == end.part) ech = tp.start + end.ch.getInsertionIndex();
           else ech = tp.end;
           for (int i = sch; i < ech; i++) buf.append(iter.setIndex(i));
           if (part == end.part) {
             sel = false;
             break;
           }
           buf.append(' ');
         }
         if (sel) buf.append('\n');
       }
       if (msg == end.msg) break;
     }
   }
   Clipboard cl;
   if ((cl = java.awt.Toolkit.getDefaultToolkit().getSystemSelection()) == null)
     cl = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
   try {
     final CharPos ownsel = selstart;
     cl.setContents(
         new StringSelection(buf.toString()),
         new ClipboardOwner() {
           public void lostOwnership(Clipboard cl, Transferable tr) {
             if (selstart == ownsel) selstart = selend = null;
           }
         });
   } catch (IllegalStateException e) {
   }
 }
Beispiel #21
0
 private static void parseText(String string, CharacterIterator it) {
   int length = string.length();
   char c = it.current();
   for (int i = 0; i < length; i++) {
     if (c != string.charAt(i))
       throw error(
           "expected to parse '" + string + "' but character " + (i + 1) + " was '" + c + "'",
           it); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$;
     c = it.next();
   }
 }
 /** Returns previous character */
 private int getPrevious() {
   char c2 = text.previous();
   if (Character.isLowSurrogate(c2) && text.getIndex() > text.getBeginIndex()) {
     char c1 = text.previous();
     if (Character.isHighSurrogate(c1)) {
       return Character.toCodePoint(c1, c2);
     } else {
       text.next();
     }
   }
   return (int) c2;
 }
Beispiel #23
0
  private static List parseArray(CharacterIterator it) {
    it.next();
    parseWhitespace(it);
    if (it.current() == ']') {
      it.next();
      return Collections.EMPTY_LIST;
    }

    List list = new ArrayList();
    while (true) {
      Object value = parseValue(it);
      list.add(value);
      parseWhitespace(it);
      if (it.current() == ',') {
        it.next();
        parseWhitespace(it);
        continue;
      }

      if (it.current() != ']')
        throw error(
            "expected an array close ']' but was '" + it.current() + "'",
            it); //$NON-NLS-1$ //$NON-NLS-2$
      break;
    }
    it.next();
    return list;
  }
 /**
  * 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;
 }
Beispiel #25
0
 private static Object parseNumber(CharacterIterator it) {
   StringBuffer buffer = new StringBuffer();
   char c = it.current();
   while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E') {
     buffer.append(c);
     c = it.next();
   }
   try {
     return new BigDecimal(buffer.toString());
   } catch (NumberFormatException e) {
     throw error(
         "expected a number but was '" + buffer.toString() + "'",
         it); //$NON-NLS-1$ //$NON-NLS-2$;
   }
 }
  /**
   * 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;
    }
  }
 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();
 }
Beispiel #28
0
 public int current() {
   int c = iterator.current();
   if (c == CharacterIterator.DONE) {
     return DONE;
   }
   return c;
 }
Beispiel #29
0
 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();
 }
Beispiel #30
0
 public int previous() {
   int i = iterator.previous();
   if (i == CharacterIterator.DONE) {
     return DONE;
   }
   return i;
 }