Ejemplo n.º 1
0
  private void finishComposition() {
    int len = buffer.length();
    if (len == 6 && format != SPECIAL_ESCAPE) {
      char codePoint = (char) getCodePoint(buffer, 2, 5);
      if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) {
        buffer.setLength(0);
        buffer.append(codePoint);
        sendCommittedText();
        return;
      }
    } else if (len == 8 && format == SPECIAL_ESCAPE) {
      int codePoint = getCodePoint(buffer, 2, 7);
      if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) {
        buffer.setLength(0);
        buffer.appendCodePoint(codePoint);
        sendCommittedText();
        return;
      }
    } else if (len == 12 && format == SURROGATE_PAIR) {
      char[] codePoint = {(char) getCodePoint(buffer, 2, 5), (char) getCodePoint(buffer, 8, 11)};
      if (Character.isHighSurrogate(codePoint[0]) && Character.isLowSurrogate(codePoint[1])) {
        buffer.setLength(0);
        buffer.append(codePoint);
        sendCommittedText();
        return;
      }
    }

    beep();
  }
Ejemplo n.º 2
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();
    }
Ejemplo n.º 3
0
  /**
   * Formats all are legal both upper/lower case
   *
   * @param input encoded character using percent characters (such as URL encoding)
   */
  @Override
  public Character decodeCharacter(PushbackString input) {
    input.mark();
    Character first = input.next();
    if (first == null) {
      input.reset();
      return null;
    }

    // If this is not an encoded character, return null
    if (first != '%') {
      input.reset();
      return null;
    }

    // Search for exactly 2 hex digits following
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 2; i++) {
      Character c = input.nextHex();
      if (c != null) sb.append(c);
    }
    if (sb.length() == 2) {
      try {
        // Parse the hex digit and create a character
        int i = Integer.parseInt(sb.toString(), 16);
        if (Character.isValidCodePoint(i)) {
          return (char) i;
        }
      } catch (NumberFormatException ignored) {
      }
    }
    input.reset();
    return null;
  }
Ejemplo n.º 4
0
 /**
  * Generate a random string containing UTC characters.
  *
  * @param size Number of characters in the string.
  * @return The random string.
  */
 public String nextString(final int size) {
   char[] out = new char[size];
   for (int i = 0; i < size; ++i) {
     int c = random.nextInt(128);
     if (c == '\n' || c == '\t' || c == '\f' || c == '\r' || c == '\b' || (c >= 32 && c < 127)) {
       out[i] = (char) c;
     } else {
       c = random.nextInt(2048);
       if (c >= 160) {
         out[i] = (char) c;
       } else {
         c = random.nextInt(1 << 16);
         if (c < 32
             || (127 <= c && c < 160)
             || (8192 <= c && c < 8448)
             || !Character.isValidCodePoint(c)) {
           out[i] = '?';
         } else {
           out[i] = (char) c;
         }
       }
     }
   }
   return String.valueOf(out);
 }
Ejemplo n.º 5
0
  @Test
  public void testUTF8() {
    for (int i = 1; i <= Character.MAX_CODE_POINT; i++) {

      if (!Character.isValidCodePoint(i)) continue;

      String orig = new String(Character.toChars(i));
      BSONObject a = new BasicBSONObject(orig, orig);
      BSONObject b = BSON.decode(BSON.encode(a));
      assertEquals(a, b);
    }
  }
