Skip to content

Commit c055544

Browse files
committed
Merge pull request #450 from caiiiycuk/openttd
strndump function implementation
2 parents ac7ad44 + edb0920 commit c055544

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ under the licensing terms detailed in LICENSE.
2222
* Brian Anderson <[email protected]>
2323
* Jon Bardin <[email protected]>
2424
* Jukka Jyl�nki <[email protected]>
25+
* Aleksander Guryanov <[email protected]>

src/library.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,6 +4199,24 @@ LibraryManager.library = {
41994199
return newStr;
42004200
},
42014201

4202+
strndup__deps: ['strdup'],
4203+
strndup: function(ptr, size) {
4204+
var len = String_len(ptr);
4205+
4206+
if (size >= len) {
4207+
return _strdup(ptr);
4208+
}
4209+
4210+
if (size < 0) {
4211+
size = 0;
4212+
}
4213+
4214+
var newStr = _malloc(size + 1);
4215+
{{{ makeCopyValues('newStr', 'ptr', 'size', 'null', null, 1) }}};
4216+
{{{ makeSetValue('newStr', 'size', '0', 'i8') }}};
4217+
return newStr;
4218+
},
4219+
42024220
strpbrk: function(ptr1, ptr2) {
42034221
var searchSet = Runtime.set.apply(null, String_copy(ptr2));
42044222
while ({{{ makeGetValue('ptr1', 0, 'i8') }}}) {

tests/runner.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,48 @@ def test_strings(self):
13931393
'''
13941394
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'])
13951395

1396+
def test_strndup(self):
1397+
src = '''
1398+
//---------------
1399+
//- http://pubs.opengroup.org/onlinepubs/9699919799/functions/strndup.html
1400+
//---------------
1401+
1402+
#include <stdio.h>
1403+
#include <stdlib.h>
1404+
#include <string.h>
1405+
1406+
int main(int argc, char **argv) {
1407+
const char* source = "strndup - duplicate a specific number of bytes from a string";
1408+
1409+
char* strdup_val = strndup(source, 0);
1410+
printf("1:%s\\n", strdup_val);
1411+
free(strdup_val);
1412+
1413+
strdup_val = strndup(source, 7);
1414+
printf("2:%s\\n", strdup_val);
1415+
free(strdup_val);
1416+
1417+
strdup_val = strndup(source, 1000);
1418+
printf("3:%s\\n", strdup_val);
1419+
free(strdup_val);
1420+
1421+
strdup_val = strndup(source, 60);
1422+
printf("4:%s\\n", strdup_val);
1423+
free(strdup_val);
1424+
1425+
strdup_val = strndup(source, 19);
1426+
printf("5:%s\\n", strdup_val);
1427+
free(strdup_val);
1428+
1429+
strdup_val = strndup(source, -1);
1430+
printf("6:%s\\n", strdup_val);
1431+
free(strdup_val);
1432+
1433+
return 0;
1434+
}
1435+
'''
1436+
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')
1437+
13961438
def test_errar(self):
13971439
src = r'''
13981440
#include <stdio.h>

0 commit comments

Comments
 (0)