-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a utility function which transform a string into another which …
…can be used in a regex without any special matching behavior other than matching the original string.
- Loading branch information
1 parent
b11dc55
commit 325b5dd
Showing
3 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns hopen.syntax.util | ||
(:require [clojure.string :as str])) | ||
|
||
(defn re-quote | ||
"Escapes characters in the string that are not safe to use in a regex. | ||
Function ported to Clojure from https://github.com/google/closure-library/blob/0257667129eded0cb9b86b4701e50c986b5db648/closure/goog/string/string.js#L1016" | ||
[s] | ||
(-> s | ||
(str/replace #"[-()\[\]{}+?*.$\^|,:#<!\\]" #(str "\\" %)) | ||
(str/replace #"\x08", "\\x08"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns hopen.syntax.util-test | ||
(:require #?(:clj [clojure.test :refer [deftest testing is are]] | ||
:cljs [cljs.test :refer [deftest testing is are] | ||
:include-macros true]) | ||
[hopen.syntax.util :refer [re-quote]])) | ||
|
||
(deftest re-quote-test | ||
(are [input output] | ||
(= (re-quote input) output) | ||
|
||
"bonjour" "bonjour" | ||
"{{}}" "\\{\\{\\}\\}" | ||
"<%%>" "\\<%%>")) |