diff options
author | Aleksander Guryanov <caiiiycuk@gmail.com> | 2012-05-28 21:35:04 +0700 |
---|---|---|
committer | Aleksander Guryanov <caiiiycuk@gmail.com> | 2012-05-28 21:35:04 +0700 |
commit | 2134a33a61d9c5dd24b021ced4af38f29b6de06b (patch) | |
tree | dd23f47c459a3d7d212e37bd3016109b0476be7e | |
parent | 712bc9041e3470920d0db6a05e5b0af85d1b78e2 (diff) |
Add implementation of strndup function and test for it
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | src/library.js | 14 | ||||
-rwxr-xr-x | tests/runner.py | 34 |
3 files changed, 49 insertions, 0 deletions
@@ -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..e49ea6da 100644 --- a/src/library.js +++ b/src/library.js @@ -4199,6 +4199,20 @@ LibraryManager.library = { return newStr; }, + strndup__deps: ['strdup'], + strndup: function(ptr, size) { + var len = String_len(ptr); + + if (size <= 0 || size >= len) { + return _strdup(ptr); + } + + var newStr = _malloc(0 + 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 d6731ed5..4064af15 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -1393,6 +1393,40 @@ 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, 7); + printf("%s\\n", strdup_val); + free(strdup_val); + + strdup_val = strndup(source, 1000); + printf("%s\\n", strdup_val); + free(strdup_val); + + strdup_val = strndup(source, 60); + printf("%s\\n", strdup_val); + free(strdup_val); + + strdup_val = strndup(source, 19); + printf("%s\\n", strdup_val); + free(strdup_val); + + return 0; + } + ''' + self.do_run(src, 'strndup\nstrndup - duplicate a specific number of bytes from a string\nstrndup - duplicate a specific number of bytes from a string\nstrndup - duplicate\n') + def test_errar(self): src = r''' #include <stdio.h> |