Ejemplo n.º 6
0
  /**
   * Returns the advance width of the specified character in this <code>Font</code>. The advance is
   * the distance from the leftmost point to the rightmost point on the character's baseline. Note
   * that the advance of a <code>String</code> is not necessarily the sum of the advances of its
   * characters.
   *
   * <p>This method doesn't validate the specified character to be a valid Unicode code point. The
   * caller must validate the character value using {@link java.lang.Character#isValidCodePoint(int)
   * Character.isValidCodePoint} if necessary.
   *
   * @param codePoint the character (Unicode code point) to be measured
   * @return the advance width of the specified character in the <code>Font</code> described by this
   *     <code>FontMetrics</code> object.
   * @see #charsWidth(char[], int, int)
   * @see #stringWidth(String)
   */
  public int charWidth(int codePoint) {
    if (!Character.isValidCodePoint(codePoint)) {
      codePoint = 0xffff; // substitute missing glyph width
    }

    if (codePoint < 256) {
      return getWidths()[codePoint];
    } else {
      char[] buffer = new char[2];
      int len = Character.toChars(codePoint, buffer, 0);
      return charsWidth(buffer, 0, len);
    }
  }
  /**
   * Reads next line from the input and:
   *
   * <ul>
   *   <li>Converts ascii-encoded \\uxxxx chars to normal characters.
   *   <li>Converts \r, \n and \t to CR, line feed and tab.
   *   <li>But! Keeps a backspace in '\ ', '\=', '\:' etc (non-trimmable space or
   *       non-key-value-breaking :-) equals).
   *       <ul>
   *         Change from BufferedReader to LinebreakPreservingReader was part of fix for bug 1462566
   */
  protected String getNextLine(LinebreakPreservingReader reader)
      throws IOException, TranslationException {
    String ascii = reader.readLine();
    if (ascii == null) {
      return null;
    }

    StringBuilder result = new StringBuilder();
    for (int cp, len = ascii.length(), i = 0; i < len; i += Character.charCount(cp)) {
      cp = ascii.codePointAt(i);
      if (cp == '\\' && ascii.codePointCount(i, len) > 1) {
        i += Character.charCount(cp);
        cp = ascii.codePointAt(i);
        if (cp != 'u') {
          if (cp == 'n') {
            cp = '\n';
          } else if (cp == 'r') {
            cp = '\r';
          } else if (cp == 't') {
            cp = '\t';
          } else {
            result.append('\\');
          }
        } else if (dontUnescapeULiterals) {
          // Put back the \ we swallowed
          result.append('\\');
        } else {
          // checking if the string is long enough
          if (ascii.codePointCount(i, len) < 1 + 4) {
            throw new TranslationException(OStrings.getString("RBFH_ERROR_ILLEGAL_U_SEQUENCE"));
          }
          int uStart = ascii.offsetByCodePoints(i, 1);
          int uEnd = ascii.offsetByCodePoints(uStart, 4);
          String uStr = ascii.substring(uStart, uEnd);
          try {
            cp = Integer.parseInt(uStr, 16);
            if (!Character.isValidCodePoint(cp)) {
              throw new TranslationException(OStrings.getString("RBFH_ERROR_ILLEGAL_U_SEQUENCE"));
            }
            i = uEnd - Character.charCount(cp);
          } catch (NumberFormatException ex) {
            throw new TranslationException(OStrings.getString("RBFH_ERROR_ILLEGAL_U_SEQUENCE"), ex);
          }
        }
      }
      result.appendCodePoint(cp);
    }

    return result.toString();
  }
Ejemplo n.º 8
0
 /*     */ protected CoderResult decodeLoop(
     ByteBuffer paramByteBuffer, CharBuffer paramCharBuffer)
       /*     */ {
   /*  65 */ if (paramByteBuffer.remaining() < 4) /*  66 */ return CoderResult.UNDERFLOW;
   /*  67 */ int i = paramByteBuffer.position();
   /*     */ try
   /*     */ {
     /*     */ int j;
     /*  70 */ if (this.currentBO == 0) {
       /*  71 */ j =
           (paramByteBuffer.get() & 0xFF) << 24
               | (paramByteBuffer.get() & 0xFF) << 16
               | (paramByteBuffer.get() & 0xFF) << 8
               | paramByteBuffer.get() & 0xFF;
       /*     */
       /*  75 */ if ((j == 65279) && (this.expectedBO != 2)) {
         /*  76 */ this.currentBO = 1;
         /*  77 */ i += 4;
         /*  78 */ } else if ((j == -131072) && (this.expectedBO != 1)) {
         /*  79 */ this.currentBO = 2;
         /*  80 */ i += 4;
         /*     */ } else {
         /*  82 */ if (this.expectedBO == 0) /*  83 */ this.currentBO = 1;
         /*     */ else /*  85 */ this.currentBO = this.expectedBO;
         /*  86 */ paramByteBuffer.position(i);
         /*     */ }
       /*     */ }
     /*     */ CoderResult localCoderResult;
     /*  89 */ while (paramByteBuffer.remaining() >= 4) {
       /*  90 */ j = getCP(paramByteBuffer);
       /*  91 */ if (Character.isBmpCodePoint(j)) {
         /*  92 */ if (!paramCharBuffer.hasRemaining()) /*  93 */ return CoderResult.OVERFLOW;
         /*  94 */ i += 4;
         /*  95 */ paramCharBuffer.put((char) j);
         /*  96 */ } else if (Character.isValidCodePoint(j)) {
         /*  97 */ if (paramCharBuffer.remaining() < 2) /*  98 */ return CoderResult.OVERFLOW;
         /*  99 */ i += 4;
         /* 100 */ paramCharBuffer.put(Character.highSurrogate(j));
         /* 101 */ paramCharBuffer.put(Character.lowSurrogate(j));
         /*     */ } else {
         /* 103 */ return CoderResult.malformedForLength(4);
         /*     */ }
       /*     */ }
     /* 106 */ return CoderResult.UNDERFLOW;
     /*     */ } finally {
     /* 108 */ paramByteBuffer.position(i);
     /*     */ }
   /*     */ }
 private static boolean containsUEscapeAt(String text, int offset) {
   if (text.codePointCount(offset, text.length()) < 1 + 1 + 4) {
     return false;
   }
   if (text.codePointAt(text.offsetByCodePoints(offset, 1)) != 'u') {
     return false;
   }
   int uStart = text.offsetByCodePoints(offset, 2);
   int uEnd = text.offsetByCodePoints(uStart, 4);
   String uStr = text.substring(uStart, uEnd);
   try {
     int uChr = Integer.parseInt(uStr, 16);
     return Character.isValidCodePoint(uChr);
   } catch (NumberFormatException ex) {
     return false;
   }
 }
