aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-08-26 22:38:32 +0100
committerLouis Pilfold <louis@lpil.uk>2021-08-27 00:37:15 +0100
commit70f065415469c65dd7f66dd4ef3f8e651f16b66a (patch)
tree9c016d6874f17c309723a7d89b7f445940d12c06 /src
parentcb80cf508ac0990038ec6ee6cd90cb61eaeabe5c (diff)
downloadgleam_stdlib-70f065415469c65dd7f66dd4ef3f8e651f16b66a.tar.gz
gleam_stdlib-70f065415469c65dd7f66dd4ef3f8e651f16b66a.zip
Regex split
Diffstat (limited to 'src')
-rw-r--r--src/gleam/regex.gleam34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam
index e59d049..8eddabe 100644
--- a/src/gleam/regex.gleam
+++ b/src/gleam/regex.gleam
@@ -27,6 +27,7 @@ pub type CompileError {
/// The problem encountered that caused the compilation to fail
error: String,
/// The byte index into the string to where the problem was found
+ /// This value may not be correct in JavaScript environments.
byte_index: Int,
)
}
@@ -114,18 +115,33 @@ if javascript {
"../gleam_stdlib.js" "regex_check"
}
+/// Splits a string
+///
+/// ## Examples
+///
+/// > assert Ok(re) = from_string(" *, *")
+/// > split(with: re, content: "foo,32, 4, 9 ,0")
+/// ["foo", "32", "4", "9", "0"]
+///
+pub fn split(with regex: Regex, content string: String) -> List(String) {
+ do_split(regex, string)
+}
+
if erlang {
- /// Splits a string
- ///
- /// ## Examples
- ///
- /// > assert Ok(re) = from_string(" *, *")
- /// > split(with: re, content: "foo,32, 4, 9 ,0")
- /// ["foo", "32", "4", "9", "0"]
- ///
- pub external fn split(with: Regex, content: String) -> List(String) =
+ external fn do_split(Regex, String) -> List(String) =
"gleam_stdlib" "regex_split"
+}
+
+if javascript {
+ fn do_split(regex, string) -> List(String) {
+ js_split(string, regex)
+ }
+
+ external fn js_split(String, Regex) -> List(String) =
+ "../gleam_stdlib.js" "split"
+}
+if erlang {
/// Collects all matches of the regular expression.
///
/// ## Examples