Skip to content

Commit

Permalink
Revert "this fixes #2005 for the existing squeeze function on strings…
Browse files Browse the repository at this point in the history
…, but the new one is still broken on unicode for a different reason"

This reverts commit 58b2283.
  • Loading branch information
jurgenvinju committed Jul 21, 2024
1 parent 58b2283 commit 67568f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/org/rascalmpl/interpreter/TraversalEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.rascalmpl.interpreter.utils.Cases.CaseBlock;
import org.rascalmpl.types.RascalType;
import org.rascalmpl.interpreter.utils.Names;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.parsetrees.ITree;
import org.rascalmpl.values.parsetrees.TreeAdapter;
Expand Down Expand Up @@ -690,11 +689,11 @@ public IValue traverseTop(IValue subject, CaseBlockList casesOrRules, TraverseRe
* Performance issue: we create a lot of garbage by producing all these substrings.
*/
public IValue traverseString(IValue subject, CaseBlockList casesOrRules, TraverseResult tr){
IString subjectString = (IString) subject;
String subjectString = ((IString) subject).getValue();
int len = subjectString.length();
int subjectCursor = 0;
int subjectCursorForResult = 0;
IString replacementString = null;
StringBuffer replacementString = null;
boolean hasMatched = false;
boolean hasChanged = false;

Expand All @@ -704,7 +703,7 @@ public IValue traverseString(IValue subject, CaseBlockList casesOrRules, Travers

while (subjectCursor < len) {
try {
IString substring = subjectString.substring(subjectCursor, len);
IString substring = eval.getValueFactory().string(subjectString.substring(subjectCursor, len));
IValue subresult = substring;
tr.matched = false;
tr.changed = false;
Expand Down Expand Up @@ -742,16 +741,16 @@ else if (lastPattern instanceof LiteralPattern || lastPattern instanceof TypedVa

// Create replacementString when this is the first replacement
if (replacementString == null) {
replacementString = IRascalValueFactory.getInstance().string("");
replacementString = new StringBuffer();
}

// Copy string before the match to the replacement string
for (; subjectCursorForResult < subjectCursor + start; subjectCursorForResult++){
replacementString = replacementString.concat(IRascalValueFactory.getInstance().string(subjectString.charAt(subjectCursorForResult)));
replacementString.append(subjectString.charAt(subjectCursorForResult));
}
subjectCursorForResult = subjectCursor + end;
// Copy replacement into replacement string
replacementString = replacementString.concat((IString) repl);
replacementString.append(((IString)repl).getValue());

tr.matched = true;
tr.changed = true;
Expand All @@ -771,9 +770,9 @@ else if (lastPattern instanceof LiteralPattern || lastPattern instanceof TypedVa

// Copy remaining characters of subject string into replacement string
for (; subjectCursorForResult < len; subjectCursorForResult++){
replacementString = replacementString.concat(IRascalValueFactory.getInstance().string(subjectString.charAt(subjectCursorForResult)));
replacementString.append(subjectString.charAt(subjectCursorForResult));
}

return replacementString;
return eval.getValueFactory().string(replacementString.toString());
}
}
18 changes: 9 additions & 9 deletions src/org/rascalmpl/library/lang/rascal/tests/basic/Strings1.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,21 @@ test bool tstSqueezeCase4() = squeeze("aabbcc", "a-c") == "abc";
test bool tstSqueezeCase5() = squeeze("aaabc", "a-c") == "abc";

// second squeeze
test bool tstSqueeze21(str S) = /<c:[a-zA-Z]><c>/ !:= squeeze(S, #[a-zA-Z]);
test bool tstSqueeze22(str S) = squeeze(S, #[]) == S;
test bool tstSqueeze23(str S) {
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 tstSqueeze2Unicode() = squeeze("Hi 🍝🍝World", #[🍝]) == "Hi 🍝World";
test bool tstSqueeze2Case1() = squeeze("abc", #[a-c]) == "abc";
test bool tstSqueeze2Case2() = squeeze("aabc", #[a-c]) == "abc";
test bool tstSqueeze2Case3() = squeeze("aabcc", #[a-c]) == "abc";
test bool tstSqueeze2Case4() = squeeze("aabbcc", #[a-c]) == "abc";
test bool tstSqueeze2Case5() = squeeze("aaabc", #[a-c]) == "abc";
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);

Expand Down

0 comments on commit 67568f2

Please sign in to comment.