-
Notifications
You must be signed in to change notification settings - Fork 2
/
extrastr.lua
590 lines (476 loc) · 23.1 KB
/
extrastr.lua
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
-- ----------------------------------------------------------------------------
--
-- ExtraStr - helpers for the string library
--
-- ----------------------------------------------------------------------------
local _floor = math.floor
local _concat = table.concat
local _format = string.format
-- ----------------------------------------------------------------------------
-- count the number of occurrences of the specified text
--
local function str_gcount(inBytes, inValue)
local iCount = 0
if inBytes and inValue then
local iEnd = inBytes:find(inValue, 1, true)
while iEnd do
iCount = iCount + 1
iEnd = inBytes:find(inValue, iEnd + 1, true)
end
end
return iCount
end
-- ----------------------------------------------------------------------------
-- remove leading whitespace from string.
-- http://en.wikipedia.org/wiki/Trim_(programming)
--
local function str_ltrim(inString)
if not inString then return "" end
return inString:gsub("^%s*", "")
end
-- ----------------------------------------------------------------------------
-- remove trailing whitespace from string.
-- http://en.wikipedia.org/wiki/Trim_(programming)
--
local function str_rtrim(inString)
if not inString then return "" end
local n = #inString
while n > 0 and inString:find("^%s", n) do n = n - 1 end
return inString:sub(1, n)
end
-- ----------------------------------------------------------------------------
--
local function str_trim(inString)
if not inString then return "" end
return str_ltrim(str_rtrim(inString))
end
-- ----------------------------------------------------------------------------
--
local function str_cap1st(inString)
if not inString then return "" end
return (inString:sub(1):upper() .. inString:lower():sub(1))
end
-- ----------------------------------------------------------------------------
-- put a % before each char that must be escaped for the string.find function
--
local function str_fmtfind(inString)
if not inString then return "" end
local tOutString = { }
local chCurrent
for i=1, #inString do
chCurrent = inString:sub(i, i)
if '.' == chCurrent then chCurrent = "%."
elseif '_' == chCurrent then chCurrent = "%_"
elseif '-' == chCurrent then chCurrent = "%-"
elseif '[' == chCurrent then chCurrent = "%["
elseif ']' == chCurrent then chCurrent = "%]"
elseif '(' == chCurrent then chCurrent = "%("
elseif ')' == chCurrent then chCurrent = "%)"
end
tOutString[#tOutString + 1] = chCurrent
end
return _concat(tOutString, nil)
end
-- ----------------------------------------------------------------------------
-- given a natural number returns a string where
-- 1024 -> 1 kb
-- 1024^2 -> 1 Mb
-- 1024^3 -> 1 Gb
--
local function str_fmtkilo(inNum)
if 0 < inNum then
local iKilo = _floor(inNum / 1024)
local iMega = _floor(iKilo / 1024)
local iGiga = _floor(iMega / 1024)
local iTera = _floor(iGiga / 1024)
if 0 < iTera then return _format("%d Tb", iTera) end
if 0 < iGiga then return _format("%d Gb", iGiga) end
if 0 < iMega then return _format("%d Mb", iMega) end
if 0 < iKilo then return _format("%d Kb", iKilo) end
end
return _format("%d b", inNum)
end
-- ----------------------------------------------------------------------------
-- ABNF from RFC 3629
--
-- UTF8-octets = *( UTF8-char )
-- UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
-- UTF8-1 = %x00-7F
-- UTF8-2 = %xC2-DF UTF8-tail
-- UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
-- %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
-- UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
-- %xF4 %x80-8F 2( UTF8-tail )
-- UTF8-tail = %x80-BF
--
local tUTF8Lookup =
{
{0x00, 0x7f}, -- 1 column
{0xc2, 0xdf, 0x80, 0xbf},
{0xe0, 0xe0, 0xa0, 0xbf, 0x80, 0xbf},
{0xe1, 0xec, 0x80, 0xbf, 0x80, 0xbf},
{0xed, 0xed, 0x80, 0x9f, 0x80, 0xbf},
{0xee, 0xef, 0x80, 0xbf, 0x80, 0xbf},
{0xf0, 0xf0, 0x90, 0xbf, 0x80, 0xbf, 0x80, 0xbf},
{0xf1, 0xf3, 0x80, 0xbf, 0x80, 0xbf, 0x80, 0xbf},
{0xf4, 0xf4, 0x80, 0x8f, 0x80, 0xbf, 0x80, 0xbf}, -- 4 columns
}
-- ----------------------------------------------------------------------------
-- return the row that contains the parameter in the very first column
--
local function str_lkp_utf_8(inCode)
if inCode then
for iIndex=1, #tUTF8Lookup do
if tUTF8Lookup[iIndex][1] <= inCode and inCode <= tUTF8Lookup[iIndex][2] then
return tUTF8Lookup[iIndex]
end
end
end
return nil
end
-- ----------------------------------------------------------------------------
-- return length of UTF_8 character (1 to 4)
-- on error return 0
--
local function str_len_utf_8(inBytes, inStart)
if 1 > inStart or #inBytes < inStart then return 0 end
local chByte = inBytes:byte(inStart, inStart)
-- quick shot, we know first 0x7f values are 1 byte long
--
if 0x80 > chByte then return 1 end
-- otherwise we check the lookup table
--
local utf8Codes = str_lkp_utf_8(chByte)
if not utf8Codes then return 0 end
return (#utf8Codes / 2)
end
-- ----------------------------------------------------------------------------
-- extract an UTF_8 bytes sequence out of a bytes array
-- check if each byte is valid
-- returns 2 values:
-- 1. the full utf_8 code or the first byte of the sequence
-- 2. validity check as:
-- -1 error
-- [1..4] length of full utf8 sequence
--
-- Note: Unicode recommends to return always the first byte in case of error.
--
local function str_sub_utf_8(inBytes, inStart)
-- sanity checks
--
if 1 > inStart then return "", -1 end
local iLength = inBytes:len()
if iLength < inStart then return "", -1 end
-- start byte, the mark
--
local chMark = inBytes:sub(inStart, inStart)
local chByte = chMark:byte()
-- quick shot, we know first 0x7f characters are 1 byte long
--
if 0x80 > chByte then return chMark, 1 end
-- otherwise we get the exact row in the lookup table
--
local utf8Codes = str_lkp_utf_8(chByte)
if not utf8Codes then return chMark, -1 end
-- check if enough bytes to complete the request
--
local iChkLen = (#utf8Codes / 2) - 1
if iLength < (inStart + iChkLen) then return chMark, -1 end
-- check each byte if in list of intervals
--
local chCode
local iOffset = 2
for j=1, iChkLen do
chCode = inBytes:byte(inStart + j, inStart + j)
if utf8Codes[iOffset + j] > chCode or
utf8Codes[iOffset + j + 1] < chCode then
-- this is an error, byte's value out of bounds
-- as per the Unicode's recommendation return 1 byte
--
return chMark, -1
end
iOffset = iOffset + 1
end
-- this a complete UTF_8 character of length > 1
--
return inBytes:sub(inStart, inStart + iChkLen), (iChkLen + 1)
end
-- ----------------------------------------------------------------------------
-- iterator for UTF_8 characters in a line of text
--
local function str_iter_utf_8_char(inText)
local iCurr = 1
local bError = false
return function ()
local sUtf8, iULen = inText:utf8sub(iCurr)
-- adjust the current position
--
if - 1 == iULen then
iCurr = iCurr + 1
bError = true
else
iCurr = iCurr + iULen
bError = false
end
if -1 == iULen and 0 == sUtf8:len() then sUtf8 = nil end
return sUtf8, bError
end
end
-- ----------------------------------------------------------------------------
-- Unicode's punctuation characters
-- Note: the fisrt row contains only ASCII codes
--
-- Hint: comment the variable declaration and content to get run-time load of
-- punctuation characters from file.
--
local sUnicodePunct =
"\x09\x0a\x0d\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x40\x5b\x5c\x5d\x5e\x5f\x60\x7b\x7c\x7d\x7e\z
\xc2\xa0\xc2\xa1\xc2\xa2\xc2\xa3\xc2\xa4\xc2\xa5\xc2\xa6\xc2\xa7\xc2\xa8\xc2\xa9\xc2\xaa\xc2\xab\xc2\xac\xc2\xad\xc2\xae\xc2\xaf\z
\xc2\xb0\xc2\xb1\xc2\xb2\xc2\xb3\xc2\xb4\xc2\xb5\xc2\xb6\xc2\xb7\xc2\xb8\xc2\xb9\xc2\xba\xc2\xbb\xc2\xbf\xcd\xbe\xce\x87\xd6\x89\z
\xd6\x8a\xd6\xb0\xd6\xb1\xd6\xb2\xd6\xb3\xd6\xb4\xd6\xb5\xd6\xb6\xd6\xb7\xd6\xb8\xd6\xb9\xd6\xba\xd6\xbb\xd6\xbc\xd6\xbd\xd6\xbe\z
\xd6\xbf\xd7\x80\xd7\x81\xd7\x82\xd7\x83\xd7\x86\xd7\x87\xd7\xb3\xd7\xb4\xd8\x89\xd8\x8a\xd8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e1\xad\x9c\xe1\xad\x9d\xe1\xad\x9e\xe1\xad\x9f\z
\xe1\xad\xa0\xe1\xaf\xbc\xe1\xaf\xbd\xe1\xaf\xbe\xe1\xaf\xbf\xe1\xb0\xbb\xe1\xb0\xbc\xe1\xb0\xbd\xe1\xb0\xbe\xe1\xb0\xbf\xe1\xb1\xbe\z
\xe1\xb1\xbf\xe1\xb3\x80\xe1\xb3\x81\xe1\xb3\x82\xe1\xb3\x83\xe1\xb3\x84\xe1\xb3\x85\xe1\xb3\x86\xe1\xb3\x87\xe2\x80\x80\xe2\x80\x81\z
\xe2\x80\x82\xe2\x80\x83\xe2\x80\x84\xe2\x80\x85\xe2\x80\x86\xe2\x80\x87\xe2\x80\x88\xe2\x80\x89\xe2\x80\x8a\xe2\x80\x8b\xe2\x80\x8c\z
\xe2\x80\x8d\xe2\x80\x8e\xe2\x80\x8f\xe2\x80\x90\xe2\x80\x91\xe2\x80\x92\xe2\x80\x93\xe2\x80\x94\xe2\x80\x95\xe2\x80\x96\xe2\x80\x97\z
\xe2\x80\x98\xe2\x80\x99\xe2\x80\x9a\xe2\x80\x9b\xe2\x80\x9c\xe2\x80\x9d\xe2\x80\x9e\xe2\x80\x9f\xe2\x80\xa0\xe2\x80\xa1\xe2\x80\xa2\z
\xe2\x80\xa3\xe2\x80\xa4\xe2\x80\xa5\xe2\x80\xa6\xe2\x80\xa7\xe2\x80\xa8\xe2\x80\xa9\xe2\x80\xaa\xe2\x80\xab\xe2\x80\xac\xe2\x80\xad\z
\xe2\x80\xae\xe2\x80\xaf\xe2\x80\xb0\xe2\x80\xb1\xe2\x80\xb2\xe2\x80\xb3\xe2\x80\xb4\xe2\x80\xb5\xe2\x80\xb6\xe2\x80\xb7\xe2\x80\xb8\z
\xe2\x80\xb9\xe2\x80\xba\xe2\x80\xbb\xe2\x80\xbc\xe2\x80\xbd\xe2\x80\xbe\xe2\x80\xbf\xe2\x81\x80\xe2\x81\x81\xe2\x81\x82\xe2\x81\x83\z
\xe2\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d\z
\xe2\xb8\xbe\xe2\xb8\xbf\xe2\xb9\x81\xe2\xb9\x82\xe2\xb9\x83\xe2\xb9\x84\xe2\xb9\x85\xe2\xb9\x86\xe2\xb9\x87\xe2\xb9\x88\xe2\xb9\x89\z
\xe2\xb9\x8a\xe2\xb9\x8b\xe2\xb9\x8c\xe2\xb9\x8d\xe2\xb9\x8e\xe3\x80\x80\xe3\x80\x81\xe3\x80\x82\xe3\x80\x83\xe3\x80\x84\xe3\x80\x85\z
\xe3\x80\x86\xe3\x80\x87\xe3\x80\x88\xe3\x80\x89\xe3\x80\x8a\xe3\x80\x8b\xe3\x80\x8c\xe3\x80\x8d\xe3\x80\x8e\xe3\x80\x8f\xe3\x80\x90\z
\xe3\x80\x91\xe3\x80\x94\xe3\x80\x95\xe3\x80\x96\xe3\x80\x97\xe3\x80\x98\xe3\x80\x99\xe3\x80\x9a\xe3\x80\x9b\xe3\x80\x9c\xe3\x80\x9d\z
\xe3\x80\x9e\xe3\x80\x9f\xe3\x80\xb0\xe3\x80\xb1\xe3\x80\xb2\xe3\x80\xb3\xe3\x80\xb4\xe3\x80\xb5\xe3\x80\xbb\xe3\x80\xbc\xe3\x80\xbd\z
\xe3\x82\xa0\xea\x93\xbe\xea\x93\xbf\xea\x98\x8d\xea\x98\x8e\xea\x98\x8f\xea\x99\xb3\xea\x99\xbe\xea\x9b\xb2\xea\x9b\xb3\xea\x9b\xb4\z
\xea\x9b\xb5\xea\x9b\xb6\xea\x9b\xb7\xea\xa1\xb6\xea\xa1\xb7\xea\xa3\x8e\xea\xa3\x8f\xea\xa3\xb8\xea\xa3\xb9\xea\xa3\xba\xea\xa3\xbb\z
\xea\xa4\xae\xea\xa4\xaf\xea\xa5\x9f\xea\xa7\x81\xea\xa7\x82\xea\xa7\x83\xea\xa7\x84\xea\xa7\x85\xea\xa7\x86\xea\xa7\x87\xea\xa7\x88\z
\xea\xa7\x89\xea\xa7\x8a\xea\xa7\x8b\xea\xa7\x8c\xea\xa7\x8d\xea\xa9\x9c\xea\xa9\x9d\xea\xa9\x9e\xea\xa9\x9f\xea\xab\x9e\xea\xab\x9f\z
\xea\xab\xb0\xea\xab\xb1\xea\xaf\xab\xea\xaf\xac\xea\xaf\xad\xef\xb4\xbe\xef\xb4\xbf\xef\xbd\x9f\xef\xbd\xa0\xef\xbd\xa1\xef\xbd\xa2\z
\xef\xbd\xa3\xef\xbd\xa4\xf0\x90\x84\x80\xf0\x90\x84\x81\xf0\x90\x84\x82\xf0\x90\x8e\x9f\xf0\x90\x8f\x90\xf0\x90\x95\xaf\z
\xf0\x90\xa1\x97\xf0\x90\xa4\x9f\xf0\x90\xa4\xbf\xf0\x90\xa9\x90\xf0\x90\xa9\x91\xf0\x90\xa9\x92\xf0\x90\xa9\x93\xf0\x90\xa9\x94\z
\xf0\x90\xa9\x95\xf0\x90\xa9\x96\xf0\x90\xa9\x97\xf0\x90\xa9\x98\xf0\x90\xab\xb0\xf0\x90\xab\xb1\xf0\x90\xab\xb2\xf0\x90\xab\xb3\z
\xf0\x90\xab\xb4\xf0\x90\xab\xb5\xf0\x90\xab\xb6\xf0\x90\xac\xb9\xf0\x90\xac\xba\xf0\x90\xac\xbb\xf0\x90\xac\xbc\xf0\x90\xac\xbd\z
\xf0\x90\xac\xbe\xf0\x90\xac\xbf\xf0\x90\xae\x99\xf0\x90\xae\x9a\xf0\x90\xae\x9b\xf0\x90\xae\x9c\xf0\x90\xbd\x95\xf0\x90\xbd\x96\z
\xf0\x90\xbd\x97\xf0\x90\xbd\x98\xf0\x90\xbd\x99\xf0\x91\x81\x87\xf0\x91\x81\x88\xf0\x91\x81\x89\xf0\x91\x81\x8a\xf0\x91\x81\x8b\z
\xf0\x91\x81\x8c\xf0\x91\x81\x8d\xf0\x91\x82\xbe\xf0\x91\x82\xbf\xf0\x91\x83\x80\xf0\x91\x83\x81\xf0\x91\x85\x80\xf0\x91\x85\x81\z
\xf0\x91\x85\x82\xf0\x91\x85\x83\xf0\x91\x85\xb4\xf0\x91\x85\xb5\xf0\x91\x87\x85\xf0\x91\x87\x86\xf0\x91\x87\x87\xf0\x91\x87\x88\z
\xf0\x91\x87\x8d\xf0\x91\x87\x9a\xf0\x91\x87\x9b\xf0\x91\x87\x9c\xf0\x91\x87\x9d\xf0\x91\x88\xb8\xf0\x91\x88\xb9\xf0\x91\x88\xba\z
\xf0\x91\x88\xbb\xf0\x91\x88\xbc\xf0\x91\x88\xbd\xf0\x91\x8a\xa9\xf0\x91\x91\x8b\xf0\x91\x91\x8c\xf0\x91\x91\x8d\xf0\x91\x91\x8e\z
\xf0\x91\x91\x8f\xf0\x91\x97\x82\xf0\x91\x97\x83\xf0\x91\x97\x84\xf0\x91\x97\x85\xf0\x91\x99\x81\xf0\x91\x99\x82\xf0\x91\x99\x83\z
\xf0\x91\x99\xa0\xf0\x91\x99\xa1\xf0\x91\x99\xa2\xf0\x91\x99\xa3\xf0\x91\x99\xa4\xf0\x91\x99\xa5\xf0\x91\x99\xa6\xf0\x91\x99\xa7\z
\xf0\x91\x99\xa8\xf0\x91\x99\xa9\xf0\x91\x99\xaa\xf0\x91\x99\xab\xf0\x91\x99\xac\xf0\x91\x9c\xbc\xf0\x91\x9c\xbd\xf0\x91\x9c\xbe\z
\xf0\x91\x9c\xbf\xf0\x91\xa0\xbb\xf0\x91\xa9\x81\xf0\x91\xa9\x82\xf0\x91\xa9\x83\xf0\x91\xa9\x84\xf0\x91\xaa\x9a\xf0\x91\xaa\x9b\z
\xf0\x91\xaa\x9c\xf0\x91\xb1\x81\xf0\x91\xb1\x82\xf0\x91\xb1\x83\xf0\x91\xb1\xb0\xf0\x91\xb1\xb1\xf0\x91\xbb\xb7\xf0\x91\xbb\xb8\z
\xf0\x92\x91\xb0\xf0\x92\x91\xb1\xf0\x92\x91\xb2\xf0\x92\x91\xb3\xf0\x92\x91\xb4\xf0\x96\xa9\xae\xf0\x96\xa9\xaf\xf0\x96\xab\xb5\z
\xf0\x96\xac\xb7\xf0\x96\xac\xb8\xf0\x96\xac\xb9\xf0\x96\xac\xba\xf0\x96\xac\xbb\xf0\x96\xad\x84\xf0\x96\xad\x85\xf0\x96\xba\x97\z
\xf0\x96\xba\x98\xf0\x9b\xb2\x9f\xf0\x9d\x84\x94\xf0\x9d\x84\x95\xf0\x9d\xaa\x87\xf0\x9d\xaa\x88\xf0\x9d\xaa\x89\xf0\x9d\xaa\x8a\z
\xf0\x9d\xaa\x8b\xf0\x9e\xa5\x9e\xf0\x9e\xa5\x9f\xf0\x9f\x84\xaa\xf0\x9f\x89\x80\xf0\x9f\x89\x81\xf0\x9f\x89\x82\xf0\x9f\x89\x83\z
\xf0\x9f\x89\x84\xf0\x9f\x89\x85\xf0\x9f\x89\x86\xf0\x9f\x89\x87\xf0\x9f\x89\x88\xf0\x9f\x99\xb6\xf0\x9f\x99\xb7\xf0\x9f\x99\xb8\z
\xf0\x9f\x99\xb9\xf0\x9f\x99\xba\xf0\x9f\x99\xbb\xf0\x9f\x99\xbc\xf0\x9f\x99\xbd"
-- ----------------------------------------------------------------------------
-- list of hex values that are Unicode code-points labelled as punctuation
-- each punctuation character can span from [1..4]
-- values are in order from lowest to highest, which speeds up lookup
--
-- Note: values will be read from file, so there'll be a performance penalty
-- at the very first time of use
--
local sUniPunct = sUnicodePunct
local sUniPunctFile = ".\\unipunct.lua"
local function LoadPunctuation(inFilename)
-- list already in memory
--
if sUniPunct then return sUniPunct end
local fhSrc = io.open(sUniPunctFile, "r")
if not fhSrc then return nil end
fhSrc:close()
sUniPunct = dofile(sUniPunctFile)
return sUniPunct
end
-- ----------------------------------------------------------------------------
-- boolean test if a given byte sequence is an Unicode punctuation character
--
local function str_ispunct_u(inUniChar)
-- get the Unicode's punctuation list
--
local lsReject = sUniPunct
if not lsReject then lsReject = LoadPunctuation() end
-- sanity check
--
if not lsReject or not inUniChar then return false end
local iUniLen = inUniChar:len()
if 0 == iUniLen then return false end
-- get the selector and the required length
--
local chUniMark = inUniChar:byte(1, 1)
local iUniReq
-- don't care about a selector with invalid values
-- because the test routine will just fail over
--
if 0x80 > chUniMark then iUniReq = 1
elseif 0xe0 > chUniMark then iUniReq = 2
elseif 0xf0 > chUniMark then iUniReq = 3
else iUniReq = 4 end
-- requested length does not match with the input char
--
if iUniReq ~= iUniLen then return false end
-- test against list
--
local chTestMark
local iTestReq
local iOffset = 1
local iLimit = lsReject:len() - 4 + 1 -- 4 is the highset known value
while iOffset < iLimit do
-- fisrt byte of UTF_8 sequence
--
chTestMark = lsReject:byte(iOffset, iOffset)
-- test the sequence after the mark byte
--
if chTestMark == chUniMark then
-- quick shot, saves a loop
--
if 1 == iUniReq then return true
else
-- test each of the following bytes [2..4]
--
local iRun = 1
while iUniReq > iRun do
-- fail test
--
if lsReject:byte(iOffset + iRun, iOffset + iRun) ~=
inUniChar:byte(1 + iRun, 1 + iRun) then break end
iRun = iRun + 1
end
-- character sequences match
--
if iUniReq == iRun then return true end
end
end
-- we passed over the limit
--
if chTestMark > chUniMark then return false end
-- length of test character in UTF_8 punctuation list
--
if 0x80 > chTestMark then iTestReq = 1
elseif 0xe0 > chTestMark then iTestReq = 2
elseif 0xf0 > chTestMark then iTestReq = 3
else iTestReq = 4 end
-- skip and get next test
--
iOffset = iOffset + iTestReq
end
return false
end
-- ----------------------------------------------------------------------------
-- boolean test if a given byte value is an ASCII punctuation character
--
local function str_ispunct_a(inByte)
local lsReject = " \t\n\r:.;,°`\'\"<>()[]{}~!?@#£$§%^&*-_+=|\\/"
local iLength = #lsReject
for iOffset = 1, iLength do
if inByte == lsReject:sub(iOffset, iOffset) then return true end
end
return false
end
-- ----------------------------------------------------------------------------
-- extract an entire alpha-word (with punctuation) from a buffer
-- stops start and end at puctuation characters
--
local function str_wordsub(inBytes, inPosition)
-- sanity check
--
if not inBytes then return nil end
local iLength = inBytes:len()
if 1 > inPosition or inPosition > iLength then return nil end
-- get the first punctuation character before beginning of word
--
local ch
local iStart = inPosition
while 0 < iStart do
ch = inBytes:sub(iStart, iStart)
if str_ispunct_a(ch) then break end
iStart = iStart - 1
end
-- get first space character after end of word
--
local iEnd = inPosition
while iEnd <= iLength do
ch = inBytes:sub(iEnd, iEnd)
if str_ispunct_a(ch) then break end
iEnd = iEnd + 1
end
-- extract word from entire line
--
return inBytes:sub(iStart + 1, iEnd - 1)
end
-- ----------------------------------------------------------------------------
-- auxiliary function to cleanup extra characters at the end of the buffer
-- remove all characters with binary value below 0x20, except 0x0a and 0x0d
-- this function will process bytes up to the first [cr or lf] or end of buffer
-- thus will fail if the input buffer is a multiline
-- return the number of bytes removed and the cleaned buffer
--
local function str_endwipe(inBuffer)
local iLength = inBuffer:len()
local iLast = 0
local sReturn = ""
local sAppend = ""
local iCurr
if 0 == iLength then return 0, inBuffer end
-- scan for \n (and eventual \r)
--
for i=1, iLength do
iCurr = inBuffer:byte(i)
if 0x20 < iCurr then iLast = i
elseif 0x0a == iCurr then sAppend = "\n" break
elseif 0x0d == iCurr then sAppend = "\r\n" break
end
end
sReturn = _format("%s%s", inBuffer:sub(1, iLast), sAppend )
return (iLength - sReturn:len()), sReturn
end
-- ----------------------------------------------------------------------------
-- install in the string library
--
if not string.gcount then string.gcount = str_gcount end
if not string.ltrim then string.ltrim = str_ltrim end
if not string.rtrim then string.rtrim = str_rtrim end
if not string.trim then string.trim = str_trim end
if not string.endwipe then string.endwipe = str_endwipe end
if not string.cap1st then string.cap1st = str_cap1st end
if not string.fmtfind then string.fmtfind = str_fmtfind end
if not string.wordsub then string.wordsub = str_wordsub end
if not string.fmtkilo then string.fmtkilo = str_fmtkilo end
if not string.a_punct then string.a_punct = str_ispunct_a end
if not string.utf8lkp then string.utf8lkp = str_lkp_utf_8 end
if not string.utf8sub then string.utf8sub = str_sub_utf_8 end
if not string.utf8len then string.utf8len = str_len_utf_8 end
if not string.u_punct then string.u_punct = str_ispunct_u end
if not string.i_Uchar then string.i_Uchar = str_iter_utf_8_char end
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------