summaryrefslogtreecommitdiffstats
path: root/testing/lua5.2-stringy/stringy-memcmp.patch
blob: d9019b79cc3696b4efebace70e24ddaf9fd48128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
 }