@Test public void chompBalanced() { TokenQueue tq = new TokenQueue(":contains(one (two) three) four"); String pre = tq.consumeTo("("); String guts = tq.chompBalanced('(', ')'); String remainder = tq.remainder(); assertEquals(":contains", pre); assertEquals("one (two) three", guts); assertEquals(" four", remainder); }
public String chompBalanced(char open, char close) { int start = -1; int end = -1; int depth = 0; char last = 0; boolean inQuote = false; do { if (isEmpty()) break; Character c = consume(); if (last == 0 || last != ESC) { if ((c.equals('\'') || c.equals('"')) && c != open) inQuote = !inQuote; if (inQuote) continue; if (c.equals(open)) { depth++; if (start == -1) start = pos; } else if (c.equals(close)) depth--; } if (depth > 0 && last != 0) end = pos; last = c; } while (depth > 0); final String out = (end >= 0) ? queue.substring(start, end) : ""; if (depth > 0) { Validate.fail("Did not find balanced marker at '" + out + "'"); } return out; }
TokenQueue { public String chompBalanced(char open, char close) { int start = -1; int end = -1; int depth = 0; char last = 0; boolean inQuote = false; do { if (isEmpty()) break; Character c = consume(); if (last == 0 || last != ESC) { if ((c.equals('\'') || c.equals('"')) && c != open) inQuote = !inQuote; if (inQuote) continue; if (c.equals(open)) { depth++; if (start == -1) start = pos; } else if (c.equals(close)) depth--; } if (depth > 0 && last != 0) end = pos; last = c; } while (depth > 0); final String out = (end >= 0) ? queue.substring(start, end) : ""; if (depth > 0) { Validate.fail("Did not find balanced marker at '" + out + "'"); } return out; } }
TokenQueue { public String chompBalanced(char open, char close) { int start = -1; int end = -1; int depth = 0; char last = 0; boolean inQuote = false; do { if (isEmpty()) break; Character c = consume(); if (last == 0 || last != ESC) { if ((c.equals('\'') || c.equals('"')) && c != open) inQuote = !inQuote; if (inQuote) continue; if (c.equals(open)) { depth++; if (start == -1) start = pos; } else if (c.equals(close)) depth--; } if (depth > 0 && last != 0) end = pos; last = c; } while (depth > 0); final String out = (end >= 0) ? queue.substring(start, end) : ""; if (depth > 0) { Validate.fail("Did not find balanced marker at '" + out + "'"); } return out; } TokenQueue(String data); }
TokenQueue { public String chompBalanced(char open, char close) { int start = -1; int end = -1; int depth = 0; char last = 0; boolean inQuote = false; do { if (isEmpty()) break; Character c = consume(); if (last == 0 || last != ESC) { if ((c.equals('\'') || c.equals('"')) && c != open) inQuote = !inQuote; if (inQuote) continue; if (c.equals(open)) { depth++; if (start == -1) start = pos; } else if (c.equals(close)) depth--; } if (depth > 0 && last != 0) end = pos; last = c; } while (depth > 0); final String out = (end >= 0) ? queue.substring(start, end) : ""; if (depth > 0) { Validate.fail("Did not find balanced marker at '" + out + "'"); } return out; } TokenQueue(String data); boolean isEmpty(); char peek(); void addFirst(Character c); void addFirst(String seq); boolean matches(String seq); boolean matchesCS(String seq); boolean matchesAny(String... seq); boolean matchesAny(char... seq); boolean matchesStartTag(); boolean matchChomp(String seq); boolean matchesWhitespace(); boolean matchesWord(); void advance(); char consume(); void consume(String seq); String consumeTo(String seq); String consumeToIgnoreCase(String seq); String consumeToAny(String... seq); String chompTo(String seq); String chompToIgnoreCase(String seq); String chompBalanced(char open, char close); static String unescape(String in); boolean consumeWhitespace(); String consumeWord(); String consumeTagName(); String consumeElementSelector(); String consumeCssIdentifier(); String consumeAttributeKey(); String remainder(); @Override String toString(); }
TokenQueue { public String chompBalanced(char open, char close) { int start = -1; int end = -1; int depth = 0; char last = 0; boolean inQuote = false; do { if (isEmpty()) break; Character c = consume(); if (last == 0 || last != ESC) { if ((c.equals('\'') || c.equals('"')) && c != open) inQuote = !inQuote; if (inQuote) continue; if (c.equals(open)) { depth++; if (start == -1) start = pos; } else if (c.equals(close)) depth--; } if (depth > 0 && last != 0) end = pos; last = c; } while (depth > 0); final String out = (end >= 0) ? queue.substring(start, end) : ""; if (depth > 0) { Validate.fail("Did not find balanced marker at '" + out + "'"); } return out; } TokenQueue(String data); boolean isEmpty(); char peek(); void addFirst(Character c); void addFirst(String seq); boolean matches(String seq); boolean matchesCS(String seq); boolean matchesAny(String... seq); boolean matchesAny(char... seq); boolean matchesStartTag(); boolean matchChomp(String seq); boolean matchesWhitespace(); boolean matchesWord(); void advance(); char consume(); void consume(String seq); String consumeTo(String seq); String consumeToIgnoreCase(String seq); String consumeToAny(String... seq); String chompTo(String seq); String chompToIgnoreCase(String seq); String chompBalanced(char open, char close); static String unescape(String in); boolean consumeWhitespace(); String consumeWord(); String consumeTagName(); String consumeElementSelector(); String consumeCssIdentifier(); String consumeAttributeKey(); String remainder(); @Override String toString();  }