Skip to content

Commit 0b09e9e

Browse files
fmeumcopybara-github
authored andcommitted
Add removeprefix/removesuffix to Starlark strings
Implements bazelbuild/starlark#185 in Java Starlark. Closes bazelbuild#14824. PiperOrigin-RevId: 430556229
1 parent a20797e commit 0b09e9e

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

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)