Created
May 3, 2011 08:20
-
-
Save meijeru/952998 to your computer and use it in GitHub Desktop.
FORM function in Red/System (improved version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red/System [ | |
Title: "Form" | |
Purpose: {formats an integer in a c-string, as signed decimal or as hex} | |
Author: "Rudolf W. MEIJER" | |
File: %form-number.reds | |
Version: 0.7 | |
Date: 27-May-2011 | |
Notes: {Improved version, not final} | |
] | |
#include %stringf.reds | |
form-signed: func [ | |
i [integer!] | |
return: [c-string!] | |
/local s [c-string!] r [c-string!] c [integer!] n [logic!] | |
][ | |
if zero? i [return "0"] | |
s: make-string 11 | |
n: negative? i | |
if n [i: negate i] | |
c: 11 | |
while [i <> 0][ | |
s/c: #"0" + (i // 10) | |
i: i / 10 | |
c: c - 1 | |
] | |
if n [s/c: #"-" c: c - 1] | |
r: copy-part s + c 11 - c | |
free s | |
r | |
] | |
form-hex: func [ | |
i [integer!] | |
return: [c-string!] | |
/local s [c-string!] r [c-string!] c [integer!] d [integer!] | |
][ | |
if zero? i [return "0"] | |
s: make-string 8 | |
c: 8 | |
while [i <> 0][ | |
d: i // 16 | |
if d > 9 [d: d + 7] ; 7 = (#"A" - 1) - #"9" | |
s/c: #"0" + d | |
i: i / 16 | |
c: c - 1 | |
] | |
r: copy-part s + c 8 - c | |
free s | |
r | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rudolf, I did a quick patch in Red/System's runtime to cop with printing -2147483648 in commit 3424bcbb in the modified version of your
form-signed
function. If you have any proposition to improve my hardcoded version, let me know.