diff options
-rw-r--r-- | src/library.js | 43 | ||||
-rw-r--r-- | src/preamble.js | 38 |
2 files changed, 18 insertions, 63 deletions
diff --git a/src/library.js b/src/library.js index b74b9744..9ebe926a 100644 --- a/src/library.js +++ b/src/library.js @@ -2710,34 +2710,19 @@ LibraryManager.library = { }); } else if (next == 's'.charCodeAt(0)) { // String. - var arg = getNextArg('i8*'); - var copiedString; - if (arg) { - copiedString = String_copy(arg); - if (precisionSet && copiedString.length > precision) { -#if USE_TYPED_ARRAYS == 2 - copiedString = copiedString.subarray(0, precision); -#else - copiedString = copiedString.slice(0, precision); -#endif - } - } else { - copiedString = intArrayFromString('(null)', true); - } + var arg = getNextArg('i8*') || 0; // 0 holds '(null)' + var argLength = String_len(arg); + if (precisionSet) argLength = Math.min(String_len(arg), precision); if (!flagLeftAlign) { - while (copiedString.length < width--) { + while (argLength < width--) { ret.push(' '.charCodeAt(0)); } } -#if USE_TYPED_ARRAYS == 2 - for (var i = 0; i < copiedString.length; i++) { - ret.push(copiedString[i]); + for (var i = 0; i < argLength; i++) { + ret.push({{{ makeGetValue('arg++', 0, 'i8', null, true) }}}); } -#else - ret = ret.concat(copiedString); -#endif if (flagLeftAlign) { - while (copiedString.length < width--) { + while (argLength < width--) { ret.push(' '.charCodeAt(0)); } } @@ -4230,9 +4215,17 @@ LibraryManager.library = { }, strpbrk: function(ptr1, ptr2) { - var searchSet = Runtime.set.apply(null, String_copy(ptr2)); - while ({{{ makeGetValue('ptr1', 0, 'i8') }}}) { - if ({{{ makeGetValue('ptr1', 0, 'i8') }}} in searchSet) return ptr1; + var curr; + var searchSet = {}; + while (1) { + var curr = {{{ makeGetValue('ptr2++', 0, 'i8') }}}; + if (!curr) break; + searchSet[curr] = 1; + } + while (1) { + curr = {{{ makeGetValue('ptr1', 0, 'i8') }}}; + if (!curr) break; + if (curr in searchSet) return ptr1; ptr1++; } return 0; diff --git a/src/preamble.js b/src/preamble.js index fde9e2cf..541b4bb5 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -721,33 +721,6 @@ function exitRuntime() { CorrectionsMonitor.print(); } - -// Copies a list of num items on the HEAP into a -// a JavaScript array of numbers (normal array in TA0 or TA1, -// typed array in TA2) -function Array_copy(ptr, num) { -#if USE_TYPED_ARRAYS == 1 - // TODO: In the SAFE_HEAP case, do some reading here, for debugging purposes - currently this is an 'unnoticed read'. - return Array.prototype.slice.call(IHEAP.subarray(ptr, ptr+num)); // Make a normal array out of the typed 'view' - // Consider making a typed array here, for speed? -#else -#if USE_TYPED_ARRAYS == 2 - var end = ptr+num; - if (end <= HEAPU8.length) { - return new Uint8Array(HEAPU8.subarray(ptr, end)); - } else { - // we fill with zeros after the end of the array - var ret = new Uint8Array(end - ptr); - ret.set(HEAPU8.subarray(ptr, end)); - return ret; - } -#else - return HEAP.slice(ptr, ptr+num); -#endif -#endif -} -Module['Array_copy'] = Array_copy; - function String_len(ptr) { var i = 0; while ({{{ makeGetValue('ptr', 'i', 'i8') }}}) i++; // Note: should be |!= 0|, technically. But this helps catch bugs with undefineds @@ -755,17 +728,6 @@ function String_len(ptr) { } Module['String_len'] = String_len; -// Copies a C-style string, terminated by a zero, from the HEAP into -// a normal JavaScript array of numbers -function String_copy(ptr, addZero) { - var len = String_len(ptr); - if (addZero) len++; - var ret = Array_copy(ptr, len); - if (addZero) ret[len-1] = 0; - return ret; -} -Module['String_copy'] = String_copy; - // Tools // This processes a JS string into a C-line array of numbers, 0-terminated. |