@@ -338,12 +338,18 @@ end)
338
338
/* *****************************************************************************/
339
339
340
340
local function checkregex (data , pattern )
341
- local limits = {15000 , 500 , 150 , 70 , 40 } -- Worst case is about 200ms
342
- local n = 0 for i in string.gmatch (string.gsub (pattern , " %%." , " " ), " [%+%-%*]" ) do n = n + 1 end
341
+ local limits = {[0 ] = 50000000 , 15000 , 500 , 150 , 70 , 40 } -- Worst case is about 200ms
342
+ -- strip escaped things
343
+ local stripped , nrepl = string.gsub (pattern , " %%." , " " )
344
+ -- strip bracketed things
345
+ stripped , nrepl2 = string.gsub (stripped , " %[.-%]" , " " )
346
+ -- strip captures
347
+ stripped = string.gsub (stripped , " [()]" , " " )
348
+ -- Find extenders
349
+ local n = 0 for i in string.gmatch (stripped , " [%+%-%*]" ) do n = n + 1 end
343
350
local msg
344
- if n == 0 then return
345
- elseif n <=# limits then
346
- if # data > limits [n ] then msg = n .. " ext search length too long (" .. limits [n ].. " max)" else return end
351
+ if n <=# limits then
352
+ if # data * (# stripped + nrepl - n + nrepl2 )> limits [n ] then msg = n .. " ext search length too long (" .. limits [n ].. " max)" else return end
347
353
else
348
354
msg = " too many extenders"
349
355
end
@@ -356,7 +362,7 @@ local find = string.find
356
362
357
363
--- Returns the 1st occurrence of the string <pattern>, returns 0 if not found. Prints malformed string errors to the chat area.
358
364
e2function number string :findRE (string pattern )
359
- local OK , Ret = pcall (function () checkregex (this , pattern ) string.find (this , pattern ) end )
365
+ local OK , Ret = pcall (function () checkregex (this , pattern ) return string.find (this , pattern ) end )
360
366
if not OK then
361
367
self .player :ChatPrint (Ret )
362
368
return 0
367
373
368
374
--- Returns the 1st occurrence of the string <pattern> starting at <start> and going to the end of the string, returns 0 if not found. Prints malformed string errors to the chat area.
369
375
e2function number string :findRE (string pattern , start )
370
- local OK , Ret = pcall (function () checkregex (this , pattern ) find (this , pattern , start ) end )
376
+ local OK , Ret = pcall (function () checkregex (this , pattern ) return find (this , pattern , start ) end )
371
377
if not OK then
372
378
self .player :ChatPrint (Ret )
373
379
return 0
394
400
395
401
--- Finds and replaces every occurrence of <pattern> with <new> using regular expressions. Prints malformed string errors to the chat area.
396
402
e2function string string :replaceRE (string pattern , string new )
397
- local OK , Ret = pcall (function () checkregex (this , pattern ) gsub (this , pattern , new ) end )
403
+ local OK , Ret = pcall (function () checkregex (this , pattern ) return gsub (this , pattern , new ) end )
398
404
if not OK then
399
405
self .player :ChatPrint (Ret )
400
406
return " "
@@ -414,7 +420,7 @@ e2function array string:explode(string delim)
414
420
end
415
421
416
422
e2function array string :explodeRE ( string delim )
417
- local ok , ret = pcall (function () checkregex (this , pattern ) string_Explode ( delim , this , true ) end )
423
+ local ok , ret = pcall (function () checkregex (this , pattern ) return string_Explode ( delim , this , true ) end )
418
424
if not ok then
419
425
self .player :ChatPrint (ret )
420
426
ret = {}
@@ -453,7 +459,7 @@ local table_remove = table.remove
453
459
454
460
--- runs [[string.match]](<this>, <pattern>) and returns the sub-captures as an array. Prints malformed pattern errors to the chat area.
455
461
e2function array string :match (string pattern )
456
- local args = {pcall (function () checkregex (this , pattern ) string_match (this , pattern ) end )}
462
+ local args = {pcall (function () checkregex (this , pattern ) return string_match (this , pattern ) end )}
457
463
if not args [1 ] then
458
464
self .player :ChatPrint (args [2 ] or " Unknown error in str:match" )
459
465
return {}
465
471
466
472
--- runs [[string.match]](<this>, <pattern>, <position>) and returns the sub-captures as an array. Prints malformed pattern errors to the chat area.
467
473
e2function array string :match (string pattern , position )
468
- local args = {pcall (function () checkregex (this , pattern ) string_match (this , pattern , position ) end )}
474
+ local args = {pcall (function () checkregex (this , pattern ) return string_match (this , pattern , position ) end )}
469
475
if not args [1 ] then
470
476
self .player :ChatPrint (args [2 ] or " Unknown error in str:match" )
471
477
return {}
501
507
--- runs [[string.gmatch]](<this>, <pattern>) and returns the captures in an array in a table. Prints malformed pattern errors to the chat area.
502
508
-- (By Divran)
503
509
e2function table string :gmatch (string pattern )
504
- local OK , ret = pcall (function () checkregex (this , pattern ) gmatch (self , this , pattern ) end )
510
+ local OK , ret = pcall (function () checkregex (this , pattern ) return gmatch (self , this , pattern ) end )
505
511
if (!OK ) then
506
512
self .player :ChatPrint ( ret or " Unknown error in str:gmatch" )
507
513
return newE2Table ()
514
520
-- (By Divran)
515
521
e2function table string :gmatch (string pattern , position )
516
522
this = this :Right ( - position - 1 )
517
- local OK , ret = pcall (function () checkregex (this , pattern ) gmatch (self , this , pattern ) end )
523
+ local OK , ret = pcall (function () checkregex (this , pattern ) return gmatch (self , this , pattern ) end )
518
524
if (!OK ) then
519
525
self .player :ChatPrint ( ret or " Unknown error in str:gmatch" )
520
526
return newE2Table ()
525
531
526
532
--- runs [[string.match]](<this>, <pattern>) and returns the first match or an empty string if the match failed. Prints malformed pattern errors to the chat area.
527
533
e2function string string :matchFirst (string pattern )
528
- local OK , Ret = pcall (function () checkregex (this , pattern ) string_match (this , pattern ) end )
534
+ local OK , Ret = pcall (function () checkregex (this , pattern ) return string_match (this , pattern ) end )
529
535
if not OK then
530
536
self .player :ChatPrint (Ret )
531
537
return " "
536
542
537
543
--- runs [[string.match]](<this>, <pattern>, <position>) and returns the first match or an empty string if the match failed. Prints malformed pattern errors to the chat area.
538
544
e2function string string :matchFirst (string pattern , position )
539
- local OK , Ret = pcall (function () checkregex (this , pattern ) string_match (this , pattern , position ) end )
545
+ local OK , Ret = pcall (function () checkregex (this , pattern ) return string_match (this , pattern , position ) end )
540
546
if not OK then
541
547
self .player :ChatPrint (Ret )
542
548
return " "
0 commit comments