Java StringBuilder.appendCodePoint Examples

Java StringBuilder.appendCodePoint - 30 examples found. These are the top rated real world Java examples of StringBuilder.appendCodePoint extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 /**
  * Given a string, return an array of tokens. The separator can be escaped with the '\' character.
  * The '\' character may also be escaped by the '\' character.
  *
  * @param s the string to tokenize.
  * @param separator the separator char.
  * @param maxTokens the maxmimum number of tokens returned. If the max is reached, the remaining
  *     part of s is appended to the end of the last token.
  * @return an array of tokens.
  */
 public static String[] tokenize(String s, char separator, int maxTokens) {
   List tokens = new ArrayList();
   StringBuilder token = new StringBuilder();
   boolean prevIsEscapeChar = false;
   for (int i = 0; i < s.length(); i += Character.charCount(i)) {
     int currentChar = s.codePointAt(i);
     if (prevIsEscapeChar) {
       // Case 1:  escaped character
       token.appendCodePoint(currentChar);
       prevIsEscapeChar = false;
     } else if (currentChar == separator && tokens.size() < maxTokens - 1) {
       // Case 2:  separator
       tokens.add(token.toString());
       token = new StringBuilder();
     } else if (currentChar == '\\') {
       // Case 3:  escape character
       prevIsEscapeChar = true;
     } else {
       // Case 4:  regular character
       token.appendCodePoint(currentChar);
     }
   }
   if (token.length() > 0) {
     tokens.add(token.toString());
   }
   return (String[]) tokens.toArray(new String[] {});
 }
Example #2
0
 /**
  * Escapes a character sequence so that it is valid XML.
  *
  * @param s The character sequence.
  * @return The escaped version of the character sequence.
  */
 public static String escapeXML(CharSequence s) {
   // double quote -- quot
   // ampersand -- amp
   // less than -- lt
   // greater than -- gt
   // apostrophe -- apos
   StringBuilder sb = new StringBuilder(s.length() * 2);
   for (int i = 0; i < s.length(); ) {
     int codePoint = Character.codePointAt(s, i);
     if (codePoint == '<') {
       sb.append(LT);
     } else if (codePoint == '>') {
       sb.append(GT);
     } else if (codePoint == '\"') {
       sb.append(QUOT);
     } else if (codePoint == '&') {
       sb.append(AMP);
     } else if (codePoint == '\'') {
       sb.append(APOS);
     } else {
       sb.appendCodePoint(codePoint);
     }
     i += Character.charCount(codePoint);
   }
   return sb.toString();
 }
Example #3
0
 /** Decodes escapes in an identifier */
 public static String decodeCssIdentifier(CharSequence ident) {
   StringBuilder sb = null;
   int pos = 0;
   for (int i = 0, n = ident.length(); i < n; ) {
     if (ident.charAt(i) == '\\') {
       if (sb == null) {
         sb = new StringBuilder();
       }
       sb.append(ident, pos, i);
       int codepoint = 0;
       while (++i < n && isHexChar(ident.charAt(i))) {
         char ch = ident.charAt(i);
         codepoint <<= 4;
         if (ch >= '0' && ch <= '9') {
           codepoint |= ch - '0';
         } else if (ch >= 'a' && ch <= 'f') {
           codepoint |= ch + 10 - 'a';
         } else {
           codepoint |= ch + 10 - 'A';
         }
       }
       sb.appendCodePoint(codepoint < Character.MAX_CODE_POINT ? codepoint : 0xfffd);
       if (i < n && isSpaceChar(ident.charAt(i))) {
         ++i;
       }
       pos = i;
     } else {
       ++i;
     }
   }
   if (sb == null) {
     return ident.toString();
   }
   return sb.append(ident, pos, ident.length()).toString();
 }
 /* map bits to unicode codepoints */
 private static String mapInt(int codePointTable[], int i) {
   StringBuilder sb = new StringBuilder();
   String binary = Integer.toBinaryString(i);
   for (int j = 0; j < binary.length(); j++)
     sb.appendCodePoint(codePointTable[binary.charAt(j) - '0']);
   return sb.toString();
 }
Example #5
0
 public static String enquoteCString(String value, char delimiter) {
   final int length = value.length();
   StringBuilder result = new StringBuilder();
   result.append(delimiter);
   for (int offset = 0, codepoint; offset < length; offset += Character.charCount(codepoint)) {
     codepoint = value.codePointAt(offset);
     if (codepoint > 0xFF) {
       result.appendCodePoint(codepoint);
     } else if (codepoint == delimiter) {
       result.append("\\" + delimiter);
     } else if (codepoint == '\\') {
       result.append("\\\\");
     } else if (codepoint == '\n') {
       result.append("\\n");
     } else if (codepoint == '\t') {
       result.append("\\t");
     } else if (codepoint == '\r') {
       result.append("\\r");
     } else if (codepoint == '\f') {
       result.append("\\f");
     } else if (codepoint >= 32 && codepoint < 127) {
       result.append((char) codepoint);
     } else if (codepoint <= 0xff) {
       result.append("\\");
       result.append(String.format("%03o", codepoint));
     }
   }
   result.append(delimiter);
   return result.toString();
 }
Example #6
0
    public String read() throws IOException {
      StringBuilder sb = new StringBuilder();
      if (offset == bufferSize) {
        offset = 0;
        bufferSize = in.read(buffer);
      }

      if (bufferSize == -1 || bufferSize == 0) throw new IOException("No new bytes");

      for (;
          buffer[offset] == ' '
              || buffer[offset] == '\t'
              || buffer[offset] == '\n'
              || buffer[offset] == '\r';
          ++offset) {
        if (offset == bufferSize - 1) {
          offset = -1;
          bufferSize = in.read(buffer);
        }
      }
      for (; offset < bufferSize; ++offset) {
        if (buffer[offset] == ' '
            || buffer[offset] == '\t'
            || buffer[offset] == '\n'
            || buffer[offset] == '\r') break;
        if (Character.isValidCodePoint(buffer[offset])) {
          sb.appendCodePoint(buffer[offset]);
        }
        if (offset == bufferSize - 1) {
          offset = -1;
          bufferSize = in.read(buffer);
        }
      }
      return sb.toString();
    }