github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/stringlib/lua/matching.quotas.lua (about)

     1  -- Tests for string.find
     2  do
     3      -- string.find in plain mode consumes cpu proportional to search string
     4      print(runtime.callcontext({kill={cpu=1000}}, string.find, ("straw"):rep(20).."needle", "needle", 1, true))
     5      --> =done	101	106
     6  
     7      print(runtime.callcontext({kill={cpu=1000}}, string.find, ("straw"):rep(200).."needle", "needle", 1, true))
     8      --> =killed
     9  
    10      -- string.find in pattern mode consumes cpu proportional to the amount of
    11      -- searching
    12      print(runtime.callcontext({kill={cpu=10000}}, string.find, ("a"):rep(50), ".-b"))
    13      --> =done	nil
    14  
    15      print(runtime.callcontext({kill={cpu=10000}}, string.find, ("a"):rep(500), ".-b"))
    16      --> =killed
    17  
    18      -- captures consumes memory
    19      print(runtime.callcontext({kill={memory=1000}}, string.find, "abbbbbbbbbbc", "(b+)"))
    20      --> =done	2	11	bbbbbbbbbb
    21  
    22      print(runtime.callcontext({kill={memory=3000}}, string.find, "a"..("b"):rep(1000).."c", "(((b+)))"))
    23      --> =killed
    24  end
    25  
    26  -- Tests for string.match
    27  do
    28      -- string.match consumes cpu proportional to the amount of searching
    29      print(runtime.callcontext({kill={cpu=10000}}, string.match, ("a"):rep(50), ".-b"))
    30      --> =done	nil
    31  
    32      print(runtime.callcontext({kill={cpu=10000}}, string.match, ("a"):rep(500), ".-b"))
    33      --> =killed
    34  
    35      -- captures consumes memory
    36      print(runtime.callcontext({kill={memory=1000}}, string.match, "abbbbbbbbbbc", "(b+)"))
    37      --> =done	bbbbbbbbbb
    38  
    39      print(runtime.callcontext({kill={memory=3000}}, string.match, "a"..("b"):rep(1000).."c", "(((b+)))"))
    40      --> =killed
    41  end
    42  
    43  -- Tests for string.gmatch
    44  do
    45      local wc
    46      local function countwords(s)
    47          wc = 0
    48          for w in string.gmatch(s, "%w+") do
    49              wc = wc + 1
    50          end
    51      end
    52  
    53      -- every match returned consumes cpu
    54      print(runtime.callcontext({kill={cpu=1000}}, countwords, ("hello"):rep(10, " ")))
    55      --> =done
    56      print(wc)
    57      --> =10
    58  
    59      print(runtime.callcontext({kill={cpu=1000}}, countwords, ("hello"):rep(1000, " ")))
    60      --> =killed
    61      print(wc > 10 and wc < 200)
    62      --> =true
    63  
    64      -- every match returned consumes memory
    65      print(runtime.callcontext({kill={memory=1000}}, countwords, ("hello"):rep(10, " ")))
    66      --> =done
    67      print(wc)
    68      --> =10
    69  
    70      print(runtime.callcontext({kill={memory=1000}}, countwords, ("hello"):rep(1000, " ")))
    71      --> =killed
    72      print(wc > 10 and wc < 200)
    73      --> =true
    74  end
    75  
    76  -- Tests for string.gsub
    77  do
    78      -- 1. Replacemement string
    79  
    80      print(runtime.callcontext({kill={cpu=1000}}, string.gsub, "a b c", "%w+", "%0 %0 %0"))
    81      --> =done	a a a b b b c c c	3
    82  
    83      -- It takes cpu to parse the input string
    84      print(runtime.callcontext({kill={cpu=1000}}, string.gsub, ("a"):rep(1000), "%w", "%0"))
    85      --> =killed
    86  
    87      -- It takes cpu to parse the replacement string
    88      print(runtime.callcontext({kill={cpu=1000}}, string.gsub, "a b c", "%w+", ("a"):rep(1000)))
    89      --> =killed
    90  
    91      -- Building the substitution consumes memory
    92      print(runtime.callcontext({kill={memory=1000}}, string.gsub, "1234567890", "%w", ("a"):rep(100)))
    93      --> =killed
    94  
    95      -- 2. Replacement function
    96  
    97      print(runtime.callcontext({kill={memory=1000}}, string.gsub, "1234567890", "%w", function(x) return x:rep(2) end))
    98      --> =done	11223344556677889900	10
    99  
   100      -- Building the substitution consumes memory
   101      print(runtime.callcontext({kill={memory=1000}}, string.gsub, "1234567890", "%w", function(x) return x:rep(100) end))
   102      --> =killed
   103  
   104      -- 3. Replacement table
   105  
   106      local t = {
   107          a = "A",
   108          b = ("B"):rep(100),
   109      }
   110  
   111      print(runtime.callcontext({kill={memory=1000}}, string.gsub, ("a"):rep(10), ".", t))
   112      --> =done	AAAAAAAAAA	10
   113  
   114      -- Building the substitution consumes memory
   115      print(runtime.callcontext({kill={memory=1000}}, string.gsub, ("b"):rep(100), ".", t))
   116      --> =killed
   117  end