/
chars.cljc
171 lines (146 loc) · 5.3 KB
/
chars.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
; 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.chars
"Tupelo - Making Clojure even sweeter"
(:require
[clojure.set :as set]
[clojure.string :as str]
[schema.core :as s]
[tupelo.core :as t]
[tupelo.schema :as tsk]
#?(:cljs [goog.string :as gstring] )
))
;-----------------------------------------------------------------------------
(def code-point-backspace
"The unicode code-point for the backspace char."
8)
(def code-point-tab
"The unicode code-point for the horizontal tab char."
9)
(def code-point-newline
"The unicode code-point for the newline char."
10)
(def code-point-tab-vertical
"The unicode code-point for the vertical tab char."
11)
(def code-point-formfeed
"The unicode code-point for the form-feed char."
12)
(def code-point-return
"The unicode code-point for the return char."
13)
(def code-point-escape
"The unicode code-point for the escape char."
27)
(def code-point-del
"The unicode code-point for the delete char."
127)
;-----------------------------------------------------------------------------
; #todo: docstrings
(s/def whitespace-horiz :- tsk/Set
"Horizontal whitespace (space & tab)"
(set [\space \tab]))
(s/def whitespace-eol :- tsk/Set
"Vertical whitespace (return & newline)"
(set [\return \newline]))
(s/def whitespace :- tsk/Set
"All whitespace (vertical & horizontal)"
(t/glue whitespace-horiz whitespace-eol))
(s/def lowercase :- tsk/Set
"Lowercase chars a-z"
(into (sorted-set) (t/chars-thru \a \z)))
(s/def uppercase :- tsk/Set
"Uppercase chars A-Z"
(into (sorted-set) (t/chars-thru \A \Z)))
(s/def digit :- tsk/Set
"Digit chars 0-9"
(into (sorted-set) (t/chars-thru \0 \9)))
(s/def hex :- tsk/Set
"Hexadecimal chars 0-9, a-f, A-F"
(into (sorted-set) (flatten [(t/chars-thru \a \f) (t/chars-thru \A \F) (t/chars-thru \0 \9)])))
(s/def alpha :- tsk/Set
"All alphabetic chars (either case)"
(t/glue lowercase uppercase))
(s/def alphanumeric :- tsk/Set
"All alphabetic & digit chars"
(t/glue alpha digit))
(s/def visible :- tsk/Set
"Set of all visible (printing) ASCII chars from exclamation point (33) to tilde (126).
Excludes all whitespace & control chars."
(into (sorted-set) (mapv t/codepoint->char (t/thru 33 126))))
(s/def visible-no-dquote :- tsk/Set
"All visible (printing) ASCII chars except double-quote."
(set/difference visible #{\"}))
(s/def visible-no-squote :- tsk/Set
"All visible (printing) ASCII chars except single-quote."
(set/difference visible #{\'}))
(s/def text :- tsk/Set
"Set of chars used in 'normal' text. Includes all visible chars plus whitespace & EOL chars."
(t/glue visible whitespace))
;---------------------------------------------------------------------------------------------------
(defn alphanumeric?
"Returns true iff char is in tupelo.chars/alphanumeric"
[ch] (contains? alphanumeric ch))
(defn whitespace-horiz?
"Returns true iff char is in tupelo.chars/whitespace-horiz"
[ch] (contains? whitespace-horiz ch))
(defn whitespace-eol?
"Returns true iff char is in tupelo.chars/whitespace-eol"
[ch] (contains? whitespace-eol ch))
(defn whitespace?
"Returns true iff char is in tupelo.chars/whitespace"
[ch] (contains? whitespace ch))
(defn lowercase?
"Returns true iff char is in tupelo.chars/lowercase"
[ch] (contains? lowercase ch))
(defn uppercase?
"Returns true iff char is in tupelo.chars/uppercase"
[ch] (contains? uppercase ch))
(defn digit?
"Returns true iff char is in tupelo.chars/digit"
[ch] (contains? digit ch))
(defn hex?
"Returns true iff char is in tupelo.chars/hex"
[ch] (contains? hex ch))
(defn alpha?
"Returns true iff char is in tupelo.chars/alpha"
[ch] (contains? alpha ch))
(defn visible?
"Returns true iff char is in tupelo.chars/visible"
[ch] (contains? visible ch))
(defn text?
"Returns true iff char is in tupelo.chars/text"
[ch] (contains? text ch))
;---------------------------------------------------------------------------------------------------
(def char-lower->upper (zipmap lowercase uppercase))
(def char-upper->lower (zipmap uppercase lowercase))
(defn ->uppercase
"Converts any lowercase chars to uppercase"
[ch]
(if-not (lowercase? ch)
ch
(t/fetch char-lower->upper ch)))
(defn ->lowercase
"Converts any uppercase chars to lowercase"
[ch]
(if-not (uppercase? ch)
ch
(t/fetch char-upper->lower ch)))
#?(:cljs
(do
;-----------------------------------------------------------------------------
(def char-nbsp (gstring/unescapeEntities " ")) ; get a char we can use in hiccup
;-----------------------------------------------------------------------------
(defn nbsp
"Return a string of N non-breaking-space (NBSP) chars (default=1)."
([] (nbsp 1))
([N] (str/join (repeat N char-nbsp))))
; #todo => tupelo.cljs.key-value-string ???
(def kvs-enter "Enter")
(def kvs-tab "Tab")
(def kvs-escape "Escape")
))