forked from moliad/utils-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-strings.r
237 lines (192 loc) · 5.02 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
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 [
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- 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
]
;--------------------------
;- 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
]
]
;------------------------------------
; We are done testing this library.
;------------------------------------
;
; test-exit-slim
;
;------------------------------------