diff options
-rw-r--r-- | src/library.js | 54 | ||||
-rw-r--r-- | tests/runner.py | 73 |
2 files changed, 109 insertions, 18 deletions
diff --git a/src/library.js b/src/library.js index 197d8a48..e1121c2f 100644 --- a/src/library.js +++ b/src/library.js @@ -3840,9 +3840,57 @@ LibraryManager.library = { return 0; }, - // Compiled from newlib; for the original source and licensing, see library_strtok_r.c XXX will not work with typed arrays - strtok_r: function(b,j,f){var a;a=null;var c,e;b=b;var i=b!=0;a:do if(i)a=0;else{b=HEAP[f];if(b!=0){a=0;break a}c=0;a=3;break a}while(0);if(a==0){a:for(;;){e=HEAP[b];b+=1;a=j;var g=e;i=a;a=2;b:for(;;){d=a==5?d:0;a=HEAP[i+d];if(a!=0==0){a=9;break a}var d=d+1;if(g==a)break b;else a=5}a=2}if(a==9)if(g==0)c=HEAP[f]=0;else{c=b+-1;a:for(;;){e=HEAP[b];b+=1;a=j;g=e;d=a;a=10;b:for(;;){h=a==13?h:0;a=HEAP[d+h];if(a==g!=0)break a;var h=h+1;if(a!=0)a=13;else break b}}if(e==0)b=0;else HEAP[b+-1]=0; HEAP[f]=b;c=c}else if(a==7){HEAP[f]=b;HEAP[b+-1]=0;c=b+-1}}return c}, - // TODO: Compile strtok() from source. + // Translated from newlib; for the original source and licensing, see library_strtok_r.c + strtok_r: function(s, delim, lasts) { + var skip_leading_delim = 1; + var spanp; + var c, sc; + var tok; + + + if (s == 0 && (s = getValue(lasts, 'i8*')) == 0) { + return 0; + } + + cont: while (1) { + c = getValue(s++, 'i8'); + for (spanp = delim; (sc = getValue(spanp++, 'i8')) != 0;) { + if (c == sc) { + if (skip_leading_delim) { + continue cont; + } else { + setValue(lasts, s, 'i8*'); + setValue(s - 1, 0, 'i8'); + return s - 1; + } + } + } + break; + } + + if (c == 0) { + setValue(lasts, 0, 'i8*'); + return 0; + } + tok = s - 1; + + for (;;) { + c = getValue(s++, 'i8'); + spanp = delim; + do { + if ((sc = getValue(spanp++, 'i8')) == c) { + if (c == 0) { + s = 0; + } else { + setValue(s - 1, 0, 'i8'); + } + setValue(lasts, s, 'i8*'); + return tok; + } + } while (sc != 0); + } + abort('strtok_r error!'); + }, strerror_r__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo'], strerror_r: function(errnum, strerrbuf, buflen) { diff --git a/tests/runner.py b/tests/runner.py index 2579f67c..6ae98c16 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -2742,6 +2742,63 @@ if 'benchmark' not in str(sys.argv): self.do_run(src, re.sub(r'\n\s+', '\n', expected)) + def test_strtok(self): + src = r''' + #include<stdio.h> + #include<string.h> + + int main() { + char test[80], blah[80]; + char *sep = "\\/:;=-"; + char *word, *phrase, *brkt, *brkb; + + strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); + + for (word = strtok_r(test, sep, &brkt); word; word = strtok_r(NULL, sep, &brkt)) { + strcpy(blah, "blah:blat:blab:blag"); + for (phrase = strtok_r(blah, sep, &brkb); phrase; phrase = strtok_r(NULL, sep, &brkb)) { + printf("at %s:%s\n", word, phrase); + } + } + return 1; + } + ''' + + expected = '''at This:blah +at This:blat +at This:blab +at This:blag +at is.a:blah +at is.a:blat +at is.a:blab +at is.a:blag +at test:blah +at test:blat +at test:blab +at test:blag +at of:blah +at of:blat +at of:blab +at of:blag +at the:blah +at the:blat +at the:blab +at the:blag +at string:blah +at string:blat +at string:blab +at string:blag +at tokenizer:blah +at tokenizer:blat +at tokenizer:blab +at tokenizer:blag +at function.:blah +at function.:blat +at function.:blab +at function.:blag +''' + self.do_run(src, expected) + def test_parseInt(self): Settings.I64_MODE = 1 # Necessary to prevent i64s being truncated into i32s, but we do still get doubling # FIXME: The output here is wrong, due to double rounding of i64s! @@ -3680,22 +3737,8 @@ if 'benchmark' not in str(sys.argv): Settings.SAFE_HEAP = 0 # Has variable object - Settings.CHECK_OVERFLOWS = 0 - + Settings.CORRECT_OVERFLOWS = 1 Settings.CORRECT_SIGNS = 1 - Settings.CORRECT_SIGNS_LINES = ['parseargs.cc:171', 'BuiltinFont.cc:64', 'NameToCharCode.cc:115', 'GooHash.cc:368', - 'Stream.h:469', 'PDFDoc.cc:1064', 'Lexer.cc:201', 'Splash.cc:1130', 'XRef.cc:997', - 'vector:714', 'Lexer.cc:259', 'Splash.cc:438', 'Splash.cc:532', 'GfxFont.cc:1152', - 'Gfx.cc:3838', 'Splash.cc:3162', 'Splash.cc:3163', 'Splash.cc:3164', 'Splash.cc:3153', - 'Splash.cc:3159', 'SplashBitmap.cc:80', 'SplashBitmap.cc:81', 'SplashBitmap.cc:82', - 'Splash.cc:809', 'Splash.cc:805', 'GooHash.cc:379', - # FreeType - 't1load.c:1850', 'psconv.c:104', 'psconv.c:185', 'psconv.c:366', 'psconv.c:399', - 'ftcalc.c:308', 't1parse.c:405', 'psconv.c:431', 'ftcalc.c:555', 't1objs.c:458', - 't1decode.c:595', 't1decode.c:606', 'pstables.h:4048', 'pstables.h:4055', 'pstables.h:4066', - 'pshglob.c:166', 'ftobjs.c:2548', 'ftgrays.c:1190', 'psmodule.c:116', 'psmodule.c:119', - 'psobjs.c:195', 'pshglob.c:165', 'ttload.c:694', 'ttmtx.c:195', 'sfobjs.c:957', - 'sfobjs.c:958', 'ftstream.c:369', 'ftstream.c:372', 'ttobjs.c:1007'] # And many more... Building.COMPILER_TEST_OPTS += [ '-I' + path_from_root('tests', 'libcxx', 'include'), # Avoid libstdc++ linking issue, see libcxx test |