Skip to content

Commit

Permalink
Add REGEX function to compile a string to pattern (#111)
Browse files Browse the repository at this point in the history
Accept the second argument as a string of supported flags:

  * "i" Makes the expression search case-insensitively.
  * "s" dot all. It makes a wild character `.` match newlines as well.
  * "m" Makes the boundary characters `^` and `$` match the beginning
        and ending of every single line instead of the beginning and
        ending of the whole string.

Combination of flags supported via the concatenation of characters: "si"
  • Loading branch information
DeLaGuardo committed Mar 2, 2021
1 parent 1ff3df1 commit ec8fca8
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
runs-on: ubuntu-latest
container: borkdude/clj-kondo:latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2.3.4
- name: Run clj-kondo
run: clj-kondo --lint src test --config .clj-kondo/config.edn
20 changes: 11 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@ jobs:
run: exit 1

- name: checkout
uses: actions/checkout@v1
uses: actions/checkout@v2.3.4
with:
fetch-depth: 1

- name: Cache maven dependencies
uses: actions/cache@v1
uses: actions/cache@v2.1.4
with:
path: ~/.m2/repository
key: maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
maven-
- name: Set TAG value from refs/tags/TAG
run: echo ::set-env name=GIT_TAG::${GITHUB_REF#refs/tags/}
run: |
echo "GIT_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Install java
uses: actions/setup-java@v1
with:
java-version: '1.8'

- name: Install clojure
uses: DeLaGuardo/setup-clojure@2.0
uses: DeLaGuardo/setup-clojure@3.1
with:
tools-deps: 'latest'

Expand Down Expand Up @@ -67,33 +68,34 @@ jobs:
run: exit 1

- name: checkout
uses: actions/checkout@v1
uses: actions/checkout@v2.3.4
with:
fetch-depth: 1

- name: Cache maven dependencies
uses: actions/cache@v1
uses: actions/cache@v2.1.4
with:
path: ~/.m2/repository
key: npm-${{ hashFiles('**/pom.xml') }}
restore-keys: |
npm-
- name: Set TAG value from refs/tags/TAG
run: echo ::set-env name=GIT_TAG::${GITHUB_REF#refs/tags/}
run: |
echo "GIT_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Install java
uses: actions/setup-java@v1
with:
java-version: '1.8'

- name: Install clojure
uses: DeLaGuardo/setup-clojure@2.0
uses: DeLaGuardo/setup-clojure@3.1
with:
tools-deps: 'latest'

- name: Install NodeJS
uses: actions/setup-node@v1
uses: actions/setup-node@v2.1.5
with:
node-version: 13.9.x

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ jobs:
with:
java-version: '1.8'

- uses: actions/setup-node@v1
- uses: actions/setup-node@v2.1.5
with:
node-version: '13.9.x'

- uses: DeLaGuardo/setup-clojure@2.0
- uses: DeLaGuardo/setup-clojure@3.1
with:
tools-deps: latest

- uses: actions/checkout@v1
- uses: actions/checkout@v2.3.4

- name: Prepare npm modules for tests
run: |
npm install ws isomorphic-ws
- name: Cache clojure dependencies
uses: actions/cache@v1
uses: actions/cache@v2.1.4
with:
path: ~/.m2/repository
key: ${{ matrix.clojure }}-clojure-${{ hashFiles('**/deps.edn') }}
Expand Down
2 changes: 1 addition & 1 deletion bin/kaocha
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

clojure -A:cljs:test -m kaocha.runner "$@"
clojure -M:cljs:test -m kaocha.runner "$@"
20 changes: 10 additions & 10 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{:deps {clj-fuzzy {:mvn/version "0.4.1"}
cheshire {:mvn/version "5.10.0"}
clojure.java-time {:mvn/version "0.3.2"}
org.bouncycastle/bcprov-jdk15on {:mvn/version "1.65.01"}
org.bouncycastle/bcpkix-jdk15on {:mvn/version "1.65"}
commons-codec/commons-codec {:mvn/version "1.14"}
{:deps {clj-fuzzy/clj-fuzzy {:mvn/version "0.4.1"}
cheshire/cheshire {:mvn/version "5.10.0"}
clojure.java-time/clojure.java-time {:mvn/version "0.3.2"}
org.bouncycastle/bcprov-jdk15on {:mvn/version "1.68"}
org.bouncycastle/bcpkix-jdk15on {:mvn/version "1.68"}
commons-codec/commons-codec {:mvn/version "1.15"}
io.xapix/matchete {:mvn/version "2.0.0"}
org.clojure/tools.reader {:mvn/version "1.3.2"}}
org.clojure/tools.reader {:mvn/version "1.3.5"}}

:paths ["src"]

:aliases
{:cljs {:extra-deps {org.clojure/clojurescript {:mvn/version "1.10.764"}}}
{:cljs {:extra-deps {org.clojure/clojurescript {:mvn/version "1.10.773"}}}

:cljs-repl {:main-opts ["-m" "cljs.main" "-co" "axel_f.dev.js.edn" "--repl"]}

:cljs-build {:main-opts ["-m" "cljs.main" "-co" "axel_f.min.js.edn" "--compile"]}

:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.632"}
lambdaisland/kaocha-cloverage {:mvn/version "1.0-45"}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.732"}
lambdaisland/kaocha-cloverage {:mvn/version "1.0.75"}
lambdaisland/kaocha-cljs {:mvn/version "0.0-71"}}}}}
6 changes: 3 additions & 3 deletions src/axel_f/buddy/dsa.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#?(:cljs
(deftype StubEngine [alg]
IEngine
(-init [it {:keys [key verify]}])
(-update [it input _offset _length])
(-end [it signature?]
(-init [_it {:keys [_key _verify]}])
(-update [_it _input _offset _length])
(-end [_it signature?]
(if signature?
(do
(js/console.warn (str "Signing for algorithm " (name alg) " is not yet implemented and will return fixed string"))
Expand Down
23 changes: 23 additions & 0 deletions src/axel_f/excel/text.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,28 @@

(def PROPER #'PROPER*)

(defn REGEX*
"Compile string to regular expression."
([st] (REGEX* st nil))
([st flags]
#?(:clj
(if (seq flags)
(let [flags (sequence
(comp
(map {\i java.util.regex.Pattern/CASE_INSENSITIVE
\s java.util.regex.Pattern/DOTALL
\m java.util.regex.Pattern/MULTILINE})
(filter identity))
flags)
flags (if (> (count flags) 1)
(apply bit-or flags)
(first flags))]
(. java.util.regex.Pattern (compile st flags)))
(. java.util.regex.Pattern (compile st)))
:cljs (js/RegExp. st flags))))

(def REGEX #'REGEX*)

(defn REGEXEXTRACT*
"Extracts matching substrings according to a regular expression."
[text regular-expression]
Expand Down Expand Up @@ -380,6 +402,7 @@
"LOWER" LOWER
"MID" MID
"PROPER" PROPER
"REGEX" REGEX
"REGEXEXTRACT" REGEXEXTRACT
"REGEXMATCH" REGEXMATCH
"REGEXREPLACE" REGEXREPLACE
Expand Down
14 changes: 14 additions & 0 deletions test/axel_f/functions_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@
(t/is (= nil
((af/compile "REGEXEXTRACT('FOO', '[a-z]+')"))))))

(t/deftest regex-function-test
(t/testing "REGEX and REGEXEXTRACT functions"
(t/is (= "Test"
((af/compile "REGEXEXTRACT('foo Test bar', REGEX('test', 'i'))"))))

(t/is (= "Foo"
((af/compile "REGEXEXTRACT('X
Foo
bar', REGEX('^foo$', 'im'))"))))

(t/is (= "Foo\n"
((af/compile "REGEXEXTRACT('Foo
', REGEX('.*', 's'))"))))))

(t/deftest regexmatch-function-test
(t/testing "REGEXMATCH function"
(t/is (= true
Expand Down

0 comments on commit ec8fca8

Please sign in to comment.