aboutsummaryrefslogtreecommitdiff
path: root/site/udo/string_tools.udo
blob: c2c14c0e5248c1bdaeb97fc03daab4c1577a7382 (plain)
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
#ifndef UDO_STRINGTOOLS
#define UDO_STRINGTOOLS ##
/*
	String processing tools

	This file is part of the SONICS UDO collection by Richard Knight 2021, 2022, 2023
		License: GPL-2.0-or-later
		http://1bpm.net
	
*/

#include "/host_platform.udo"

/*
	Replace character with another character or sequence of characters
	Soutput str_replacechar Sinput, Sfrom, Sto

	Soutput		string with replacements
	Sinput		input string
	Sfrom		character to replace
	Sto			string or character to substitute
*/
opcode str_replacechar, S, SSS
	Sinput, Sfrom, Sto xin
	Soutput = ""
	index = 0
	while (index < strlen(Sinput)) do
		Schar = strsub(Sinput, index, index + 1)
		if (strcmp(Sfrom, Schar) == 0) then
			Soutput = strcat(Soutput, Sto)
		else
			Soutput = strcat(Soutput, Schar)
		endif
		index += 1
	od
	xout Soutput
endop


/*
	String alphabetical bubble sort
	Sout[] srt_bubblestr Sin[]

	Sout[]		sorted array
	Sin[]		array to sort
*/
opcode srt_bubblestr, S[], S[]
	Sin[] xin
	Stemp = ""
	ilen = lenarray(Sin)
	index1 = 0
	while (index1 < ilen-1) do
		index2 = 0
		while (index2 < ilen-1-index1) do
			if (strcmp(Sin[index2], Sin[index2+1]) > 0) then
				Stemp = Sin[index2]
				Sin[index2] = Sin[index2+1]
				Sin[index2+1] = Stemp
			endif

			index2 += 1
		od
		index1 += 1
	od
	xout Sin
endop


/*
	Polynomial rolling hash
	ihash str_hash Sstring

	ihash		the resulting hash value
	Sstring		string to be hashed
*/
opcode str_hash, i, S
	Sin xin
	ip = 31
	im = 1e9+7 ; was 1e9+9, changed for 32bit
	ipp = 1
	ihash = 0
	index = 0
	while (index < strlen(Sin)) do
		ihash = (ihash + ((strchar(strsub(Sin, index)) - 97) + 1) * ipp)  ; changed from *ip to *ipp ??? due to collisions. not fully checked
		ipp = (ipp * ip) % im
		index += 1
	od
	xout ihash
endop


/*
	Split separated string to array
	Sitems[] str_split Sinput, Separator

	Sitems[]	the processed items
	Sinput		string to split
	Separator	separator to split on
*/
opcode str_split, S[], SS
	Sin, Sep xin
	Stemp = Sin
	itemnum = 1
	index = 1
	while (index > 0) do
		index strindex Stemp, Sep
		if (index > 0) then
			Stemp strsub Stemp, index+1
			itemnum += 1
		endif
	od
	Sout[] init itemnum

	iwindex = 0
	Stemp = Sin
	index = 1
	while (index > 0) do
		index strindex Stemp, Sep
		if (index > 0) then
			Sout[iwindex] strsub Stemp, 0, index
			Stemp strsub Stemp, index+1
		else
			Sout[iwindex] = Stemp
		endif
		iwindex += 1
	od

	xout Sout
endop


/*
	Print ftable contents
	printtable ifn

	ifn		ftable number to print
*/
opcode printtable, 0, i
	ifn xin
	index = 0
	while (index < ftlen(ifn)) do
		print table:i(index, ifn)
		index += 1
	od
endop



/*
	Store string to an exising table as ascii characters
	str2tab String, ifn

	String		string to store
	ifn			table to store to
*/
opcode str2tab, 0, Si
	String, ifn xin
	index = 0
	while (index < strlen(String)) do
		ival strchar String, index
		tabw_i ival, index, ifn
		index += 1
	od
	tabw_i -99, index, ifn
endop


/*
	Create a table and store a string in it as ascii characters
	ifn str2newtab String

	ifn			new table number
	String		string to store
*/
opcode str2newtab, i, S
	String xin
	ifn ftgen 0, 0, strlen(String)+1, 7, 0
	str2tab String, ifn
	xout ifn
