diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-04-29 15:54:45 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-04-29 15:54:45 -0700 |
commit | d94a785aea5d9ef1dda385c6f11d50827adb7c33 (patch) | |
tree | e84d8f73c8922cb06816922beb0897d6eeaecf25 | |
parent | d3cf5d84ecfd5218b6f570860b0a92c7eb1be0eb (diff) |
support h and hh in sscanf, and fix non-i32 case in hex sscanf; fixes #2322
-rw-r--r-- | src/library.js | 23 | ||||
-rw-r--r-- | tests/core/test_sscanf_hex.in | 24 | ||||
-rw-r--r-- | tests/core/test_sscanf_hex.out | 3 |
3 files changed, 38 insertions, 12 deletions
diff --git a/src/library.js b/src/library.js index c2830397..c8c5a0ff 100644 --- a/src/library.js +++ b/src/library.js @@ -1584,7 +1584,6 @@ LibraryManager.library = { return /^[+-]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?/.exec(text); }, - // TODO: Document. _scanString__deps: ['_getFloat'], _scanString: function(format, get, unget, varargs) { if (!__scanString.whiteSpace) { @@ -1726,6 +1725,7 @@ LibraryManager.library = { } var long_ = false; var half = false; + var quarter = false; var longLong = false; if (format[formatIndex] == 'l') { long_ = true; @@ -1737,6 +1737,10 @@ LibraryManager.library = { } else if (format[formatIndex] == 'h') { half = true; formatIndex++; + if (format[formatIndex] == 'h') { + quarter = true; + formatIndex++; + } } var type = format[formatIndex]; formatIndex++; @@ -1795,20 +1799,21 @@ LibraryManager.library = { var text = buffer.join(''); var argPtr = {{{ makeGetValue('varargs', 'argIndex', 'void*') }}}; argIndex += Runtime.getAlignSize('void*', null, true); + var base = 10; switch (type) { + case 'X': case 'x': + base = 16; case 'd': case 'u': case 'i': - if (half) { - {{{ makeSetValue('argPtr', 0, 'parseInt(text, 10)', 'i16') }}}; + if (quarter) { + {{{ makeSetValue('argPtr', 0, 'parseInt(text, base)', 'i8') }}}; + } else if (half) { + {{{ makeSetValue('argPtr', 0, 'parseInt(text, base)', 'i16') }}}; } else if (longLong) { - {{{ makeSetValue('argPtr', 0, 'parseInt(text, 10)', 'i64') }}}; + {{{ makeSetValue('argPtr', 0, 'parseInt(text, base)', 'i64') }}}; } else { - {{{ makeSetValue('argPtr', 0, 'parseInt(text, 10)', 'i32') }}}; + {{{ makeSetValue('argPtr', 0, 'parseInt(text, base)', 'i32') }}}; } break; - case 'X': - case 'x': - {{{ makeSetValue('argPtr', 0, 'parseInt(text, 16)', 'i32') }}}; - break; case 'F': case 'f': case 'E': diff --git a/tests/core/test_sscanf_hex.in b/tests/core/test_sscanf_hex.in index d8175e82..a05eb890 100644 --- a/tests/core/test_sscanf_hex.in +++ b/tests/core/test_sscanf_hex.in @@ -1,7 +1,27 @@ -#include "stdio.h" +#include <stdio.h> +#include <string> +#include <cstdlib> -int main() { +int main() +{ unsigned int a, b; sscanf("0x12AB 12AB", "%x %x", &a, &b); printf("%d %d\n", a, b); + + std::string hexstr("0102037F00FF"); + const char * cstr = hexstr.c_str(); + int len = hexstr.length() / 2; + char * tmp_data = new char[len]; + for(int i = 0; i < len; i++) + { + sscanf(cstr, "%2hhx", &tmp_data[i]); + cstr += 2 * sizeof(char); + } + + for (int j = 0; j < len; j++) + printf("%i, ", tmp_data[j]); + printf("\n"); + delete[] tmp_data; } + + diff --git a/tests/core/test_sscanf_hex.out b/tests/core/test_sscanf_hex.out index ac855044..6e7f66aa 100644 --- a/tests/core/test_sscanf_hex.out +++ b/tests/core/test_sscanf_hex.out @@ -1 +1,2 @@ -4779 4779
\ No newline at end of file +4779 4779 +1, 2, 3, 127, 0, -1, |