diff options
author | Steve Bennett <steveb@workware.net.au> | 2013-11-26 08:27:01 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2013-11-28 07:17:02 +1000 |
commit | 2448a2047e587cae8a1b2f401607b7b4c3108429 (patch) | |
tree | 60640b3016aa811cb4d78c7c3527a787970e169b | |
parent | 388573d26679efc0d7f6df87f834bdd32da64e5d (diff) | |
download | jimtcl-2448a2047e587cae8a1b2f401607b7b4c3108429.zip jimtcl-2448a2047e587cae8a1b2f401607b7b4c3108429.tar.gz jimtcl-2448a2047e587cae8a1b2f401607b7b4c3108429.tar.bz2 |
Add basic support for [format %b]
Binary conversion
Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r-- | jim-format.c | 36 | ||||
-rw-r--r-- | jim_tcl.txt | 5 | ||||
-rw-r--r-- | tests/format.test | 16 |
3 files changed, 56 insertions, 1 deletions
diff --git a/jim-format.c b/jim-format.c index 818eea4..7876d3e 100644 --- a/jim-format.c +++ b/jim-format.c @@ -307,6 +307,42 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, int objc, Jim_ formatted_chars = 1; break; } + case 'b': { + unsigned jim_wide w; + int length; + int i; + int j; + + if (Jim_GetWide(interp, objv[objIndex], (jim_wide *)&w) != JIM_OK) { + goto error; + } + length = sizeof(w) * 8; + + /* XXX: width and precision not yet implemented for binary + * also flags in 'spec', e.g. #, 0, - + */ + + /* Increase the size of the buffer if needed */ + if (num_buffer_size < length + 1) { + num_buffer_size = length + 1; + num_buffer = Jim_Realloc(num_buffer, num_buffer_size); + } + + j = 0; + for (i = length; i > 0; ) { + i--; + if (w & ((unsigned jim_wide)1 << i)) { + num_buffer[j++] = '1'; + } + else if (j || i == 0) { + num_buffer[j++] = '0'; + } + } + num_buffer[j] = 0; + formatted_chars = formatted_bytes = j; + formatted_buf = num_buffer; + break; + } case 'e': case 'E': diff --git a/jim_tcl.txt b/jim_tcl.txt index 0f2af38..a859dfd 100644 --- a/jim_tcl.txt +++ b/jim_tcl.txt @@ -55,6 +55,7 @@ Changes between 0.74 and 0.75 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. `binary`, `pack` and `unpack` now support floating point 2. `file copy` '-force' handles source and target as the same file +3. `format` now supports +%b+ for binary conversion Changes between 0.73 and 0.74 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -2471,7 +2472,9 @@ before passing it to 'sprintf' for formatting. The only unusual conversion is for +%c+; in this case the argument must be a decimal string, which will then be converted to the corresponding -ASCII character value. +ASCII (or UTF-8) character value. + +In addition, Jim Tcl provides basic support for conversion to binary with +%b+. `format` does backslash substitution on its +'formatString'+ argument, so backslash sequences in +'formatString'+ will be handled diff --git a/tests/format.test b/tests/format.test index bb45235..d3d7f20 100644 --- a/tests/format.test +++ b/tests/format.test @@ -492,6 +492,22 @@ for {set i 290} {$i < 400} {incr i} { append b "x" } +test format-16.1 {format %b} { + format %b 0 +} {0} + +test format-16.2 {format %b} { + format %b 1 +} {1} + +test format-16.3 {format %b} { + format %b 0xf +} {1111} + +test format-16.4 {format %b} { + format %b 1234567 +} {100101101011010000111} + # cleanup catch {unset a} catch {unset b} |