Ejemplo n.º 10
0
 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
   if (src.remaining() < 4) return CoderResult.UNDERFLOW;
   int mark = src.position();
   int cp;
   try {
     if (currentBO == NONE) {
       cp =
           ((src.get() & 0xff) << 24)
               | ((src.get() & 0xff) << 16)
               | ((src.get() & 0xff) << 8)
               | (src.get() & 0xff);
       if (cp == BOM_BIG && expectedBO != LITTLE) {
         currentBO = BIG;
         mark += 4;
       } else if (cp == BOM_LITTLE && expectedBO != BIG) {
         currentBO = LITTLE;
         mark += 4;
       } else {
         if (expectedBO == NONE) currentBO = BIG;
         else currentBO = expectedBO;
         src.position(mark);
       }
     }
     while (src.remaining() >= 4) {
       cp = getCP(src);
       if (Character.isBmpCodePoint(cp)) {
         if (!dst.hasRemaining()) return CoderResult.OVERFLOW;
         mark += 4;
         dst.put((char) cp);
       } else if (Character.isValidCodePoint(cp)) {
         if (dst.remaining() < 2) return CoderResult.OVERFLOW;
         mark += 4;
         dst.put(Character.highSurrogate(cp));
         dst.put(Character.lowSurrogate(cp));
       } else {
         return CoderResult.malformedForLength(4);
       }
     }
     return CoderResult.UNDERFLOW;
   } finally {
     src.position(mark);
   }
 }
Ejemplo n.º 11
0
 private static boolean isMonospaced(FontMetrics fontMetrics) {
   boolean isMonospaced = true;
   int charWidth = -1;
   for (int codePoint = 0; codePoint < 128; codePoint++) {
     if (Character.isValidCodePoint(codePoint)) {
       char character = (char) codePoint;
       if (isWordCharacter(character)) {
         int w = fontMetrics.charWidth(character);
         if (charWidth != -1) {
           if (w != charWidth) {
             isMonospaced = false;
             break;
           }
         } else {
           charWidth = w;
         }
       }
     }
   }
   return isMonospaced;
 }
Ejemplo n.º 12
0
 /**
  * Escape control characters in a string and append them to the string buffer
  *
  * @param string String to be written
  * @param sb String builder
  * @throws CharConversionException Invalid Unicode character
  */
 private static void escapeString(String string, StringBuilder sb) throws CharConversionException {
   if (string.length() == 0) {
     return;
   }
   //
   // Find the next special character in the string
   //
   int start = 0;
   Matcher matcher = pattern.matcher(string);
   while (matcher.find(start)) {
     int pos = matcher.start();
     if (pos > start) {
       sb.append(string.substring(start, pos));
     }
     start = pos + 1;
     //
     // Check for a valid Unicode codepoint
     //
     int ch = string.codePointAt(pos);
     if (!Character.isValidCodePoint(ch)) {
       throw new CharConversionException("Invalid Unicode character in JSON string value");
     }
     //
     // Process a supplementary codepoint
     //
     if (Character.isSupplementaryCodePoint(ch)) {
       sb.appendCodePoint(ch);
       start++;
       continue;
     }
     //
     // Escape control characters
     //
     char c = string.charAt(pos);
     switch (c) {
       case '"':
         sb.append("\\\"");
         break;
       case '\\':
         sb.append("\\\\");
         break;
       case '\b':
         sb.append("\\b");
         break;
       case '\f':
         sb.append("\\f");
         break;
       case '\n':
         sb.append("\\n");
         break;
       case '\r':
         sb.append("\\r");
         break;
       case '\t':
         sb.append("\\t");
         break;
       case '/':
         sb.append("\\/");
         break;
       default:
         if ((c >= '\u0000' && c <= '\u001F')
             || (c >= '\u007F' && c <= '\u009F')
             || (c >= '\u2000' && c <= '\u20FF')) {
           sb.append("\\u").append(String.format("%04X", (int) c));
         } else {
           sb.append(c);
         }
     }
   }
   //
   // Append the remainder of the string
   //
   if (start == 0) {
     sb.append(string);
   } else if (start < string.length()) {
     sb.append(string.substring(start));
   }
 }