aboutsummaryrefslogtreecommitdiff
path: root/src/snippets.js
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-09-05 18:02:55 -0700
committeralon@honor <none@none>2010-09-05 18:02:55 -0700
commit81161db178519d189e930b423832193802364f63 (patch)
tree8632ec43d230b936a68539d1c3efcdd84a800a6a /src/snippets.js
parented6f0355817810a757e4e464474e03c69264ce5f (diff)
snippets for C string funcs
Diffstat (limited to 'src/snippets.js')
-rw-r--r--src/snippets.js37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/snippets.js b/src/snippets.js
index a93fd0fd..83cc26e6 100644
--- a/src/snippets.js
+++ b/src/snippets.js
@@ -17,12 +17,45 @@ var Snippets = {
},
strspn: function(pstr, pset) {
- var str = String_copy(pstr);
+ var str = String_copy(pstr, true);
var set = String_copy(pset);
var i = 0;
- while (set.indexOf(str[i]) != -1) i++; // Must halt, as 0 is in both
+ while (set.indexOf(str[i]) != -1) i++; // Must halt, as 0 is in str but not set
return i;
},
+
+ strcspn: function(pstr, pset) {
+ var str = String_copy(pstr, true);
+ var set = String_copy(pset, true);
+ var i = 0;
+ while (set.indexOf(str[i]) == -1) i++; // Must halt, as 0 is in both
+ return i;
+ },
+
+ strncpy: function(pdest, psrc, num) {
+ var padding = false;
+ for (var i = 0; i < num; i++) {
+ HEAP[pdest+i] = padding ? 0 : HEAP[psrc+i];
+ padding = padding || HEAP[psrc+i] == 0;
+ }
+ },
+
+ strcmp: function(px, py) {
+ var i = 0;
+ while (true) {
+ var x = HEAP[px+i];
+ var y = HEAP[py+i];
+ if (x == y && x == 0) return 0;
+ if (x == 0) return -1;
+ if (y == 0) return 1;
+ if (x == y) {
+ i ++;
+ continue;
+ } else {
+ return x > y ? 1 : -1;
+ }
+ }
+ },
};
// Aliases