Skip to content

Commit 50bb742

Browse files
authored
Add removeprefix/removesuffix to Starlark strings (#14899)
Implements bazelbuild/starlark#185 in Java Starlark. Closes #14824. PiperOrigin-RevId: 430556229
1 parent 8ebd70b commit 50bb742

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/main/java/net/starlark/java/eval/StringModule.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,36 @@ public boolean startsWith(String self, Object sub, Object start, Object end)
10221022
private static boolean substringStartsWith(String str, int start, int end, String prefix) {
10231023
return start + prefix.length() <= end && str.startsWith(prefix, start);
10241024
}
1025+
1026+
@StarlarkMethod(
1027+
name = "removeprefix",
1028+
doc =
1029+
"If the string starts with <code>prefix</code>, returns a new string with the prefix "
1030+
+ "removed. Otherwise, returns the string.",
1031+
parameters = {
1032+
@Param(name = "self", doc = "This string."),
1033+
@Param(name = "prefix", doc = "The prefix to remove if present."),
1034+
})
1035+
public String removePrefix(String self, String prefix) {
1036+
if (self.startsWith(prefix)) {
1037+
return self.substring(prefix.length());
1038+
}
1039+
return self;
1040+
}
1041+
1042+
@StarlarkMethod(
1043+
name = "removesuffix",
1044+
doc =
1045+
"If the string ends with <code>suffix</code>, returns a new string with the suffix "
1046+
+ "removed. Otherwise, returns the string.",
1047+
parameters = {
1048+
@Param(name = "self", doc = "This string."),
1049+
@Param(name = "suffix", doc = "The suffix to remove if present."),
1050+
})
1051+
public String removeSuffix(String self, String suffix) {
1052+
if (self.endsWith(suffix)) {
1053+
return self.substring(0, self.length() - suffix.length());
1054+
}
1055+
return self;
1056+
}
10251057
}

src/test/java/net/starlark/java/eval/testdata/string_misc.star

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,35 @@ assert_eq("abc" * 0, "")
177177
assert_eq("abc" * -1, "")
178178
assert_fails(lambda: "abc" * (1 << 35), "got 34359738368 for repeat, want value in signed 32-bit range")
179179
assert_fails(lambda: "abc" * (1 << 30), "excessive repeat \\(3 \\* 1073741824 characters\\)")
180+
181+
# removeprefix
182+
assert_eq("Apricot".removeprefix("Apr"), "icot")
183+
assert_eq("Apricot".removeprefix("apr"), "Apricot")
184+
assert_eq("Apricot".removeprefix("A"), "pricot")
185+
assert_eq("a".removeprefix(""), "a")
186+
assert_eq("".removeprefix(""), "")
187+
assert_eq("".removeprefix("a"), "")
188+
assert_eq("Apricot".removeprefix("pr"), "Apricot")
189+
assert_eq("AprApricot".removeprefix("Apr"), "Apricot")
190+
def removeprefix_self_unmodified():
191+
original_string = "Apricot"
192+
assert_eq(original_string.removeprefix("Apr"), "icot")
193+
assert_eq(original_string, "Apricot")
194+
removeprefix_self_unmodified()
195+
assert_fails(lambda: "1234".removeprefix(1), "got value of type 'int', want 'string")
196+
197+
# removesuffix
198+
assert_eq("Apricot".removesuffix("cot"), "Apri")
199+
assert_eq("Apricot".removesuffix("Cot"), "Apricot")
200+
assert_eq("Apricot".removesuffix("t"), "Aprico")
201+
assert_eq("a".removesuffix(""), "a")
202+
assert_eq("".removesuffix(""), "")
203+
assert_eq("".removesuffix("a"), "")
204+
assert_eq("Apricot".removesuffix("co"), "Apricot")
205+
assert_eq("Apricotcot".removesuffix("cot"), "Apricot")
206+
def removesuffix_self_unmodified():
207+
original_string = "Apricot"
208+
assert_eq(original_string.removesuffix("cot"), "Apri")
209+
assert_eq(original_string, "Apricot")
210+
removesuffix_self_unmodified()
211+
assert_fails(lambda: "1234".removesuffix(4), "got value of type 'int', want 'string")

0 commit comments

Comments
 (0)