@Override public final char[] findNextChar( int match, int esc ) { return findNextChar(false, false, match, esc); }
@Override public final int nextChar() { ensureBuffer(); return ch = readBuf[index++]; }
@Test public void findStringWithFindNextCharWithEscape() { String testString = "abc \"train a\\b\" abc"; initSourceWithString(testString); boolean found = false; loop: while (source.hasChar()) { int i = source.currentChar(); switch ( i ) { case '"': found = true; break loop; } source.nextChar(); } boolean ok = found || die("not found"); source.nextChar(); char [] chars = source.findNextChar ( '"', '\\' ) ; ok &= Chr.equals( chars, "train a\\b".toCharArray ()) || die(new String(chars)); ok |= source.currentChar () == ' ' || die("" + (char)source.currentChar()); }
@Override public void skipWhiteSpace() { try { index = CharScanner.skipWhiteSpace( readBuf, index, length ); if (index >= length && more) { ensureBuffer(); skipWhiteSpace(); } } catch ( Exception ex ) { ex.printStackTrace(); String str = CharScanner.errorDetails ( "skipWhiteSpaceIfNeeded issue", readBuf, index, ch ); Exceptions.handle ( ex, str, "\n\nLENGTH", length, "INDEX", index ); } }
public char[] readNumber( ) { try { ensureBuffer(); char [] results = CharScanner.readNumber( readBuf, index, length); index += results.length; if (index >= length && more) { ensureBuffer(); if (length!=0) { char results2[] = readNumber(); return Chr.add(results, results2); } else { return results; } } else { return results; } } catch (Exception ex) { String str = CharScanner.errorDetails ( "readNumber issue", readBuf, index, ch ); return Exceptions.handle ( char[].class, str, ex ); } }
ensureBuffer(); //grow the buffer and read in if needed ensureBuffer(); char results2[] = findNextChar(true, wasEscapeChar, match, esc); return Chr.add(results, results2); } else {
@Test public void readNumberTest2() { String testString = "123 456"; initSourceWithString(testString); char [] numberChars = source.readNumber(); boolean ok = Chr.equals ( "123".toCharArray (), numberChars ) || die( new String(numberChars) ) ; source.skipWhiteSpace(); numberChars = source.readNumber(); ok = Chr.equals ( "456".toCharArray (), numberChars ) || die( new String(numberChars) ) ; }
@Before public void setup() { ByteArrayInputStream inputStream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8)); //IOInputStream stream = IOInputStream.input(null, 10).input(inputStream); source = new ReaderCharacterSource ( new InputStreamReader(inputStream) ); }
@Test public void consumeIfMatchTest() { String testString = "abc true abc"; initSourceWithString(testString); boolean found = false; loop: while (source.hasChar()) { int i = source.currentChar(); switch ( i ) { case 't': found = true; break loop; } source.nextChar(); } boolean ok = found || die("not found"); ok |= source.consumeIfMatch ( "true".toCharArray() ) || die(); ok |= source.safeNextChar () == ' ' || die("" + (char)source.safeNextChar ()); }
@Test public void skipWhiteSpace() { String testString = "a b c"; initSourceWithString(testString); boolean ok = source.nextChar() == 'a' || die("" + (char)source.currentChar()); source.nextChar(); source.skipWhiteSpace(); ok &= source.nextChar() == 'b' || die("$$" + (char)source.currentChar() + "$$"); source.nextChar(); source.skipWhiteSpace(); ok &= source.nextChar() == 'c' || die("" + (char)source.currentChar()); source.skipWhiteSpace(); }
@Test public void readNumberTest3() { String testString = "123456789"; initSourceWithString(testString); char [] numberChars = source.readNumber(); boolean ok = Chr.equals ( "123456789".toCharArray (), numberChars ) || die( new String(numberChars) ) ; }
private void ensureBuffer() { try { if (index >= length && !done) { readNextBuffer (); } else if (done && index >=length) { more = false; }else { more = true; } } catch ( Exception ex ) { String str = CharScanner.errorDetails ( "ensureBuffer issue", readBuf, index, ch ); Exceptions.handle ( str, ex ); } }
@Override public final boolean consumeIfMatch( char[] match ) { try { char [] _chars = readBuf; int i=0; int idx = index; boolean ok = true; if ( idx + match.length > length ) { readForToken (); } for (; i < match.length; i++, idx++) { ok &= ( match[i] == _chars[idx] ); if (!ok) break; } if ( ok ) { index = idx; return true; } else { return false; } } catch (Exception ex) { String str = CharScanner.errorDetails ( "consumeIfMatch issue", readBuf, index, ch ); return Exceptions.handle ( boolean.class, str, ex ); } }
@Test public void basicCurrentChar() { int i = source.nextChar(); boolean ok = i == 'a' || die( "" + (char) i); i = source.currentChar(); ok = i == 'b' || die( "" + (char) i); }
@Test public void findStringWithFindNextCharWithEscapeOfQuote() { String testString = "abc \"train\\\"\" abc0123456789"; initSourceWithString(testString); boolean found = false; loop: while (source.hasChar()) { int i = source.currentChar(); switch ( i ) { case '"': found = true; break loop; } source.nextChar(); } boolean ok = found || die("not found"); source.nextChar(); char [] chars = source.findNextChar ( '"', '\\' ) ; ok &= Chr.equals( chars, "train\\\"".toCharArray ()) || die(new String(chars)); ok |= source.currentChar () == ' ' || die("" + (char)source.currentChar()); }
@Override public void skipWhiteSpace() { try { index = CharScanner.skipWhiteSpace( readBuf, index, length ); if (index >= length && more) { ensureBuffer(); skipWhiteSpace(); } } catch ( Exception ex ) { ex.printStackTrace(); String str = CharScanner.errorDetails ( "skipWhiteSpaceIfNeeded issue", readBuf, index, ch ); Exceptions.handle ( ex, str, "\n\nLENGTH", length, "INDEX", index ); } }
public char[] readNumber( ) { try { ensureBuffer(); char [] results = CharScanner.readNumber( readBuf, index, length); index += results.length; if (index >= length && more) { ensureBuffer(); if (length!=0) { char results2[] = readNumber(); return Chr.add(results, results2); } else { return results; } } else { return results; } } catch (Exception ex) { String str = CharScanner.errorDetails ( "readNumber issue", readBuf, index, ch ); return Exceptions.handle ( char[].class, str, ex ); } }
ensureBuffer(); //grow the buffer and read in if needed ensureBuffer(); char results2[] = findNextChar(true, wasEscapeChar, match, esc); return Chr.add(results, results2); } else {
private void initSourceWithString(String testString) { ByteArrayInputStream inputStream = new InMemoryInputStream(testString.getBytes(StandardCharsets.UTF_8)); source = new ReaderCharacterSource ( new InputStreamReader(inputStream) ); }
@Test public void consumeIfMatchNotMatchingTest() { String testString = "abc train abc"; initSourceWithString(testString); boolean found = false; loop: while (source.hasChar()) { int i = source.currentChar(); switch ( i ) { case 't': found = true; break loop; } source.nextChar (); } boolean ok = found || die("not found"); ok |= !source.consumeIfMatch ( "true".toCharArray() ) || die(); ok |= source.currentChar() == 't' || die("" + (char)source.currentChar()); }