-
Notifications
You must be signed in to change notification settings - Fork 13
/
parse.cljc
213 lines (174 loc) · 8.44 KB
/
parse.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public
; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the
; file epl-v10.html at the root of this distribution. By using this software in any
; fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns tupelo.parse
"Utils for parsing string values. Provides a thin Clojure wrapper around java native
parsing functions such as java.lang.Float/parseFloat. Unlike the original java
functions, these native-Clojure functions can be used as higher-order functions in maps,
function arguments, etc. Each function also provides an optional default-value which
will be returned if there is an exception during parsing."
(:require
#?@(:clj [[clojure.tools.reader.edn :as edn]
[clojure.walk :as walk]])
[clojure.string :as str]
[schema.core :as s]
[tupelo.core :as t]
))
; #todo: write doc page
; #todo: convert args from [str-val & opts] -> [str-val & {:as opts} ]
#?(:clj
(do
(defn edn-parsible
"Traverses a data structure to ensure it can be serialized with `pr-str` and read via
clojure.tools.reader.edn/read-string. All non-parsible content is replaced
with `::non-parsible-object`. "
[data]
(let [edn-parse-roundtrip (fn [item]
(t/with-exception-default ::edn-non-parsible
(let [item-str (pr-str item)
item-edn (edn/read-string item-str)]
(when-not (= item item-edn)
(throw (IllegalArgumentException. (str ::edn-non-parsible))))
item)))
data-unlazy (t/unlazy data) ; coerce to map/vector/string wherever possible
data-parsible (walk/postwalk edn-parse-roundtrip data-unlazy)]
data-parsible))
;---------------------------------------------------------------------------
(defn parse-byte
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Byte/parseByte. Parses the string str-val into a byte.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Byte/parseByte str-val)
(t/with-exception-default default-val (Byte/parseByte str-val)))))
(defn parse-short
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Short/parseShort. Parses the string str-val into a short.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Short/parseShort str-val)
(t/with-exception-default default-val (Short/parseShort str-val)))))
(defn parse-int
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Integer/parseInt Parses the string str-val into a integer.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Integer/parseInt str-val)
(t/with-exception-default default-val (Integer/parseInt str-val)))))
(defn parse-long
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Long/parseLong. Parses the string str-val into a long.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Long/parseLong str-val)
(t/with-exception-default default-val (Long/parseLong str-val)))))
(defn parse-float
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Float/parseFloat. Parses the string str-val into a float.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Float/parseFloat str-val)
(t/with-exception-default default-val (Float/parseFloat str-val)))))
(defn parse-double
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.Double/parseDouble. Parses the string str-val into a double.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Double/parseDouble str-val)
(t/with-exception-default default-val (Double/parseDouble str-val)))))
#_(defn parse-xxxx
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.XXXX/parseXXXX. Parses the string str-val into a xxxx.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(XXXX/parseXXXX str-val)
(t/with-exception-default default-val (XXXX/parseXXXX str-val)))))
; #awt TODO: finish other parse* functions
))
#?(:cljs
(do
; These regexes are from the internet and aren't guarenteed to be perfect
(def regex-int
"Nominal regex for signed/unsigned integers"
#"^([+-]?[1-9]\d*|0)$")
(def regex-float
"Nominal regex for signed/unsigned floating-point numbers (possibly in scientific notation)"
#"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
(s/defn parse-int :- s/Int ; #todo => tupelo.cljs.parse
"( [str-val]
[str-val :default default-val] )
A thin wrapper around js/parseInt Parses the string str-val into a integer.
If the optional default-val is specified, it will be returned in the event of an
Nan."
([str-val :- s/Str]
(t/cond-it-> (str/trim str-val)
(not (re-matches regex-int it)) (throw (ex-info "parse-int: could not parse input value #1"
(t/vals->map str-val)))
true (js/parseInt it)
(js/isNaN it) (throw (ex-info "parse-int: could not parse input value #2"
(t/vals->map str-val))) ))
([str-val default-val]
(t/with-exception-default default-val
(parse-int str-val))))
(s/defn parse-float :- s/Num
"( [str-val]
[str-val :default default-val] )
A thin wrapper around js/parseFloat. Parses the string str-val into a float.
If the optional default-val is specified, it will be returned in the event of an
NaN."
([str-val :- s/Str]
(t/cond-it-> (str/trim str-val)
(not (re-matches regex-float it)) (throw (ex-info "parse-float: could not parse input value #1"
(t/vals->map str-val)))
true (js/parseFloat it)
(js/isNaN it) (throw (ex-info "parse-float: could not parse input value #2"
(t/vals->map str-val)))))
([str-val default-val]
(t/with-exception-default default-val
(parse-float str-val))))
))