File size: 1,798 Bytes
d7c5875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- This file is part of SA MoonLoader package.
-- Licensed under the MIT License.
-- Copyright (c) 2016, BlastHack Team <blast.hk>

local util = require 'extensions-lite.core.util'
local stringex = {}

local patternescape = function(str) -- lume for funcs
   return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
end

function stringex.split(str, delim, plain) -- bh FYP
   util.checkType("string", str, "string", delim)
   -- assert(type(str) == "string", ("bad argument #1 to 'split' (string expected, got %s)"):format(type(str)))
   -- assert(type(delim) == "string", ("bad argument #2 to 'split' (string expected, got %s)"):format(type(delim)))
   local tokens, pos, plain = {}, 1, not (plain == false) --[[ delimiter is plain text by default ]]
   repeat
       local npos, epos = string.find(str, delim, pos, plain)
       table.insert(tokens, string.sub(str, pos, npos and npos - 1))
       pos = epos and epos + 1
   until not pos
   return tokens
end

function stringex.trim(str, chars) -- lume
   util.checkType("string", str)
   if not chars then
      return str:match("^[%s]*(.-)[%s]*$")
   end
   local chars = patternescape(chars)
   return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
end

function stringex.contains(str, substr)
   util.checkType("string", str, "string", substr)
   return string.find(str, substr, 1, true) ~= nil
end

function stringex.rfind(str, pattern, offset, plain)
   util.checkType("string", str, "string", pattern)
   local pos, lnpos, lepos, plain = offset and offset - 1 or 0, offset or 1, -1, not (plain == false)
   repeat
      local npos, epos = string.find(str, pattern, pos, plain)
      pos = epos and epos + 1
      if pos then
         lnpos, lepos = npos, epos
      end
   until not pos
   return lnpos, lepos
end

return stringex