Skip to content

Commit 7afbe7f

Browse files
committed
Add strip_prefix and strip_suffix in string.gleam
1 parent 22f5b85 commit 7afbe7f

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

src/gleam/string.gleam

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,3 +888,45 @@ pub fn trim_prefix(string: String, prefix prefix: String) -> String
888888
@external(erlang, "gleam_stdlib", "string_trim_suffix")
889889
@external(javascript, "../gleam_stdlib.mjs", "string_trim_suffix")
890890
pub fn trim_suffix(string: String, suffix suffix: String) -> String
891+
892+
/// Removes the given prefix from the start of a `String` if present, and returns an `Ok(String)`.
893+
///
894+
/// If the `String` does not start with the given prefix, it is returned as an `Error(Nil)`.
895+
///
896+
/// ## Examples
897+
///
898+
/// ```gleam
899+
/// assert strip_prefix("gleamlucy", "gleam") == Ok("lucy")
900+
/// ```
901+
///
902+
/// ```gleam
903+
/// assert strip_prefix("lucygleam", "gleam") == Error(Nil)
904+
/// ```
905+
///
906+
@external(erlang, "gleam_stdlib", "string_strip_prefix")
907+
@external(javascript, "../gleam_stdlib.mjs", "string_strip_prefix")
908+
pub fn strip_prefix(
909+
string: String,
910+
prefix prefix: String,
911+
) -> Result(String, Nil)
912+
913+
/// Removes the given suffix from the end of a `String` if present, and returns an `Ok(String)`.
914+
///
915+
/// If the `String` does not end with the given suffix, it is returned as an `Error(Nil)`.
916+
///
917+
/// ## Examples
918+
///
919+
/// ```gleam
920+
/// assert strip_suffix("lucygleam", "gleam") == Ok("lucy")
921+
/// ```
922+
///
923+
/// ```gleam
924+
/// assert strip_suffix("gleamlucy", "gleam") == Error(Nil)
925+
/// ```
926+
///
927+
@external(erlang, "gleam_stdlib", "string_strip_suffix")
928+
@external(javascript, "../gleam_stdlib.mjs", "string_strip_suffix")
929+
pub fn strip_suffix(
930+
string: String,
931+
suffix suffix: String,
932+
) -> Result(String, Nil)

src/gleam_stdlib.erl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
crop_string/2, base16_encode/1, base16_decode/1, string_replace/3, slice/3,
1313
bit_array_to_int_and_size/1, bit_array_pad_to_bytes/1, index/2, list/5,
1414
dict/1, int/1, float/1, bit_array/1, is_null/1, string_trim_prefix/2,
15-
string_trim_suffix/2
15+
string_trim_suffix/2, string_strip_prefix/2, string_strip_suffix/2
1616
]).
1717

1818
%% Taken from OTP's uri_string module
@@ -551,3 +551,29 @@ string_trim_suffix(String, Suffix) ->
551551
true -> binary_part(String, 0, Size - SuffixSize);
552552
false -> String
553553
end.
554+
555+
string_strip_prefix(String, <<>>) ->
556+
{ok, String};
557+
string_strip_prefix(<<>>, _) ->
558+
{error, nil};
559+
string_strip_prefix(String, Prefix) when byte_size(Prefix) > byte_size(String) ->
560+
{error, nil};
561+
string_strip_prefix(String, Prefix) ->
562+
PrefixSize = byte_size(Prefix),
563+
case Prefix == binary_part(String, 0, PrefixSize) of
564+
true -> {ok, binary_part(String, PrefixSize, byte_size(String) - PrefixSize)};
565+
false -> {error, nil}
566+
end.
567+
568+
string_strip_suffix(String, <<>>) ->
569+
{ok, String};
570+
string_strip_suffix(<<>>, _) ->
571+
{error, nil};
572+
string_strip_suffix(String, Suffix) when byte_size(Suffix) > byte_size(String) ->
573+
{error, nil};
574+
string_strip_suffix(String, Suffix) ->
575+
SuffixSize = byte_size(Suffix),
576+
case Suffix == binary_part(String, byte_size(String) - SuffixSize, SuffixSize) of
577+
true -> {ok, binary_part(String, 0, byte_size(String) - SuffixSize)};
578+
false -> {error, nil}
579+
end.

src/gleam_stdlib.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,3 +1080,35 @@ export function string_trim_suffix(str, suffix) {
10801080

10811081
return str;
10821082
}
1083+
1084+
export function string_strip_prefix(str, prefix) {
1085+
if (prefix == "") {
1086+
return Result$Ok(str);
1087+
}
1088+
1089+
if (str == "" && prefix.length != 0) {
1090+
return Result$Error(undefined);
1091+
}
1092+
1093+
if (str.startsWith(prefix)) {
1094+
return Result$Ok(str.substring(prefix.length));
1095+
}
1096+
1097+
return Result$Error(undefined);
1098+
}
1099+
1100+
export function string_strip_suffix(str, suffix) {
1101+
if (suffix == "") {
1102+
return Result$Ok(str);
1103+
}
1104+
1105+
if (str == "" && suffix.length != 0) {
1106+
return Result$Error(undefined);
1107+
}
1108+
1109+
if (str.endsWith(suffix)) {
1110+
return Result$Ok(str.substring(0, str.length - suffix.length));
1111+
}
1112+
1113+
return Result$Error(undefined);
1114+
}

test/gleam/string_test.gleam

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,35 @@ pub fn trim_suffix_test() {
13291329

13301330
assert string.trim_suffix("🦑", "🦑") == ""
13311331
}
1332+
1333+
pub fn strip_prefix_test() {
1334+
assert string.strip_prefix("gleamlucy", "gleam") == Ok("lucy")
1335+
1336+
assert string.strip_prefix("lucygleam", "gleam") == Error(Nil)
1337+
1338+
assert string.strip_prefix("lucygleam", "") == Ok("lucygleam")
1339+
1340+
assert string.strip_prefix("", "gleam") == Error(Nil)
1341+
1342+
assert string.strip_prefix("gleam", "gleam") == Ok("")
1343+
1344+
assert string.strip_prefix("🦑lucy", "🦑") == Ok("lucy")
1345+
1346+
assert string.strip_prefix("🦑", "🦑") == Ok("")
1347+
}
1348+
1349+
pub fn strip_suffix_test() {
1350+
assert string.strip_suffix("lucygleam", "gleam") == Ok("lucy")
1351+
1352+
assert string.strip_suffix("gleamlucy", "gleam") == Error(Nil)
1353+
1354+
assert string.strip_suffix("lucygleam", "") == Ok("lucygleam")
1355+
1356+
assert string.strip_suffix("", "gleam") == Error(Nil)
1357+
1358+
assert string.strip_suffix("gleam", "gleam") == Ok("")
1359+
1360+
assert string.strip_suffix("lucy🦑", "🦑") == Ok("lucy")
1361+
1362+
assert string.strip_suffix("🦑", "🦑") == Ok("")
1363+
}

0 commit comments

Comments
 (0)