-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils-strings.r
492 lines (400 loc) · 10.7 KB
/
utils-strings.r
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
REBOL [
title: "String handling tools."
file: %utils-strings.r
version: 1.0.1
date: 2013-01-09
author: "Maxim Oliver-Adlhoch"
purpose: "Collection of generic, re-useable string! handling functions"
web: http://www.revault.org/modules/utils-strings.rmrk
source-encoding: "Windows-1252"
note: "slim Library Manager is Required to use this module."
; SLiM - Steel Library Manager, minimal requirements
slim-name: 'utils-strings
slim-version: 1.0.5
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/utils-strings.r
;-- Licensing details --
copyright: "Copyright © 2013 Maxim Oliver-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2013 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;- / history
history: {
2012-02-21 - v1.0.0
-creation
-compiling previous code from other libraries
2013-01-09 - v1.0.1
-Adding slut.r tests.
}
;- \ history
]
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'utils-strings
;
;--------------------------------------
slim/register [
parse-utils: slim/open 'utils-parse none
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- STRING
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------
;- zfill()
;
; test-group [ zfill string utils-strings.r ] [ ]
; [ "0000003" = zfill 3 7 ]
; [ "0000666" = zfill 666 7 ]
; [ "0000055" = probe zfill "55" 7 ]
; end-group
;
;--------------------
zfill: func [
"left fills the supplied string with zeros to amount size."
data [string! integer! decimal!]
length [integer!]
/local string
][
string: either string? data [
data
][
to-string data
]
if (length? string) < length [
head insert/dup string "0" (length - length? string)
]
head string
]
;--------------------
;- fill()
;--------------------
fill: func [
"Fills a series to a fixed length"
data "series to fill, any non series is converted to string!"
len [integer!] "length of resulting string"
/with val "replace default space char"
/right "right justify fill"
/truncate "will truncate input data if its larger than len"
/local buffer
][
unless series? data [
data: to-string data
]
val: any [
val " " ; default value
]
buffer: head insert/dup make type? data none val len
either right [
reverse data
change buffer data
reverse buffer
][
change buffer data
]
if truncate [
clear next at buffer len
]
buffer
]
;--------------------------
;- coerce-string()
;--------------------------
; purpose: a merge of to-string and as-string, the most memory efficient.
;
; inputs:
;
; returns:
;
; notes:
;
; tests:
;--------------------------
coerce-string: funcl [
data
][
vin "coerce-string()"
data: either any-string? data [
as-string data
][
to-string data
]
vout
data
]
;--------------------------
;- mold-decimal()
;--------------------------
; purpose: always returns a normal decimal string version of a decimal! value, not scientific notation.
;
; inputs:
;
; returns:
;
; notes: uses time! to do work.
;
; to do:
;
; tests:
;--------------------------
mold-decimal: funcl [
value [decimal!]
][
vin "mold-decimal()"
value: to-time value
val: rejoin [to-integer value/second skip mold value 7]
vout
val
]
;--------------------------
;- make-mem-buffer()
;--------------------------
; purpose: Create a mem cleared memory buffer for use with system functions
;
; inputs: when a string is given on input, its length is used.
;
; returns: a 0 filled sting of length bytes.
;--------------------------
make-mem-buffer: funcl [
length [integer! string!]
][
;vin "make-mem-buffer()"
if string? length [
length: length? string
]
;vout/return
str: head insert/dup make string! length to-char 0 length
]
;-----------------
;- text-to-lines()
;-----------------
text-to-lines: func [
str [string!]
][
either empty? str [
copy []
][
parse/all str "^/"
]
]
;-----------------
;- count-lines()
;
; given any string of text, returns at what line that text character
; is at within the text. (minimum is 1)
;
; used to report errors in general.
;
; note that this function is meant to be fast, not memory efficient.
;-----------------
count-lines: func [
string [string!]
][
vin [{count-lines()}]
parse-utils/.line-count: 1
parse/all copy/part head string string parse-utils/=line-counter=
vout
parse-utils/.line-count
]
;--------------------------
;- integer-label()
;--------------------------
; purpose: takes an integer and returns a human readable version of it.
;
; inputs: integer!
;--------------------------
integer-label: funcl [
value [integer!]
/of type "a string to append to the label"
/precision magnitute [decimal!]
/default deftype "string to use when no multiplier is given."
][
;vin "integer-label()"
type: any [type ""]
deftype: any [ deftype type ]
case [
value > 1'000'000'000 [
magnitute: any [magnitute 0.01]
value: value / 1'000'000'000
value: round/to value magnitute
rejoin ["" value "G" type]
]
value > 1'000'000 [
magnitute: any [magnitute 0.1]
value: value / 1'000'000
value: round/to value magnitute
rejoin ["" value "M" type]
]
value > 1000 [
magnitute: any [magnitute 0.1]
value: value / 1000
value: round/to value magnitute
rejoin ["" value "K" type]
]
'default [
rejoin ["" value deftype]
]
]
;vout
]
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- DATES
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- international-datestring()
;--------------------------
; purpose: just a quick setup to return the prefered international date format in string! format
;--------------------------
international-datestring: funcl [
date [date!]
][
rejoin [date/year "-" zfill date/month 2 "-" zfill date/day 2]
]
;--------------------
;- date-time()
;--------------------
; use this to prevent having to supply a spec all the time.
; the /default option of date-time sets this.
default-date-time-spec: "YYYY-MM-DDThh:mm:ssITZ"
;---
date-time: func [
""
/with spec ; specify
/using thedate [string! date! time!] ; specify an explicit date instead of now()
/UTC "removes the timezone from the date being used (given or now)"
/default ; set the default to /with spec
/local str date-rules thetime tz-string itz-string
][
;vin ["date-time()"]
str: copy ""
either spec [
if default [
default-date-time-spec: spec
]
][
spec: default-date-time-spec
]
unless thedate [
thedate: now/precise
]
unless thedate/time [
thedate/time: 0:00 ; specify midnight if time isn't given (TZ unspecified).
]
;------------------
; UTC time required, bake any tz into time directly
;------------------
if UTC [
if thedate/zone [
thedate: thedate - thedate/zone
thedate/zone: none
]
]
;------------------
; set timezone strings
;------------------
case [
(any [
none? thedate/zone
thedate/zone = 0:00
]) [
tz-string: "+0:00"
itz-string: "Z"
]
(thedate/zone < 0:00) [
tz-string: itz-string: mold thedate/zone
]
'default [
tz-string: itz-string: rejoin ["+" thedate/zone]
]
]
;------------------
; extract time from date information.
;------------------
either time? thedate [
thetime: thedate
thedate: none
][
if thedate/time [
thetime: thedate/time
thedate/time: none
]
]
;------------------
;process spec
;------------------
filler: complement charset "YMDHhmspPS"
;error: spec
itime: true
unless parse/case spec [
some [
here:
(error: here)
; padded dates
["YYYY" (append str zfill thedate/year 4)] |
["YY" (append str copy/part at (zfill (to-string thedate/year) 4) 3 2)] |
["MM" (append str zfill thedate/month 2)] |
["DD" (append str zfill thedate/day 2)] |
["M" (append str thedate/month)] |
["D" (append str thedate/day)] |
; padded time
["hh" (append str zfill thetime/hour 2)] |
["mm" (append str zfill thetime/minute 2)] |
["ss" (append str zfill to-integer thetime/second 2)] |
["SS" (append str zfill (round/to thetime/second 0.001) 6)] | ;precise
; am/pm indicator
["P" (append str "#@#@#@#")] |
["p" (append str "-@-@-@-")] |
; time zone
["TZ" (append str tz-string)] | ; LOCAL TZ
["ITZ" (append str itz-string)] | ; internet TZ - like TZ but prints "Z" when the tz is 0:00 (UTC time)
; american/english style 12hour format
["H" (
itime: remainder thetime/hour 12
if 0 = itime [ itime: 12]
append str itime
itime: either thetime/hour >= 12 ["PM"]["AM"]
)
] |
; non padded time
["h" (append str thetime/hour)] |
["m" (append str thetime/minute)] |
["s" (append str to-integer thetime/second)] |
; escape a character (skip next)
["^^" copy val skip (append str val)] |
; copy input
[copy val some filler (append str val)]
]
(replace str "#@#@#@#" any [to-string itime ""])
(replace str "-@-@-@-" lowercase any [to-string itime ""])
][
to-error rejoin [
"date-time() DATE FORMAT ERROR: " spec newline
" starting at: " error newline
" valid so far: " str newline
]
]
;vout
str
]
]
;------------------------------------
; We are done testing this library.
;------------------------------------
;
; test-exit-slim
;
;------------------------------------