From aa86e67a71a4e87573f07043f1b7c5168fa42edb Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Wed, 12 Oct 2011 19:34:04 +0000 Subject: testing/lua-stringy: new aport Lua string utility library http://hackmap.blogspot.com --- .../0001-use-memcmp-for-startswith-endswith.patch | 78 ++++++++++++++++++++++ testing/lua-stringy/APKBUILD | 42 ++++++++++++ testing/lua-stringy/stringy-memcmp.patch | 64 ++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 testing/lua-stringy/0001-use-memcmp-for-startswith-endswith.patch create mode 100644 testing/lua-stringy/APKBUILD create mode 100644 testing/lua-stringy/stringy-memcmp.patch diff --git a/testing/lua-stringy/0001-use-memcmp-for-startswith-endswith.patch b/testing/lua-stringy/0001-use-memcmp-for-startswith-endswith.patch new file mode 100644 index 000000000..4680a0561 --- /dev/null +++ b/testing/lua-stringy/0001-use-memcmp-for-startswith-endswith.patch @@ -0,0 +1,78 @@ +From 86e4e9d16befd02230a699f045afdd68a47f6122 Mon Sep 17 00:00:00 2001 +From: Natanael Copa +Date: Wed, 12 Oct 2011 20:57:55 +0200 +Subject: [PATCH] use memcmp for startswith/endswith + +This fixes an uninitialized variable bug and should be faster since +most libc has optimized memcmp() + +Signed-off-by: Natanael Copa +--- + stringy/stringy.c | 43 +++++++++++++------------------------------ + 1 files changed, 13 insertions(+), 30 deletions(-) + +diff --git a/stringy/stringy.c b/stringy/stringy.c +index 3341b87..aa9a2a2 100644 +--- a/stringy/stringy.c ++++ b/stringy/stringy.c +@@ -65,44 +65,27 @@ static int endswith(lua_State *L) { + + size_t token_len; + const char *token = luaL_checklstring(L, 2, &token_len); ++ int end = 0; + +- int ti = token_len, si = string_len, end = 1; +- if(token_len <= string_len){ +- while(ti > 0) { +- if(string[--si] != token[--ti]){ +- end = 0; +- break; +- +- } +- } +- } +- else { +- end = 0; ++ if(token_len <= string_len) { ++ string += string_len - token_len; ++ end = memcmp(string, token, token_len) == 0; + } + lua_pushboolean(L, end); + return 1; + } + + static int startswith(lua_State *L) { +- const char *string = luaL_checkstring(L, 1); +- int string_len = lua_objlen(L, 1); ++ size_t string_len; ++ const char *string = luaL_checklstring(L, 1, &string_len); ++ ++ size_t token_len; ++ const char *token = luaL_checklstring(L, 2, &token_len); ++ int start = 0; ++ ++ if (token_len <= string_len) ++ start = memcmp(string, token, token_len) == 0; + +- const char *token = luaL_checkstring(L, 2); +- int token_len = lua_objlen(L, 2); +- int i, start = 1; +- // please make this less ugly... +- if(token_len <= string_len){ +- while(i < token_len) { +- if(string[i] != token[i]){ +- start = 0; +- break; +- } +- i++; +- } +- } +- else { +- start = 0; +- } + lua_pushboolean(L, start); + return 1; + } +-- +1.7.7 + diff --git a/testing/lua-stringy/APKBUILD b/testing/lua-stringy/APKBUILD new file mode 100644 index 000000000..3b268594d --- /dev/null +++ b/testing/lua-stringy/APKBUILD @@ -0,0 +1,42 @@ +# Contributor: Natanael Copa +# Maintainer: Natanael Copa +pkgname=lua-stringy +pkgver=0.2 +pkgrel=0 +pkgdesc="Lua string utility library" +url="http://hackmap.blogspot.com" +arch="all" +license="MIT" +depends="" +makedepends="lua-dev" +install="" +subpackages="" +source="http://bpbio.googlecode.com/files/stringy-$pkgver.tar.gz + stringy-memcmp.patch" + +_builddir="$srcdir"/stringy-$pkgver +prepare() { + local i + cd "$_builddir" + for i in $source; do + case $i in + *.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;; + esac + done +} + +build() { + cd "$_builddir" + ${CC:-gcc} ${CFLAGS} -fPIC -shared ${LDFLAGS} -llua -o stringy.so stringy.c || return 1 + lua stringy_test.lua +} + +package() { + local _lualibdir=/usr/lib/lua/5.1 + cd "$_builddir" + mkdir -p "$pkgdir"/$_lualibdir + cp stringy.so "$pkgdir"/$_lualibdir/ +} + +md5sums="c53f05a37410a234d4afa0aa3210d800 stringy-0.2.tar.gz +b3fbeca41227425894d6e0d74a684969 stringy-memcmp.patch" diff --git a/testing/lua-stringy/stringy-memcmp.patch b/testing/lua-stringy/stringy-memcmp.patch new file mode 100644 index 000000000..d9019b79c --- /dev/null +++ b/testing/lua-stringy/stringy-memcmp.patch @@ -0,0 +1,64 @@ +--- ./stringy.c.orig ++++ ./stringy.c +@@ -8,49 +8,27 @@ + + + static int endswith(lua_State *L) { +- const char *string = luaL_checkstring(L, 1); +- int string_len = lua_objlen(L, 1); ++ size_t string_len, token_len; ++ const char *string = luaL_checklstring(L, 1, &string_len); ++ const char *token = luaL_checklstring(L, 2, &token_len); ++ int end = 0; + +- const char *token = luaL_checkstring(L, 2); +- int token_len = lua_objlen(L, 2); +- +- int ti = token_len, si = string_len, end = 1; + if(token_len <= string_len){ +- while(ti > 0) { +- if(string[--si] != token[--ti]){ +- end = 0; +- break; +- +- } +- } ++ string += string_len - token_len; ++ end = memcmp(string, token, token_len) == 0; + } +- else { +- end = 0; +- } + lua_pushboolean(L, end); + return 1; + } + + static int startswith(lua_State *L) { +- const char *string = luaL_checkstring(L, 1); +- int string_len = lua_objlen(L, 1); ++ size_t string_len, token_len; ++ const char *string = luaL_checklstring(L, 1, &string_len); ++ const char *token = luaL_checklstring(L, 2, &token_len); ++ int start = 0; + +- const char *token = luaL_checkstring(L, 2); +- int token_len = lua_objlen(L, 2); +- int i, start = 1; +- // please make this less ugly... +- if(token_len <= string_len){ +- while(i < token_len) { +- if(string[i] != token[i]){ +- start = 0; +- break; +- } +- i++; +- } +- } +- else { +- start = 0; +- } ++ if(token_len <= string_len) ++ start = memcmp(string, token, token_len) == 0; + lua_pushboolean(L, start); + return 1; + } -- cgit v1.2.3