aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-05-30 11:19:36 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-05-30 11:19:36 -0700
commitc0555446e5f3e4e8cd1b9210b9ed447c0197c386 (patch)
tree8a2c7bc5517ac587967757bbd92542ee3094c72f
parentac7ad44cbabb2cac0425ce5273a1e0f636f03a78 (diff)
parentedb0920e82300069f8b0ccdc405463f2e44079d8 (diff)
Merge pull request #450 from caiiiycuk/openttd
strndump function implementation
-rw-r--r--AUTHORS1
-rw-r--r--src/library.js18
-rwxr-xr-xtests/runner.py42
3 files changed, 61 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index 1dd6039a..3d189c9f 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -22,3 +22,4 @@ under the licensing terms detailed in LICENSE.
* Brian Anderson <banderson@mozilla.com>
* Jon Bardin <diclophis@gmail.com>
* Jukka Jylänki <jujjyl@gmail.com>
+* Aleksander Guryanov <caiiiycuk@gmail.com>
diff --git a/src/library.js b/src/library.js
index f406f02a..8d68251f 100644
--- a/src/library.js
+++ b/src/library.js
@@ -4199,6 +4199,24 @@ LibraryManager.library = {
return newStr;
},
+ strndup__deps: ['strdup'],
+ strndup: function(ptr, size) {
+ var len = String_len(ptr);
+
+ if (size >= len) {
+ return _strdup(ptr);
+ }
+
+ if (size < 0) {
+ size = 0;
+ }
+
+ var newStr = _malloc(size + 1);
+ {{{ makeCopyValues('newStr', 'ptr', 'size', 'null', null, 1) }}};
+ {{{ makeSetValue('newStr', 'size', '0', 'i8') }}};
+ return newStr;
+ },
+
strpbrk: function(ptr1, ptr2) {
var searchSet = Runtime.set.apply(null, String_copy(ptr2));
while ({{{ makeGetValue('ptr1', 0, 'i8') }}}) {
diff --git a/tests/runner.py b/tests/runner.py
index 794b6dd8..9c29dc96 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1393,6 +1393,48 @@ m_divisor is 1091269979
'''
self.do_run(src, '4:10,177,543,def\n4\nwowie\ntoo\n76\n5\n(null)\n/* a comment */\n// another\ntest\n', ['wowie', 'too', '74'])
+ def test_strndup(self):
+ src = '''
+ //---------------
+ //- http://pubs.opengroup.org/onlinepubs/9699919799/functions/strndup.html
+ //---------------
+
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+
+ int main(int argc, char **argv) {
+ const char* source = "strndup - duplicate a specific number of bytes from a string";
+
+ char* strdup_val = strndup(source, 0);
+ printf("1:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, 7);
+ printf("2:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, 1000);
+ printf("3:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, 60);
+ printf("4:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, 19);
+ printf("5:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, -1);
+ printf("6:%s\\n", strdup_val);
+ free(strdup_val);
+
+ return 0;
+ }
+ '''
+ self.do_run(src, '1:\n2:strndup\n3:strndup - duplicate a specific number of bytes from a string\n4:strndup - duplicate a specific number of bytes from a string\n5:strndup - duplicate\n6:\n')
+
def test_errar(self):
src = r'''
#include <stdio.h>