{"id":20265393,"url":"https://github.com/telemachus/split","last_synced_at":"2025-07-27T04:36:20.082Z","repository":{"id":109960941,"uuid":"81654682","full_name":"telemachus/split","owner":"telemachus","description":"A string split function and iterator for Lua","archived":false,"fork":false,"pushed_at":"2023-01-31T18:46:21.000Z","size":91,"stargazers_count":21,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-11T02:43:01.819Z","etag":null,"topics":["lua","split","text"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/telemachus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-02-11T13:29:40.000Z","updated_at":"2024-01-30T20:38:00.000Z","dependencies_parsed_at":"2023-03-22T14:32:20.400Z","dependency_job_id":null,"html_url":"https://github.com/telemachus/split","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/telemachus/split","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telemachus%2Fsplit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telemachus%2Fsplit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telemachus%2Fsplit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telemachus%2Fsplit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/telemachus","download_url":"https://codeload.github.com/telemachus/split/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telemachus%2Fsplit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267300047,"owners_count":24066091,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","split","text"],"created_at":"2024-11-14T11:47:05.278Z","updated_at":"2025-07-27T04:36:20.077Z","avatar_url":"https://github.com/telemachus.png","language":"Lua","readme":"# split\n\n## Synopsis\n\nA string `split` function and iterator for Lua since Lua's standard sting\nlibrary doesn't provide such a function. When working with text `split` is very\nuseful, and [many people have written a version for Lua][wiki].\n\n[wiki]: http://lua-users.org/wiki/SplitJoin\n\n## Usage\n\n+ `split(string, delimiter) =\u003e { results }`\t\n\n  The delimiter can be a literal string or a Lua pattern. The function returns\n  a table of items found by splitting the string up into pieces divided by the\n  delimiter. If the delimiter is not present in the string, then the result\n  will be a table consisting of one item: the original string parameter. Extra\n  delimiters anywhere in the string will result in empty strings being returned\n  as part of the results table.\n\n  The function also provides two shortcuts for common situations. If the\n  delimiter parameter is an empty string, the function returns a table\n  containing every character in the original string as a separate item. (I.e.,\n  if the delimiter is the empty string, the function explodes the string.) If\n  the delimiter parameter is `nil`, the function considers this equivalent to\n  the Lua pattern `'%s+'` and splits the string on whitespace.\n\n  Examples:\n\n    * Split on a literal character\n\n            local split = require 'split'.split\n            split('foo,bar,buzz', ',') -- returns {'foo', 'bar', 'buzz'}\n            split(',foo,bar,,buzz,', ',') -- returns {'', 'foo', 'bar', '', 'buzz', ''}\n\n    * Split on a Lua pattern\n\n            split('foo       bar\t\tbuzz', '%s+') -- returns {'foo', 'bar', 'buzz'}\n\n    * A special case: empty string delimiter\n\n        If the delimiter is an empty string, the function returns each\n        character from the original string as an individual item. Think of\n        this as \"explode the string\".\n\n            split('foo', '') -- returns {'f', 'o', 'o'}\n\n    * Another special case: `nil` delimiter\n\n        Pass nothing or an explicit `nil` as the delimiter and `split` acts as\n        if the delimiter were `'$s+'`. This makes it easier to split on\n        consecutive runs of whitespace.\n\n            split('foo       bar\tbuzz') -- returns {'foo', 'bar', 'buzz'}\n\n+ `each(string, delimiter) =\u003e custom iterator`\n\n  **NB**: This function was previously called `spliterator`, but I've renamed\n  it to the shorter and less goofy `each`. In order to give people who might\n  rely on the previous name time to switch over, `spliterator` is still\n  provided as an alias for `each`. However, that name will be removed in the\n  next major version release (i.e., 4.0.0) of this module.\n\n  This is an iterator version of the same idea as `split`. Everything from\n  above applies, except that the function returns a iterator to work through\n  results rather than a table.\n\n            local split_each = require 'split'.each\n\n            local str = 'foo,bar,bizz,buzz'\n            local count = 1\n            for p in split_each(str, ',') do\n              print(count .. '. [' .. p .. ']')\n              count = count + 1\n            end\n\n+ `first_and_rest(string, delimiter) =\u003e string, string (or nil)`\n\n  This function is a string equivalent for a function that divides a list into\n  its head and tail. The head of the string is everything that appears before\n  the first appearance of a specified delimiter; the tail is the rest of the\n  string. `first_and_rest` attempts to split a string into two pieces, and it\n  returns two results using Lua's multiple return. The exact return values vary\n  depending on the string and delimiter.\n\n  In the simplest case, the string contains the delimiter at least once. If so,\n  the first return value will be the portion of the string before the first\n  appearance of the delimiter, and the second return value will be the rest of\n  the string after that delimiter.\n\n  If the delimiter does not appear in the string, however, then there's no\n  possible split. In this case, the first return value will be the entire\n  string, and the second return value will be `nil`. (From Lua's point of view,\n  a second return value of `nil` is equivalent to saying that the function only\n  returns one value.)\n\n  If the second return value is `nil`, there is probably a problem or malformed\n  record. So it will often make sense to test the second return value before\n  proceeding. For example:\n\n            local head, tail = first_and_rest(record, '%s*:%s*')\n            if not tail then\n              -- Signal an error to the caller.\n            else\n              -- Process the record.\n            end\n\n  A second complication is that the strings returned by the function may be\n  empty. If the delimiter is found, but the portion of the string before or\n  after it is zero-length, then an empty string may be returned. The examples\n  below show various possible outcomes.\n\n            first_and_rest('head: tail', ': ') -- returns 'head', 'tail'\n            first_and_rest('head, tail', ': ') -- returns 'head, tail', nil\n            first_and_rest(': tail', ': ') -- returns '', 'tail'\n            first_and_rest('head: ', ': ') -- returns 'head', ''\n\n  Like `split` and `each`, `first_and_rest` accepts `nil` or an empty string as\n  special cases for the delimiter. `nil` is automatically transformed into\n  '%s+', a generic \"separated by space\" pattern. In the case of an empty string\n  delimiter, `first_and_rest` returns the first character of the input and the\n  rest of the input. (This seems to be the only reasonable interpretation of\n  \"exploding\" the input string in the context of this function.)\n\n## Varia\n\nThe module provides four informational functions that return strings. They\nshould be self-explanatory.\n\n+ `version() -- 3.2.1`\n\n+ `author() -- Peter Aronoff`\n\n+ `url() -- https://github.com/telemachus/split`\n\n+ `license() -- BSD 3-Clause`\n\n## Credits\n\nMany of my ideas came from reading [the LuaWiki page on split][wiki]. I thank\nall those contributors for their suggestions and examples.\n\n[Alexey Melnichuk, AKA moteus][moteus] provided the idea and initial code for\n`first_and_rest`.\n\nAll mistakes are mine. See [version history][c] for release details.\n\n[moteus]: https://bitbucket.org/moteus\n[c]: /CHANGES.md\n\n---\n\n(c) 2012-2023 Peter Aronoff. BSD 3-Clause license; see [LICENSE.md][li] for\ndetails.\n\n[li]: /LICENSE.md\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelemachus%2Fsplit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftelemachus%2Fsplit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelemachus%2Fsplit/lists"}