Allen

An utility library to manipulate strings in Lua

Allen is a Lua library which provides a set of utilities and helpers for strings operations. In a nutshell, Allen is to strings what Moses is to tables. Allen can be considered as an extension of the built-in Lua string library.
Allen's API was heavily inspired by the Javascript library Underscore.string.

Installation

Put the file Allen.lua inside your project folder and call it using require command. It will return a table containing a set of functions.

Usage example

local lyrics = 'hey I just met you\nand this is crazy\nbut here is my number\nso call me maybe'
lyrics = _.lines(lyrics)
for i,line in ipairs(lyrics) do 
  line = _.capitalizeFirst(line) 
end
-- => { 'Hey I just met you',
--      'And this is crazy',
--      'But here is my number',
--      'So call me maybe'}

Allen and the built-in string library

Allen provides a very handy function named _.import, which links Lua's native string library with Allen's functions.

_.import()
print(string.capitalize('hello'))
print(('Hello'):splice(2,2,'r'))

About Lua Operators

Once Allen is called inside your projet,it enables the use of arithmetic operators on strings by default.
Compatible operators are : +,*,/,%,^,-.

Strings Indexing

Using Allen, strings can be indexed, like regular Lua tables.

for i = 1,5 do 
  print(('Accessing %s[%d] :'):format('Hello',i),('Hello')[i]) 
end
-- => Accessing Hello[1] :  H
-- => Accessing Hello[2] :  e
-- => Accessing Hello[3] :  l
-- => Accessing Hello[4] :  l
-- => Accessing Hello[5] :  o

Strings as functions

Once Allen is loaded inside a project, strings can be called as functions.

local s = 'Hello'
print(s()) --> 'Hello'

Passing a positive number i as the first argument will return the character at the corresponding index

local s = 'Hello'
print(s(2)) --> 'e'

Passing a number j as a second argument will return the substring between i and j

local s = 'Hello'
print(s(2,4)) --> 'ell'

Passing a character as a second argument will replace the character at index i

local s = 'Helo'
print(s(3,'r')) --> 'Hero'

See Allen_StringOp_Snippets.lua for more details.

API Overview

Allen offers a lot of functions, iterators to operate on strings.
A fully complete documentation, with usage examples can be found here : Documentation
Some usage examples can be found here : Allen_Lib_Snippets.lua and Allen_StringOp_Snippets.lua

Credits and Thanks

Esa-Matti Suuronen and Edward Tsech, for the amazing Underscore.string

License

This work is under MIT-LICENSE
Copyright (c) 2012 Roland Yonaba

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.