aboutsummaryrefslogtreecommitdiff
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
parentcb80cf508ac0990038ec6ee6cd90cb61eaeabe5c (diff)
downloadgleam_stdlib-70f065415469c65dd7f66dd4ef3f8e651f16b66a.tar.gz
gleam_stdlib-70f065415469c65dd7f66dd4ef3f8e651f16b66a.zip
Regex split
-rw-r--r--src/gleam/regex.gleam34
-rw-r--r--test/gleam/regex_test.gleam14
2 files changed, 32 insertions, 16 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
diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam
index 367b810..87535a0 100644
--- a/test/gleam/regex_test.gleam
+++ b/test/gleam/regex_test.gleam
@@ -1,4 +1,4 @@
-import gleam/regex.{CompileError, Match, Options}
+import gleam/regex.{Match, Options}
import gleam/should
import gleam/io
import gleam/option.{None, Some}
@@ -39,14 +39,14 @@ pub fn check_test() {
|> should.equal(False)
}
-if erlang {
- pub fn split_test() {
- assert Ok(re) = regex.from_string(" *, *")
+pub fn split_test() {
+ assert Ok(re) = regex.from_string(" *, *")
- regex.split(re, "foo,32, 4, 9 ,0")
- |> should.equal(["foo", "32", "4", "9", "0"])
- }
+ regex.split(re, "foo,32, 4, 9 ,0")
+ |> should.equal(["foo", "32", "4", "9", "0"])
+}
+if erlang {
pub fn scan_test() {
assert Ok(re) = regex.from_string("Gl\\w+")