@Test
  public void shouldParseItemAndDiscardSpaces() {
    Parser parser = Parsers.token(Parsers.integer());

    assertThat(parser.parse("1 \t\n"), is(equalTo(Parser.result(1L, ""))));
    assertThat(parser.parse("1"), is(equalTo(Parser.result(1L, ""))));
    assertThat(parser.parse(""), is(equalTo(Parser.error("Can not parse item"))));
  }
Esempio n. 2
0
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int dlASCII = dp + Math.min(sl - sp, dl - dp);

      // ASCII only loop
      while (dp < dlASCII && sa[sp] < '\u0080') da[dp++] = (byte) sa[sp++];
      while (sp < sl) {
        char c = sa[sp];
        if (c < 0x80) {
          // Have at most seven bits
          if (dp >= dl) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) c;
        } else if (c < 0x800) {
          // 2 bytes, 11 bits
          if (dl - dp < 2) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xc0 | (c >> 6));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        } else if (isSurrogate(c)) {
          // Have a surrogate pair
          if (sgp == null) sgp = new Parser();
          int uc = sgp.parse(c, sa, sp, sl);
          if (uc < 0) {
            updatePositions(src, sp, dst, dp);
            return sgp.error();
          }
          if (dl - dp < 4) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xf0 | ((uc >> 18)));
          da[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
          da[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (uc & 0x3f));
          sp++; // 2 chars
        } else {
          // 3 bytes, 16 bits
          if (dl - dp < 3) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xe0 | ((c >> 12)));
          da[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        }
        sp++;
      }
      updatePositions(src, sp, dst, dp);
      return CoderResult.UNDERFLOW;
    }
Esempio n. 3
0
 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
   int mark = src.position();
   while (src.hasRemaining()) {
     char c = src.get();
     if (c < 0x80) {
       // Have at most seven bits
       if (!dst.hasRemaining()) return overflow(src, mark);
       dst.put((byte) c);
     } else if (c < 0x800) {
       // 2 bytes, 11 bits
       if (dst.remaining() < 2) return overflow(src, mark);
       dst.put((byte) (0xc0 | (c >> 6)));
       dst.put((byte) (0x80 | (c & 0x3f)));
     } else if (isSurrogate(c)) {
       // Have a surrogate pair
       if (sgp == null) sgp = new Parser();
       int uc = sgp.parse(c, src);
       if (uc < 0) {
         src.position(mark);
         return sgp.error();
       }
       if (dst.remaining() < 4) return overflow(src, mark);
       dst.put((byte) (0xf0 | ((uc >> 18))));
       dst.put((byte) (0x80 | ((uc >> 12) & 0x3f)));
       dst.put((byte) (0x80 | ((uc >> 6) & 0x3f)));
       dst.put((byte) (0x80 | (uc & 0x3f)));
       mark++; // 2 chars
     } else {
       // 3 bytes, 16 bits
       if (dst.remaining() < 3) return overflow(src, mark);
       dst.put((byte) (0xe0 | ((c >> 12))));
       dst.put((byte) (0x80 | ((c >> 6) & 0x3f)));
       dst.put((byte) (0x80 | (c & 0x3f)));
     }
     mark++;
   }
   src.position(mark);
   return CoderResult.UNDERFLOW;
 }