Skip to content

Commit 38d1917

Browse files
committed
StringBuiltins: remove TruffleBoundary for strip/lstrip/rstrip
1 parent 05bc6f0 commit 38d1917

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_string.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,3 +1080,19 @@ def test_strip_whitespace():
10801080
assert 'hello ' == ' hello '.lstrip(None)
10811081
assert ' hello' == ' hello '.rstrip(None)
10821082
assert 'hello' == 'hello'.strip(None)
1083+
1084+
1085+
def test_strip_with_sep():
1086+
# strip/lstrip/rstrip with str arg
1087+
assert 'hello' == 'xyzzyhelloxyzzy'.strip('xyz')
1088+
assert 'helloxyzzy' == 'xyzzyhelloxyzzy'.lstrip('xyz')
1089+
assert 'xyzzyhello' == 'xyzzyhelloxyzzy'.rstrip('xyz')
1090+
assert 'hello' == 'hello'.strip('xyz')
1091+
assert '' == 'mississippi'.strip('mississippi')
1092+
1093+
# only trim the start and end; does not strip internal characters
1094+
assert 'mississipp' == 'mississippi'.strip('i')
1095+
1096+
assertRaises(TypeError, 'hello', 'strip', 42, 42)
1097+
assertRaises(TypeError, 'hello', 'lstrip', 42, 42)
1098+
assertRaises(TypeError, 'hello', 'rstrip', 42, 42)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.nio.charset.CodingErrorAction;
5858
import java.util.Arrays;
5959
import java.util.List;
60-
import java.util.regex.Pattern;
6160

6261
import com.oracle.graal.python.builtins.Builtin;
6362
import com.oracle.graal.python.builtins.CoreFunctions;
@@ -1359,9 +1358,8 @@ public String doReplace(String self, String old, String with, int maxsplit) {
13591358
@TypeSystemReference(PythonArithmeticTypes.class)
13601359
public abstract static class StripNode extends PythonBuiltinNode {
13611360
@Specialization
1362-
@TruffleBoundary
13631361
String strip(String self, String chars) {
1364-
return self.replaceAll("^[" + Pattern.quote(chars) + "]+", "").replaceAll("[" + Pattern.quote(chars) + "]+$", "");
1362+
return StringUtils.strip(self, chars, StripKind.BOTH);
13651363
}
13661364

13671365
@SuppressWarnings("unused")
@@ -1376,9 +1374,8 @@ String strip(String self, PNone chars) {
13761374
@TypeSystemReference(PythonArithmeticTypes.class)
13771375
public abstract static class RStripNode extends PythonBuiltinNode {
13781376
@Specialization
1379-
@TruffleBoundary
13801377
String rstrip(String self, String chars) {
1381-
return self.replaceAll("[" + Pattern.quote(chars) + "]+$", "");
1378+
return StringUtils.strip(self, chars, StripKind.RIGHT);
13821379
}
13831380

13841381
@SuppressWarnings("unused")
@@ -1393,9 +1390,8 @@ String rstrip(String self, PNone chars) {
13931390
@TypeSystemReference(PythonArithmeticTypes.class)
13941391
public abstract static class LStripNode extends PythonBuiltinNode {
13951392
@Specialization
1396-
@TruffleBoundary
13971393
String rstrip(String self, String chars) {
1398-
return self.replaceAll("^[" + Pattern.quote(chars) + "]+", "");
1394+
return StringUtils.strip(self, chars, StripKind.LEFT);
13991395
}
14001396

14011397
@SuppressWarnings("unused")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringUtils.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,35 @@ public static String strip(String str, StripKind stripKind) {
168168

169169
return str.substring(i, j);
170170
}
171+
172+
public static String strip(String str, String chars, StripKind stripKind) {
173+
int i = 0;
174+
int len = str.length();
175+
// TODO: cpython uses a bloom filter for to skip chars that are not in the sep list:
176+
// to avoid the linear search in chars
177+
if (stripKind != StripKind.RIGHT) {
178+
while (i < len) {
179+
char ch = str.charAt(i);
180+
if (chars.indexOf(ch) < 0) {
181+
break;
182+
}
183+
i++;
184+
}
185+
}
186+
187+
int j = len;
188+
if (stripKind != StripKind.LEFT) {
189+
j--;
190+
while (j >= i) {
191+
char ch = str.charAt(j);
192+
if (chars.indexOf(ch) < 0) {
193+
break;
194+
}
195+
j--;
196+
}
197+
j++;
198+
}
199+
200+
return str.substring(i, j);
201+
}
171202
}

0 commit comments

Comments
 (0)