Skip to content

Commit

Permalink
Merge pull request #1992 from usethesource/chore/remove-commons-lang-…
Browse files Browse the repository at this point in the history
…dependency

Rewrote squeeze to avoid commons-lang dependency
  • Loading branch information
DavyLandman authored Jul 17, 2024
2 parents cfa4c57 + 47c7ac9 commit 6c2e1b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,6 @@
<artifactId>commons-math</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency> <!-- used by the compression uri feature-->
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down
29 changes: 25 additions & 4 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@

import org.apache.commons.codec.CodecPolicy;
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.lang.CharSetUtils;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.JavaCompilation;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
Expand Down Expand Up @@ -3071,9 +3070,31 @@ public IString trim(IString s) {
}

public IString squeeze(IString src, IString charSet) {
//@{http://commons.apache.org/lang/api-2.6/index.html?org/apache/commons/lang/text/package-summary.html}
String s = CharSetUtils.squeeze(src.getValue(), charSet.getValue());
return values.string(s);
if (charSet.getValue().isEmpty()) {
return src;
}
final Pattern isCharset = Pattern.compile("[" + charSet.getValue() + "]", Pattern.UNICODE_CHARACTER_CLASS);
StringBuilder result = new StringBuilder(src.length());
var chars = src.iterator();
int previousMatch = -1;
while (chars.hasNext()) {
int cp = chars.nextInt();
if (cp == previousMatch) {
// swallow
continue;
}

String c = Character.toString(cp);
if (isCharset.matcher(c).matches()) {
previousMatch = cp;
// swallow the next
}
else {
previousMatch = -1;
}
result.append(c);
}
return values.string(result.toString());
}

public IString capitalize(IString src) {
Expand Down
17 changes: 17 additions & 0 deletions src/org/rascalmpl/library/lang/rascal/tests/basic/Strings1.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,23 @@ test bool tstSplit(str S1, str S2) = areOverlapping(S1,S2) || isEmpty(S1) || isE

// squeeze

test bool tstSqueeze1(str S) = /<c:[a-zA-Z]><c>/ !:= squeeze(S, "a-zA-Z");
test bool tstSqueeze2(str S) = squeeze(S, "") == S;
test bool tstSqueeze3(str S) {
if (/<c:[a-zA-Z]><c>/ := S) {
return /<c><c>/ := squeeze(S, "0-9");
}
return true;
}

test bool tstSqueezeUnicode() = squeeze("Hi 🍝🍝World", "🍝") == "Hi 🍝World";
test bool tstSqueezeCase1() = squeeze("abc", "a-c") == "abc";
test bool tstSqueezeCase2() = squeeze("aabc", "a-c") == "abc";
test bool tstSqueezeCase3() = squeeze("aabcc", "a-c") == "abc";
test bool tstSqueezeCase4() = squeeze("aabbcc", "a-c") == "abc";
test bool tstSqueezeCase5() = squeeze("aaabc", "a-c") == "abc";


test bool tstStartsWith(str S1, str S2) = startsWith(S1+S2, S1);

test bool tstSubstring1(str S){
Expand Down

0 comments on commit 6c2e1b9

Please sign in to comment.