Skip to content

Commit

Permalink
trim abstract semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
VincenzoArceri committed Jul 9, 2023
1 parent b61aee0 commit 5976f94
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,8 @@ public RegexAutomaton repeat(long n) {
return emptyString();
return toRegex().simplify().repeat(n).toAutomaton(this).minimize();
}

public RegexAutomaton trim() {
return toRegex().trim().toAutomaton(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,8 @@ else if (intv.interval.isFinite()) {
} else
return new Tarsis(a.repeat(intv.interval.getLow().toLong()).concat(a.star()));
}

public Tarsis trim() {
return new Tarsis(this.a.trim());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.unive.lisa.analysis.string.tarsis;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import it.unive.lisa.util.numeric.MathNumberConversionException;

public class TrimTest {

@Test
public void repeatSingleString() throws MathNumberConversionException {
RegexAutomaton abc = RegexAutomaton.string("abc");
RegexAutomaton ws = RegexAutomaton.string(" ");
RegexAutomaton epsilon = RegexAutomaton.string("");
RegexAutomaton abc_with_ws = RegexAutomaton.string(" a b c ");

Tarsis a = new Tarsis(abc);
Tarsis b = new Tarsis(ws);
Tarsis c = new Tarsis(abc_with_ws);

// "abc".trim() = "abc"
assertTrue(a.trim().getAutomaton().isEqualTo(abc));

// " ".trim() = ""
assertTrue(b.trim().getAutomaton().isEqualTo(epsilon));

// " a b c ".trim() = "a b c"
assertTrue(c.trim().getAutomaton().isEqualTo(RegexAutomaton.string("a b c")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,9 @@ public RegularExpression repeat(long n) {
r.simplify();
return r;
}

@Override
public RegularExpression trim() {
return new Atom(this.string.trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,14 @@ public RegularExpression repeat(long n) {
r = new Comp(r, this);
return r.simplify();
}

@Override
public RegularExpression trim() {

RegularExpression trimFirst = first.trim().simplify();
if (trimFirst.isEmpty())
return second.trim();
else
return new Comp(trimFirst, second).simplify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,9 @@ protected int compareToAux(RegularExpression other) {
public RegularExpression repeat(long n) {
return this;
}

@Override
public RegularExpression trim() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,9 @@ protected int compareToAux(RegularExpression other) {
public RegularExpression repeat(long n) {
return new Or(first.repeat(n), second.repeat(n)).simplify();
}

@Override
public RegularExpression trim() {
return new Or(first.trim(), second.trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public String toString() {

public abstract RegularExpression repeat(long n);

public abstract RegularExpression trim();
/**
* Yields {@code true} if and only if this regular expression corresponds to
* the empty string or to no strings at all.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,9 @@ protected int compareToAux(RegularExpression other) {
public RegularExpression repeat(long n) {
return this;
}

@Override
public RegularExpression trim() {
return new Star(op.trim());
}
}

0 comments on commit 5976f94

Please sign in to comment.