-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstrings.ijs
More file actions
362 lines (325 loc) · 10.2 KB
/
strings.ijs
File metadata and controls
362 lines (325 loc) · 10.2 KB
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
NB. string manipulation
NB.%strings.ijs - string utilities
NB.-This script defines string utilities and is included in the J standard library.
NB.-Definitions are loaded into the z locale.
NB. charsub character substitution
NB. chopstring chop delimited string to list of boxed strings
NB. cut cut text, by default on blanks
NB. cuts cut y at x (conjunction)
NB. deb delete extra blanks
NB. debc delete extra blank columns in matrix
NB. dlb delete leading blanks
NB. dltb delete leading and trailing blanks
NB. dltbs delete multiple leading and trailing blanks
NB. dtb delete trailing blanks
NB. dtbs delete multiple trailing blanks
NB. delstring delete occurrences of x from y
NB. joinstring join boxed list y with x; see splitstring
NB. ljust left justify
NB. rjust right justify
NB. rplc replace in string
NB. splitnostring split y by non-overlapping substrings x
NB. splitstring split y by substring x
NB. ss string search for x in y
NB.
NB. dropafter drop after x in y
NB. dropto drop to x in y
NB. takeafter take after x in y
NB. taketo take to x in y
NB.
NB. quote quote text
NB. dquote double quote text
NB.
NB. stringreplace replace in string
NB. fstringreplace replace in file
NB. For example:
NB.
NB. 3 = 'de' # cuts _1 'abcdefg'
NB. 'abcfg' = 'de' delstring 'abcdefg'
NB. 'abcde' = 'de' dropafter 'abcdefg'
NB. 'defg' = 'de' dropto 'abcdefg'
NB. 'fg' = 'de' takeafter 'abcdefg'
NB. 'abc' = 'de' taketo 'abcdefg'
cocurrent 'z'
NB. =========================================================
NB.*cuts c cut strings at given text
NB.-This builds verbs to cut strings at given text and
NB.-apply verbs to the pieces.
NB.-syntax:
NB.+string (verb cuts n) text
NB.- n=_1 up to but not including string
NB.- n= 1 up to and including string
NB.- n=_2 after but not including string
NB.- n= 2 after and including string
cuts=: 2 : 0
if. n=1 do. [: u (#@[ + E. i. 1:) {. ]
elseif. n=_1 do. [: u (E. i. 1:) {. ]
elseif. n= 2 do. [: u (E. i. 1:) }. ]
elseif. 1 do. [: u (#@[ + E. i. 1:) }. ]
end.
)
NB. =========================================================
NB.*cut v cut text, by default on blanks
NB.*deb v delete extra blanks
NB.*dlb v delete leading blanks
NB.*dltb v delete leading and trailing blanks
NB.*dtb v delete trailing blanks
NB.*delstring v delete occurrences of x from y
NB.*joinstring v join boxed list y with x; see splitstring
NB.*ljust v left justify
NB.*rjust v right justify
NB.*ss v string search
cut=: ' '&$: :([:-.&a:<;._2@,~) :.(;:inv@] : [:)
deb=: #~ (+. 1: |. (> </\))@(' '&~:)
debc=: #~"1 [: (+. (1: |. (> </\))) ' '&(+./ .~:)
delstring=: 4 : ';(x E.r) <@((#x)&}.) ;.1 r=. x,y'
detab=: ' ' I.@(=&TAB@])} ]
3 : 0''
try.
dlb=: 1&(128!:11)
dltb=: 2&(128!:11)
dtb=: 0&(128!:11)
catch.
dlb=: }.~ =&' ' i. 0:
dltb=: #~ [: (+./\ *. +./\.) ' '&~:
dtb=: #~ [: +./\. ' '&~:
end.
''
)
joinstring=: ''&$: : (#@[ }. <@[ ;@,. ])
ljust=: (|.~ +/@(*./\)@(' '&=))"1
rjust=: (|.~ -@(+/)@(*./\.)@(' '&=))"1
ss=: I. @ E.
NB. =========================================================
NB.*dropafter v drop after x in y
NB.*dropto v drop to x in y
NB.*takeafter v take after x in y
NB.*taketo v take to x in y
dropto=: ] cuts 2
dropafter=: ] cuts 1
taketo=: ] cuts _1
takeafter=: ] cuts _2
NB. =========================================================
NB.*charsub v character substitution
NB.-syntax:
NB.+characterpairs charsub string
NB.-example:
NB.+ '-_$ ' charsub '$123 -456 -789'
NB.+123 _456 _789
NB.-note:
NB.-Use [rplc](#rplc) for arbitrary string replacement.
NB.
NB. thanks to Dan Bron/Jforum 25 April 2006
charsub=: 4 : 0
'f t'=. |: _2 ]\ x
l=. f i."1 0 y
x=. l { t,'?'
c=. l = #f
c } x ,: y
)
NB. =========================================================
NB.*chopstring v chop delimited string to list of boxed strings
NB.-syntax:
NB.+[fd[;sd0[,sd1]]] chopstring string
NB.- returns: list of boxed literals
NB.- y is: delimited string
NB.- x is: a literal or 1 or 2-item boxed list of optional delimiters.
NB.- 0{:: single literal field delimiter (fd). Defaults to ' '
NB.- (1;0){:: (start) string delimiter (sd0). Defaults to "
NB.- (1;1){:: end string delimiter (sd1). Defaults to "
NB.-
NB.-Consecutive field delimiters indicate empty field.
NB.-Field delimiters may occur within a field if
NB.-the field is enclosed by string delimiters.
NB.-example:
NB.+ ('|';'<>') chopstring '<hello|world>|4|84.3'
NB.+┌───────────┬─┬────┐
NB.+│hello|world│4│84.3│
NB.+└───────────┴─┴────┘
chopstring=: 3 : 0
(' ';'"') chopstring y
:
dat=. y
'fd sd'=. 2{. boxopen x
assert. 1 = #fd
if. #sd do.
sd=. ~.sd
if. 1 < #sd do. NB. replace diff start and end delims with single
s=. {. '|`' -. fd NB. choose single sd
dat=. dat charsub~ ,sd,.s
sd=. s
end.
dat=. dat,fd
b=. dat = fd
c=. dat = {.sd
d=. ~:/\ c NB. mask inside sds
fmsk=. b > d NB. end of fields
smsk=. (> (0 , }:)) c NB. first in group of sds
smsk=. -. smsk +. c *. 1|.fmsk NB. or previous to fd
y=. smsk#y,fd NB. compress out string delims
fmsk=. 0:^:(,@1: -: ]) smsk#fmsk
fmsk <;._2 y NB. box
else. NB. no string delimters so can simplify processing
<;._2 dat,fd
end.
)
NB. =========================================================
NB.*dltbs v delete multiple leading and trailing blanks
NB.-Delete multiple leading and trailing blanks.
NB.-Text is delimited by characters in x with default LF
NB.-example:
NB.+ < 'A' dltbs ' A abc def Ars A x y z '
NB.+┌───────────────────┐
NB.+│Aabc defArsAx y z│
NB.+└───────────────────┘
dltbs=: LF&$: : (4 : 0)
txt=. ({.x), y
a=. txt ~: ' '
b=. (a # txt) e. x
c=. b +. }. b, 1
d=. ~: /\ a #^:_1 c ~: }: 0, c
}. (a >: d) # txt
)
NB. =========================================================
NB.*dquote v double quote text
NB.-example:
NB.+ dquote 'Pete"s Place'
NB.+"Pete""s Place"
dquote=: ('"'&,@(,&'"'))@ (#~ >:@(=&'"'))
NB. =========================================================
NB.*dtbs v delete multiple trailing blanks in text
NB.-Delete multiple trailing blanks in text.
NB.-Text is delimited by characters in x with default CRLF
NB.-example:
NB.+ < 'A' dtbs ' A abc def Ars A x y z '
NB.+┌──────────────────────┐
NB.+│A abc defArsA x y z│
NB.+└──────────────────────┘
NB.
NB. Algorithm thanks to Brian Bambrough (JForum Nov 2000)
dtbs=: 3 : 0
CRLF dtbs y
:
txt=. y , {.x
blk=. txt ~: ' '
ndx=. +/\ blk
b=. blk < }. (txt e. x), 0
msk=. blk >: ndx e. b # ndx
}: msk # txt
)
NB. =========================================================
NB.*rplc v replace characters in text string
NB.-This is [stringreplace](#stringreplace) but
NB.-with arguments reversed.
NB.-example:
NB.+ 'hello' rplc 'e';'a';'o';'owed'
NB.+hallowed
rplc=: stringreplace~
NB. =========================================================
NB.*fstringreplace v file string replace
NB.-Replace strings in file
NB.-syntax:
NB.+(old;new) fstringreplace file
fstringreplace=: 4 : 0
nf=. 'no match found'
y=. fboxname y
try. size=. 1!:4 y catch. nf return. end.
if. size=0 do. nf return. end.
old=. freads y
new=. x stringreplace old
if. old -: new do. nf return. end.
new fwrites y
cnt=. +/ (0 pick x) E. old
(":cnt),' replacement',((1~:cnt)#'s'),' made'
)
NB. =========================================================
NB.*quote v quote text
NB. quote 'Pete''s Place'
NB.-example:
NB.+ quote 'Pete"s Place'
NB.+'Pete''s Place'
quote=: (''''&,@(,&''''))@ (#~ >:@(=&''''))
NB. =========================================================
NB.*splitnostring v split y by non-overlapping substrings x
NB.-Split y by non-overlapping substrings x
NB.-This is a non-overlapping variant of E.
nos=. i.@#@] e. #@[ ({~^:a:&0@(,&_1)@(]I.+) { _1,~]) I.@E.
splitnostring=: #@[ }.each [ (nos f. <;.1 ]) ,
NB. =========================================================
NB.*splitstring v split y by substring x
NB.-Split y by substring x.
NB.-see [joinstring](#joinstring).
splitstring=: #@[ }.each [ (E. <;.1 ]) ,
NB. =========================================================
NB.*stringreplace v replace characters in text string
NB.-
NB.-syntax:
NB.+oldnew stringreplace text
NB.-oldnew is a 2-column boxed matrix of `old ,. new`
NB.-or a vector of same
NB.-
NB.-stringreplace priority is the same order as oldnew
NB.-
NB.-example:
NB.-
NB.+ ('aba';'XYZT';'ba';'+') stringreplace 'ababa'
NB.+ XYZT+
NB.+
NB.+ ('ba';'+';'aba';'XYZT') stringreplace 'ababa'
NB.+ a++
stringreplace=: 4 : 0
txt=. ,y
t=. _2 [\ ,x
old=. {."1 t
new=. {:"1 t
oldlen=. # &> old
newlen=. # &> new
if. *./ 1 = oldlen do.
hit=. (;old) i. txt
ndx=. I. hit < #old
if. 0 e. $ndx do. txt return. end.
cnt=. 1
exp=. hit { newlen,1
hnx=. ndx { hit
bgn=. ndx + +/\ 0, (}: hnx) { newlen - 1
else.
hit=. old I. @ E. each <txt
cnt=. # &> hit
if. 0 = +/ cnt do. txt return. end.
bgn=. set=. ''
pick=. > @ {
diff=. }. - }:
for_i. I. 0 < cnt do.
ln=. i pick oldlen
cx=. (i pick hit) -. set, ,bgn -/ i.ln
while. 0 e. b=. 1, <:/\ ln <: diff cx do. cx=. b#cx end.
hit=. (<cx) i} hit
bgn=. bgn, cx
set=. set, ,cx +/ i.ln
end.
cnt=. # &> hit
msk=. 0 < cnt
exp=. (#txt) $ 1
del=. newlen - oldlen
if. #add=. I. msk *. del > 0 do.
exp=. (>: (add{cnt) # add{del) (;add{hit) } exp
end.
if. #sub=. I. msk *. del < 0 do.
sbx=. ; (;sub{hit) + each (sub{cnt) # i. each sub{del
exp=. 0 sbx } exp
end.
hit=. ; hit
ind=. /: (#hit) $ 1 2 3
hnx=. (/: ind { hit) { ind
bgn=. (hnx { hit) + +/\ 0, }: hnx { cnt # del
end.
ind=. ; bgn + each hnx { cnt # i.each newlen
rep=. ; hnx { cnt # new
rep ind} exp # txt
)
NB. =========================================================
NB.*undquote v undo double quote text
NB.-example:
NB.+ undquote '"Pete""s Place"'
NB.+Pete"s Place
undquote=: (#~ -.@('""'&E.))@}:@}.^:(('"' = {.) *. '"' = {:)