endop


/*
	Obtain a string from a table containing ascii characters
	String tab2str ifn

	String		retrieved string
	ifn		table number
*/
opcode tab2str, S, i
	ifn xin
	index = 0
	Sval = ""
	ival = 0
	while (ival != -99) do
		ival tab_i index, ifn
		if (ival == -99) igoto done
		Sval strcat Sval, sprintf("%c", ival)
		index += 1
	od
done:
	xout Sval
endop


/*
	Obtain file extension converted to lowercase (anything past the last dot)
	Sextension fileextension Sfile

	Sfile		path or filename
	Sextension	the extension in lower case
*/
opcode fileextension, S, S
	Sfile xin
	ilastdot strrindex Sfile, "."
	if (ilastdot == -1) then
		Sextension = "none"
		goto return
	endif
	ilen strlen Sfile
	Sextension strsub Sfile, ilastdot + 1, ilen
	Sextension strlower Sextension
	goto return
return:
	xout Sextension
endop


/*
	Print a string array with each value separated by a newline

	str_printarray Sarray[]

	Sarray[]	the array to print
*/
opcode str_printarray, 0, S[]
	Sarray[] xin
	ilen = lenarray(Sarray)
	index = 0
	while (index < ilen) do
		prints sprintf("%s\n", Sarray[index])
		index += 1
	od
endop


/*
	Test if a string is numeric

	isnumeric str_isnumeric Svalue

	isnumeric	0 if not numeric, 1 if numeric
	Svalue		string to test
*/
opcode str_isnumeric, i, S
	Svalue xin
	isnumeric = 1
	ihaspoint = 0
	index = 0
	while (index < strlen(Svalue)) do
		ichar = strchar(Svalue, index)

		; allow only one decimal point
		if (ichar == 46) then
			if (ihaspoint == 1) then
				isnumeric = 0
				goto complete
			else
				ihaspoint = 1
			endif

		; not a number or decimal place
		elseif (!(ichar >= 48 && ichar <= 57)) then
			isnumeric = 0
			goto complete
		endif
		index += 1
	od
complete:
	xout isnumeric
endop



/*
	Convert a string to float if numeric

	ivalid, ivalue try_strtod Svalue

	ivalid		0 if the input is not numeric, 1 if conversion is successful
	ivalue		converted value, or -1 if the input is not numeric
*/
opcode try_strtod, ii, S
	Svalue xin
	if (str_isnumeric(Svalue) == 0) then
		ivalid = 0
		ivalue = -1
	else
		ivalid = 1
		ivalue = strtod(Svalue)
	endif
	xout ivalid, ivalue
endop


/*
	Get the basename from a file path separated by slashes

	Sbasename str_basename Spath

	Sbasename	the filename
	Spath		the path
*/
opcode str_basename, S, S
	Spath xin
	Sbasename = strsub(Spath, strrindex(Spath, "/") + 1)
	xout Sbasename
endop


/* 
	Get a random alphanumeric string

	Sout str_random [ilen=10]

	Sout		the random string
	ilen		length of string
*/
opcode str_random, S, j
	ilen xin
	Sout = ""
	ilen = (ilen == -1) ? 10 : ilen
	index = 0
	while (index < ilen) do
		irange = round(random(0, 2))
		if (irange == 0) then
			ichar random 48, 57
		elseif (irange == 1) then
			ichar random 65, 90
		elseif (irange == 2) then
			ichar random 97, 122
		endif
		Sout = strcat(Sout, sprintf("%c", ichar))
		index += 1
	od
	xout Sout
endop

/*
	Get a random filename

	Sout str_randomfilename Sext [,ilen=10]

	Sout		the random filename
	Sext		file extension
	ilen		length of string
*/
opcode str_randomfilename, S, Sj
	Sext, ilen xin
	Sfile str_random ilen
	xout strcat(Sfile, sprintf(".%s", Sext))
endop

/*
	Get a random filename in the host temporary directory

	Sout str_randomtempfilename Sext [,ilen=10]
*/
opcode str_randomtempfilename, S, Sj
	Sext, ilen xin
	Sfile str_randomfilename Sext, ilen
	xout sprintf("%s/%s", host_tempdir(), Sfile)
endop

#end