Coder Social home page Coder Social logo

lua-split's Introduction

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

lua-split's People

Contributors

moteus avatar tst2005 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.