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

     1  local function errtest(f)
     2      return function(...)
     3          local ok, err = pcall(f, ...)
     4          if ok then
     5              print"OK"
     6          else
     7              print(err)
     8          end
     9      end
    10  end
    11  
    12  do
    13      local s = "hello"
    14      print(string.byte(s))
    15      --> =104
    16  
    17      print(string.byte(s, -1))
    18      --> =111
    19  
    20      print(string.byte(s, 2, 4))
    21      --> =101	108	108
    22  
    23      print(string.byte(s, -5, 2))
    24      --> =104	101
    25      
    26      print(string.byte(s, -2, -1))
    27      --> =108	111
    28  
    29      -- Byte can also be called as a method on strings
    30      print(s:byte(3))
    31      --> =108
    32  
    33      local errbyte = errtest(string.byte)
    34  
    35      errbyte()
    36      --> ~value needed
    37  
    38      errbyte({})
    39      --> ~must be a string
    40  
    41      errbyte("xxx", true)
    42      --> ~must be an integer
    43  
    44      errbyte("xxx", 1, nil)
    45      --> ~must be an integer
    46  end
    47  
    48  do
    49      print(string.char(65, 66, 67))
    50      --> =ABC
    51  
    52      local errchar = errtest(string.char)
    53  
    54      errchar(-1)
    55      --> ~out of range
    56  
    57      errchar(256)
    58      --> ~out of range
    59  
    60      errchar(1, 2, "x")
    61      --> ~must be integers
    62  end
    63  
    64  do
    65      print(string.len("abc"), string.len(""))
    66      --> =3	0
    67  
    68      errtest(string.len)()
    69      --> ~value needed
    70  
    71      errtest(string.len)(123)
    72      --> ~must be a string
    73  end
    74  
    75  do
    76      local s = "ABCdef123"
    77      print(s:lower())
    78      --> =abcdef123
    79  
    80      print(s:upper())
    81      --> =ABCDEF123
    82  
    83      errtest(string.lower)()
    84      --> ~value needed
    85  
    86      errtest(string.lower)(123)
    87      --> ~must be a string
    88  
    89      errtest(string.upper)()
    90      --> ~value needed
    91  
    92      errtest(string.upper)(123)
    93      --> ~must be a string
    94  end
    95  
    96  do
    97      local s = "xy"
    98      for i = 0, 3 do
    99          print(s:rep(i))
   100      end
   101      --> =
   102      --> =xy
   103      --> =xyxy
   104      --> =xyxyxy
   105  
   106      for i = 0, 3 do
   107          print(s:rep(i, "--"))
   108      end
   109      --> =
   110      --> =xy
   111      --> =xy--xy
   112      --> =xy--xy--xy
   113  
   114      local errrep = errtest(string.rep)
   115  
   116      errrep()
   117      --> ~2 arguments needed
   118  
   119      errrep(1, 2)
   120      --> ~must be a string
   121  
   122      errrep("xx", {})
   123      --> ~must be an integer
   124  
   125      errrep("xx", -1)
   126      --> ~out of range
   127  
   128      errrep("x", 2, 1)
   129      --> ~must be a string
   130  
   131      errrep("xxxxxxxxxx", 1 << 62)
   132      --> ~overflow
   133  
   134      errrep("xx", 1 << 62, ";")
   135      --> ~overflow
   136  end
   137  
   138  do
   139      local s = "EGASSEM TERCES"
   140      print(s:reverse())
   141      --> =SECRET MESSAGE
   142  
   143      print(string.reverse("12345"))
   144      --> =54321
   145  
   146      errtest(string.reverse)()
   147      --> ~value needed
   148  
   149      errtest(string.reverse)(123)
   150      --> ~must be a string
   151  end
   152  
   153  do
   154      local s = "abc"
   155      for i = -4, 4 do
   156          print(s:sub(i))
   157      end
   158      --> =abc
   159      --> =abc
   160      --> =bc
   161      --> =c
   162      --> =abc
   163      --> =abc
   164      --> =bc
   165      --> =c
   166      --> =
   167  
   168      print(s:sub(2, 3))
   169      --> =bc
   170  
   171      print(s:sub(3, 6))
   172      --> =c
   173  
   174      local subtest = errtest(string.sub)
   175  
   176      subtest()
   177      --> ~2 arguments needed
   178  
   179      subtest(1, 2)
   180      --> ~must be a string
   181  
   182      subtest("x", {})
   183      --> ~must be an integer
   184  
   185      subtest("xxx", 1, true)
   186      --> ~must be an integer
   187  end
   188  
   189  do
   190      local function pf(...)
   191          print(string.format(...))
   192      end
   193      
   194      local errf = errtest(string.format)
   195  
   196      pf("%s=%f", "pi", 3.14)
   197      --> =pi=3.140000
   198  
   199      pf("-%s-%s-%s", nil, true, false)
   200      --> =-nil-true-false
   201  
   202      pf("%% %q %%", [["hello"	123]])
   203      --> =% "\"hello\"\t123" %
   204  
   205      pf("%d//%5d//%-5d//%+d//%05d", 10.0, "10", 10, 10, 10)
   206      --> =10//   10//10   //+10//00010
   207  
   208      pf("%.2f~~%5.2f~~%-5.2f~~%+.2f~~%05.2f", 3.14, "3.14", 3.14, 3.14, 3.14)
   209      --> =3.14~~ 3.14~~3.14 ~~+3.14~~03.14
   210  
   211      -- To many values is OK
   212      pf("%s", 1, 2, 3)
   213      --> =1
   214  
   215      pf("%c", 65)
   216      --> =A
   217  
   218      errf("%c")
   219      --> ~not enough values
   220  
   221      errf("%c", {})
   222      --> ~invalid value
   223  
   224      errf("%d", "hello")
   225      --> ~invalid value
   226  
   227      pf("-%u-", 55)
   228      --> =-55-
   229  
   230      pf("%i", -12)
   231      --> =-12
   232  
   233      pf("%x", 255)
   234      --> =ff
   235  
   236      errf("%e")
   237      --> ~not enough values
   238  
   239      errf("%e", false)
   240      --> ~invalid value
   241  
   242      errf('"%s"')
   243      --> ~not enough values
   244  
   245      local t = {}
   246      setmetatable(t, {__tostring=function() error("bad", 0) end})
   247      errf("%s", t)
   248      --> =bad
   249  
   250      errf("%q")
   251      --> ~not enough values
   252  
   253      pf("[%q]", nil)
   254      --> =[nil]
   255  
   256      errf("%q", {})
   257      --> ~no literal
   258  
   259      pf("%q %q %q", false, 1, 1.5)
   260      --> =false 1 1.5
   261  
   262      print(pcall(pf, "%10q", 2))
   263      --> ~false\t.*cannot have modifiers
   264  
   265      errf("%t")
   266      --> ~not enough values
   267  
   268      pf("This is %t", true)
   269      --> =This is true
   270  
   271      errf("%t", 1)
   272      --> ~invalid value
   273  
   274      errf("%z")
   275      --> ~invalid format string
   276  
   277      -- Not enough values is not OK
   278      print(pcall(pf, "%s %d", 1))
   279      --> ~^false\t.*$
   280  
   281      errf()
   282      --> ~value needed
   283  
   284      errf(321)
   285      --> ~must be a string
   286  
   287      -- New in Lua 5.4: %p formatting
   288  
   289      local function ps(x) return string.format("%p", x) end
   290  
   291      local t = {}
   292      pf("=%p=", t)
   293      --> ~=0x[0-9a-f]+=
   294  
   295      local tp = ps(t)
   296      t.x = "something"
   297      print(ps(t) == tp)
   298      --> =true
   299  
   300      pf("%p", 1)
   301      --> =(null)
   302      pf("%p", true)
   303      --> =(null)
   304      pf("%p", 2.5)
   305      --> =(null)
   306      pf("%p", nil)
   307      --> =(null)
   308  
   309      pf("=%p=", "hello")
   310      --> ~=0x[0-9a-f]+=
   311  
   312      pf("=%p=", print)
   313      --> ~=0x[0-9a-f]+=
   314  
   315      print(ps(print) == ps(print))
   316      --> =true
   317  
   318      pf("=%p=", coroutine.running())
   319      --> ~=0x[0-9a-f]+=
   320  
   321      print(ps(coroutine.running()) == ps(coroutine.running()))
   322      --> =true
   323  
   324      pf("=%p=", io.stdout)
   325      --> ~=0x[0-9a-f]+=
   326  
   327      print(ps(io.stdout) == ps(io.stdout))
   328      --> =true
   329  
   330      print(ps(io.stdout) == ps(io.stdin))
   331      --> =false
   332  
   333      pf("=%p=", pf)
   334      --> ~=0x[0-9a-f]+=
   335  
   336      print(ps(pf) == ps(pf))
   337      --> =true
   338  
   339      print(ps(pf) == ps(ps))
   340      --> =false
   341  
   342      -- In Lua 5.4 infinite floats parse to out of range float literals
   343      pf("%q,%q", math.huge, -math.huge)
   344      --> =1e9999,-1e9999
   345  
   346      pf("%q", 0/0)
   347      --> =(0/0)
   348  end
   349  
   350  do
   351      local function dl(...)
   352          return load(string.dump(...))
   353      end
   354      
   355      local function f(x)
   356          print(string.format("%s squared is %s", x, x*x))
   357      end
   358  
   359      print(type(string.dump(f)))
   360      --> =string
   361  
   362      dl(f)(2)
   363      --> =2 squared is 4
   364  
   365      dl(f, true)(10)
   366      --> =10 squared is 100
   367  
   368      local function g(x)
   369          return function(y)
   370              print("Working...")
   371              return x + y + 2
   372          end
   373      end
   374  
   375      print(dl(g)(3)(5))
   376      --> =Working...
   377      --> =10
   378  
   379      local errd = errtest(string.dump)
   380  
   381      errd()
   382      --> ~value needed
   383  
   384      errd(true)
   385      --> ~must be a lua function
   386  end