Skip to content

Commit eaedfb3

Browse files
committed
swappable engine and finalizable regex object
- some renames - defprotocol IRegex - deftype CRE2Regex implementing IFinalize and IRegex
1 parent 5b0e880 commit eaedfb3

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

pixie/re.pxi

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
(i/defcfn cre2_strings_to_ranges)
4747
)
4848

49-
(def optmap
49+
(def cre2_optmap
5050
{ :ascii #(cre2_set_encoding % 2)
5151
:posix #(cre2_opt_set_posix_syntax % 1)
5252
:longest_match #(cre2_opt_set_longest_match % 1)
@@ -57,43 +57,32 @@
5757
:never_capture #(do %) ;; ??
5858
:ignore_case #(cre2_opt_set_case_sensitive % 0) })
5959

60-
(defn- cre2-opts [opts]
60+
(defn cre2_make_opts [opts]
6161
(let [opt (cre2_opt_new)]
62-
(doseq [key opts] ((key optmap) opt))
62+
(doseq [key opts] ((key cre2_optmap) opt))
6363
opt))
6464

65-
66-
(defn match
65+
(defn cre2_run_match
6766
[pattern text]
68-
(cre2_match
69-
pattern
70-
text
71-
(count text)
72-
0
73-
(count text)
74-
1 ;; anchor 1 - no, 2 - start, 3 - both
75-
(cre2_string_t)
76-
(+ 1 (cre2_num_capturing_groups pattern))))
77-
78-
(defn regex
79-
{:doc "Returns internal representation for regular
80-
expression, used in matching functions."
81-
:signatures [[rexeg-str opts]]}
82-
[regex-str opts]
83-
(re-pattern regex-str opts))
67+
(= 1
68+
(cre2_match pattern
69+
text (count text)
70+
0 (count text)
71+
1 ;; anchor 1 - no, 2 - start, 3 - both
72+
(cre2_string_t)
73+
(+ 1 (cre2_num_capturing_groups pattern)))))
8474

8575
(defprotocol IRegex
86-
(re-matches [r s])
87-
(re-find [r s]))
76+
(re-matches [r t]))
8877

89-
(defrecord CRE2Regex [pattern opts]
78+
(deftype CRE2Regex [pattern opts]
9079
IFinalize
9180
(-finalize! [this]
92-
(println "dropping cre2 obj " this)
9381
(cre2_opt_delete opts)
9482
(cre2_delete pattern))
9583

96-
IRegex)
84+
IRegex
85+
(re-matches [_ text] (cre2_run_match pattern text)))
9786

9887
(def ^:dynamic *default-re-engine* :cre2)
9988

@@ -102,11 +91,12 @@
10291

10392
;; add cre2 to registry
10493
(defmethod re-engine :cre2 [_ regex-str opts]
105-
(let [copts (cre2-opts opts)]
94+
(let [copts (cre2_make_opts opts)]
10695
(->CRE2Regex (cre2_new regex-str (count regex-str) copts) copts)))
10796

108-
;; dispatch on the right engine constructor via registry
109-
(defn re-pattern
110-
([s o] (re-pattern s o *default-re-engine*))
111-
([s o kw] (re-engine kw s o)))
112-
97+
(defn regex
98+
{:doc "Returns internal representation for regular
99+
expression, used in matching functions."
100+
:signatures [[rexeg-str opts]]}
101+
([pattern opts] (regex pattern opts *default-re-engine*))
102+
([pattern opts engine] (re-engine engine pattern opts)))

0 commit comments

Comments
 (0)