From 74cee40df199937e2f40957e4b56a2596b2c8434 Mon Sep 17 00:00:00 2001 From: LagunaElectric <6085143+LagunaElectric@users.noreply.github.com> Date: Sun, 15 Sep 2024 14:06:34 -0400 Subject: [PATCH 1/4] add regex escape --- src/gleam/regex.gleam | 9 +++++++++ test/gleam/regex_test.gleam | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam index d1f8ef0a4..b567f88da 100644 --- a/src/gleam/regex.gleam +++ b/src/gleam/regex.gleam @@ -3,7 +3,9 @@ //// all of the PCRE library is interfaced and some parts of the library go beyond //// what PCRE offers. Currently PCRE version 8.40 (release date 2017-01-11) is used. +import gleam/list import gleam/option.{type Option} +import gleam/string pub type Regex @@ -214,3 +216,10 @@ pub fn replace( in string: String, with substitute: String, ) -> String + +pub fn escape(input: String) -> String { + ["\\", "$", "^", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|"] + |> list.fold(input, fn(acc, char) { + string.replace(in: acc, each: char, with: "\\" <> char) + }) +} diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam index e51757798..9e08e24bb 100644 --- a/test/gleam/regex_test.gleam +++ b/test/gleam/regex_test.gleam @@ -185,3 +185,31 @@ pub fn replace_3_test() { regex.replace(re, "🐈🐈 are great!", "🐕") |> should.equal("🐕🐕 are great!") } + +// Test for escaping regex special characters +pub fn escape_test() { + // Test escaping common regex symbols + let input = "$^.*+?()[]{}|\\" + let expected = "\\$\\^\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\" + input + |> regex.escape + |> should.equal(expected) +} + +// Test with a string that contains no special characters +pub fn escape_no_special_chars_test() { + let input = "hello" + let expected = "hello" + input + |> regex.escape + |> should.equal(expected) +} + +// Test with a mix of normal characters and regex symbols +pub fn escape_mixed_string_test() { + let input = "hello$world^test" + let expected = "hello\\$world\\^test" + input + |> regex.escape + |> should.equal(expected) +} From c8f9571918c308b261a5dea2893d70293307feb4 Mon Sep 17 00:00:00 2001 From: LagunaElectric <6085143+LagunaElectric@users.noreply.github.com> Date: Sun, 15 Sep 2024 14:08:52 -0400 Subject: [PATCH 2/4] remove comments --- test/gleam/regex_test.gleam | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam index 9e08e24bb..793d86e69 100644 --- a/test/gleam/regex_test.gleam +++ b/test/gleam/regex_test.gleam @@ -186,7 +186,6 @@ pub fn replace_3_test() { |> should.equal("🐕🐕 are great!") } -// Test for escaping regex special characters pub fn escape_test() { // Test escaping common regex symbols let input = "$^.*+?()[]{}|\\" @@ -196,7 +195,6 @@ pub fn escape_test() { |> should.equal(expected) } -// Test with a string that contains no special characters pub fn escape_no_special_chars_test() { let input = "hello" let expected = "hello" @@ -205,7 +203,6 @@ pub fn escape_no_special_chars_test() { |> should.equal(expected) } -// Test with a mix of normal characters and regex symbols pub fn escape_mixed_string_test() { let input = "hello$world^test" let expected = "hello\\$world\\^test" From 5f5fd063c015c76c0e6f898b8109d82e345bbf2d Mon Sep 17 00:00:00 2001 From: LagunaElectric <6085143+LagunaElectric@users.noreply.github.com> Date: Sun, 15 Sep 2024 14:26:24 -0400 Subject: [PATCH 3/4] add docstring --- src/gleam/regex.gleam | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam index b567f88da..9cba7944d 100644 --- a/src/gleam/regex.gleam +++ b/src/gleam/regex.gleam @@ -217,6 +217,19 @@ pub fn replace( with substitute: String, ) -> String +/// Exapes all Regex characters in a given `String`. +/// +/// ## Examples +/// +/// ```gleam +/// regex.escape("hello$world^test") +/// // -> "hello\\$world\\^test" +/// ``` +/// +/// ```gleam +/// regex.escape("$^.*+?()[]{}|\\") +/// // -> "\\$\\^\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\\\" +/// ``` pub fn escape(input: String) -> String { ["\\", "$", "^", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|"] |> list.fold(input, fn(acc, char) { From f4f44fccf725238e50931232af1ecb360c783e4b Mon Sep 17 00:00:00 2001 From: Laguna <6085143+LagunaElectric@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:21:35 -0400 Subject: [PATCH 4/4] fix typo in docstring Co-authored-by: Pranav RK <39577726+radar07@users.noreply.github.com> --- src/gleam/regex.gleam | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gleam/regex.gleam b/src/gleam/regex.gleam index 9cba7944d..fc61506d6 100644 --- a/src/gleam/regex.gleam +++ b/src/gleam/regex.gleam @@ -217,7 +217,7 @@ pub fn replace( with substitute: String, ) -> String -/// Exapes all Regex characters in a given `String`. +/// Escapes all `Regex` characters in a given `String`. /// /// ## Examples ///