Skip to content

strndump function implementation #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ under the licensing terms detailed in LICENSE.
* Brian Anderson <[email protected]>
* Jon Bardin <[email protected]>
* Jukka Jyl�nki <[email protected]>
* Aleksander Guryanov <[email protected]>
18 changes: 18 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') }}}) {
Expand Down
42 changes: 42 additions & 0 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,48 @@ def test_strings(self):
'''
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>
Expand Down