Skip to content

Implement functions to split strings

License

Notifications You must be signed in to change notification settings

MNAJIM75/lua-split

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lua-Split

Build Status Coverage Status Licence


Functions

split(str, [sep, [plain])

Split string and returns array

t = split('aaa;bbb', ';', true)
-- t = {'aaa', 'bbb'}

unpack(str, [sep, [plain])

Split string and returns result as multiple values

a, b = split.unpack('aaa;bbb', ';', true)
-- a = 'aaa' b = 'bbb'

first(str, [sep, [plain])

Split first substring result as 2 values

key, val = split.first('pass=hello=world', '=', true)
-- key = 'pass' val = 'hello=world'

each(str, [sep, [plain])

Create iterator to iterate over substrings

for word in split.each('hello world', '%s') do
  print(word)
end

Example

-- decode header value like:
-- `value1;key1=1;key2=2,value2;key1=1;key2=2`
-- result:
-- {
--   {value1,{key=1,key2=2}};
--   {value2,{key=1,key2=2}};
-- }
function decode_header(str)
  local res = {}
  for ext in split.each(str, "%s*,%s*") do
    local name, tail = split.first(ext, '%s*;%s*')
    if #name > 0 then
      local opt  = {}
      if tail then
        for param in split.each(tail, '%s*;%s*') do
          local k, v = split.first(param, '%s*=%s*')
          opt[k] = v
        end
      end
      res[#res + 1] = {name, opt}
    end
  end
  return res
end

About

Implement functions to split strings

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 81.5%
  • Shell 18.5%