Package org.bitbucket.bradleysmithllc.java_cl_parser.regexp

Examples of org.bitbucket.bradleysmithllc.java_cl_parser.regexp.EscapeFunctionExpression


public class EscapeFunctionTest
{
  @Test
  public void matches()
  {
    Assert.assertTrue(new EscapeFunctionExpression("\\{1}").matches());
    Assert.assertTrue(new EscapeFunctionExpression("\\{      1        }").matches());

    Assert.assertFalse(new EscapeFunctionExpression("\\ {1}").matches());
    Assert.assertFalse(new EscapeFunctionExpression("\\{1} ").matches());


    Assert.assertTrue(new EscapeFunctionExpression("\\\\{1}").matches());
    Assert.assertFalse(new EscapeFunctionExpression("\\{1").matches());
    Assert.assertFalse(new EscapeFunctionExpression("\\1}").matches());
    Assert.assertFalse(new EscapeFunctionExpression("\\{}").matches());
  }
View Full Code Here


  }

  @Test
  public void find()
  {
    Assert.assertTrue(new EscapeFunctionExpression("\\ \\{1} ").hasNext());
    Assert.assertTrue(new EscapeFunctionExpression("\\\\{1} ").hasNext());

    EscapeFunctionExpression escapeFunctionExpression = new EscapeFunctionExpression("\\{1}");
    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("1", escapeFunctionExpression.getEscapeFunction());
    Assert.assertFalse(escapeFunctionExpression.hasNext());

    escapeFunctionExpression = new EscapeFunctionExpression("\\{  1  }");
    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("  1  ", escapeFunctionExpression.getEscapeFunction());
    Assert.assertFalse(escapeFunctionExpression.hasNext());

    escapeFunctionExpression = new EscapeFunctionExpression("\\{  \\\\\\\\ [] \\\\\\\\  }");
    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("  \\\\\\\\ [] \\\\\\\\  ", escapeFunctionExpression.getEscapeFunction());
    Assert.assertEquals("\\", escapeFunctionExpression.getPreChar());
    Assert.assertTrue(escapeFunctionExpression.hasPreChar());
    Assert.assertFalse(escapeFunctionExpression.hasNext());

    escapeFunctionExpression = new EscapeFunctionExpression("  \\\\{Blah}\\{blah}\\\\{Blah}  ");
    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("\\\\", escapeFunctionExpression.getPreChar());
    Assert.assertEquals("Blah", escapeFunctionExpression.getEscapeFunction());
    Assert.assertTrue(escapeFunctionExpression.hasPreChar());

    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("blah", escapeFunctionExpression.getEscapeFunction());
    Assert.assertEquals("\\", escapeFunctionExpression.getPreChar());

    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("Blah", escapeFunctionExpression.getEscapeFunction());
    Assert.assertEquals("\\\\", escapeFunctionExpression.getPreChar());

    Assert.assertFalse(escapeFunctionExpression.hasNext());

    escapeFunctionExpression = new EscapeFunctionExpression("\\{\\\\s\\e}");
    Assert.assertTrue(escapeFunctionExpression.hasNext());
    Assert.assertEquals("\\", escapeFunctionExpression.getPreChar());
    Assert.assertEquals("\\\\s\\e", escapeFunctionExpression.getEscapeFunction());
    Assert.assertTrue(escapeFunctionExpression.hasPreChar());
  }
View Full Code Here

    // split on every value, ignoring escaped separators denoted by ESCAPE_CHARACTER_DEFAULT
    // pass over the input two times, once looking for \{} constructs, and the second
    // time resolving escape sequences
    StringBuilder builder = new StringBuilder();

    EscapeFunctionExpression escapeFunctionExpression = new EscapeFunctionExpression(textValue);

    int lastEnd = 0;

    while (escapeFunctionExpression.hasNext())
    {
      String escText = escapeFunctionExpression.getEscapeFunction();

      // check the preChar - an even number means pass it through.
      // in the case of an odd number, remove the last one and escape the sequence

      String pre = escapeFunctionExpression.getPreChar();

      if (pre.length() % 2 == 0)
      {
        // pass all through unmodified
        builder.append(textValue.substring(lastEnd, escapeFunctionExpression.start()));
        builder.append(escapeFunctionExpression.group(0));
      }
      else
      {
        if (pre.length() > 1)
        {
          // remove one char, for our escape
          builder.append(pre.substring(1));
        }

        builder.append(textValue.substring(lastEnd, escapeFunctionExpression.start()));

        builder.append(escapeText(escText, c));
      }

      lastEnd = escapeFunctionExpression.end();
    }

    if (lastEnd < textValue.length())
    {
      builder.append(textValue.substring(lastEnd));
View Full Code Here

TOP

Related Classes of org.bitbucket.bradleysmithllc.java_cl_parser.regexp.EscapeFunctionExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.