Created
April 4, 2010 19:14
-
-
Save brentp/355629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package = "stringext" | |
version = "0.2-1" | |
source = { | |
url = "http://bpbio.googlecode.com/files/stringext-0.2.tar.gz" | |
} | |
description = { | |
summary = "C extension for string.split, string.strip", | |
detailed = [[ | |
Implementation of string.split and string.strip in the | |
lua c-api as borrowed from the wiki and book. | |
This was implemented as a learning exercise. Additions | |
are welcome if the library is kept simple. Usage is: | |
> require "stringext" -- adds to string methods | |
> astr = " asdf " | |
> = astr:strip() | |
asdf | |
-- split() returns a table/array. | |
> bstr = "aXbXc" | |
> table.concat(bstr.split("X"), "|") | |
a|b|c | |
]], | |
homepage = "http://hackmap.blogspot.com", | |
license = "MIT/X11" | |
} | |
dependencies = { | |
"lua >= 5.1" | |
} | |
build = { | |
type = "builtin", | |
modules = { | |
stringext = "stringext.c" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LUA_CFLAGS=`pkg-config lua5.1 --cflags` | |
all: stringext.so | |
stringext.so: stringext.c | |
gcc $(LUA_CFLAGS) -O3 -fPIC -o stringext.o -c stringext.c | |
gcc -shared -O3 stringext.o -o stringext.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LUA_LIB | |
#include <lua.h> | |
#include <lauxlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stddef.h> | |
#include <ctype.h> | |
static int split(lua_State *L) { | |
const char *string = luaL_checkstring(L, 1); | |
const char *sep = luaL_checkstring(L, 2); | |
const char *token; | |
int i = 1; | |
lua_newtable(L); | |
while ((token = strchr(string, *sep)) != NULL) { | |
lua_pushlstring(L, string, token - string); | |
lua_rawseti(L, -2, i++); | |
string = token + 1; | |
} | |
lua_pushstring(L, string); | |
lua_rawseti(L, -2, i); | |
return 1; | |
} | |
// http://lua-users.org/wiki/StringTrim | |
int strip(lua_State *L) { | |
const char *front; | |
const char *end; | |
size_t size; | |
front = luaL_checklstring(L, 1, &size); | |
end = &front[size - 1]; | |
for ( ; size && isspace(*front) ; size-- , front++) | |
; | |
for ( ; size && isspace(*end) ; size-- , end--) | |
; | |
lua_pushlstring(L, front, (size_t)(end - front) + 1); | |
return 1; | |
} | |
static const luaL_reg stringext[] = { | |
{"split", split}, | |
{"strip", strip}, | |
{NULL, NULL} | |
}; | |
int luaopen_stringext(lua_State *L){ | |
luaL_openlib(L, "string", stringext, 0); | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "stringext" | |
b = ("asdfsk"):split("s") | |
assert(#b == 3, #b) | |
print(table.concat(string.split("a\tb\tc", "\t"), "|")) | |
assert(("asdf"):split("s")[1] == "a") | |
print(table.concat(string.split("aXXbXXc", "XX"), "|")) | |
assert(string.strip(" asdf ") == "asdf") | |
assert(string.strip("asdf ") == "asdf") | |
assert(string.strip(" asdf ") == "asdf") | |
assert(string.strip("asdf") == "asdf") | |
assert(("asdf"):strip() == "asdf") | |
assert(("\nasdf\r\n"):strip() == "asdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment