-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of mp_printf #550
base: develop
Are you sure you want to change the base?
Conversation
Ah, it is the c89 mode that gives me trouble! |
Nice! How about adding an option to have a stripped down version that can hook into glibc's |
Yes, that was exactly what I tried first. I'm quite lazy, y'know ;-)
The parsed, how to call it, mark-up? is in the struct But I can complete my example ( |
@sjaeckel Seems to work, so where to go?
|
The standarization of ; Extended and simplified Posix file format to support big integers
; This ABNF has been extended to use curly braces to mark a kind of
; a set. The example
;
; {a, b, c}
;
; translates into all subsets and their permutations;
;
; ()/a/b/(a b)/(b a)/(a c)/(c a)/(b c)/(c b)/(a b c)/(b a c)/
; (c a b)/(a c b)/(b c a)/(c b a)
;
; PRINTABLE = %x20-7E
HEXDIGIT = DIGIT / ( %x41-46 / %x61-66 ) ; 0-9 / (A-F/a-f)
HASH = %x23 ; '#'
PERIOD = %x2E ; '.' (locale dependent if used as a decimal point)
FORMSTART = %x25 ; '%'
MINUS = %x2D ; '-'
PLUS = %x2B ; '+'
ZERO = %x30 ; '0' (for padding)
QUOT = %x27 ; "'" single quote
; The compiler will, in most cases, do the un-escaping.
ESCSIGN = %x5C ; '\'
ESCCHAR = %s'a' / %s'b' / %s'f' / %s'n' / %s'r'
ESCCHAR =/ %s't' / %s 'v'
ESCCHAR =/ DQUOTE / ESCSIGN ; '\"' and '\\'
ESCAPE = ESCSIGN ESCCHAR
EXCOCTAL = ESCSIGN ZERO 0*3DIGIT ; no digits -> zero
EXCHEX = ESCSIGN %i'x' 0*2HEXDIGIT ; extension. no digits -> zero
FLOATSPEC = %s'a' / %s'A' / %s'd' / %s'e' / %s'E' ; Floating point conversion specifiers
FLOATSPEC =/ %s'f' / %s'F' / %s'g' / %s'G' ; Floating point conversion specifiers
INTSPEC =/ %s'd' / %s'i' / %s'o' / %s'u' / %s'x' / %s'X' ; Integer conversion specifiers (d == i)
INTSPEC =/ %s'b' / %s'B' ; changed from the Posix definition to binary representation
INTSPEC =/ %x40 ; ('@' sign) extension for base64 representation
CHARSPEC = %s'c' ; one byte (integer)
STRINGSPEC = %s's' ; nul-terminated string
PTRSPEC = %s'p' ; value of a pointer
LENSPEC = %s'n' ; puts number of printed characters up to FORMSTART
; in the argument g9iven
ERRSPEC = %s'm' ; Glibc extension to print output of strerror(errno)
; without argument. (reads global variable errno)
INTMOD = %s'l' / %s"ll" / %s'h' / %s"hh" ; 'l' also extends "char" to a "wint_t", char* to wchar_t*
INTMOD =/ %s'j' / %s'z' / %s't' ; (u)intmax, (s)size_t, (signed?) ptrdiff
FLOATMOD = %s'L' ; "long double". Extended "double".
; Most likely binary80, but can be binary128, too.
; Glibc's extension 'll == L' is not supported
; The non-standard extensions "q == ll", "C == lc", "S == ls" are not supported
; Big integer extensions
BIGINTMOD = %s'Z' ; The big integer itself (also a very old synonym for 'z')
BIGINTMOD =/ %s'M' ; A single limb
BIGINTMOD =/ %s'N' ; The raw limb-array
; Formatting
; The non-standard extension 'I' (upper case i) for locale dependent digits is not supported
; Flags
; Without the new "set" formatting {...} it would be this abomination
;FRMTFLAGS = ([(SP / PLUS)] [(MINUS / ZERO)] [HASH] [QUOT] )
;FRMTFLAGS =/ ([(MINUS / ZERO)] [(SP / PLUS)] [HASH] [QUOT] )
;FRMTFLAGS =/ ([HASH] [(SP / PLUS)] [(MINUS / ZERO)] [QUOT] )
;FRMTFLAGS =/ ([(SP / PLUS)] [HASH] [(MINUS / ZERO)] [QUOT] )
;FRMTFLAGS =/ ([(MINUS / ZERO)] [HASH] [(SP / PLUS)] [QUOT] )
;FRMTFLAGS =/ ([HASH] [(MINUS / ZERO)] [(SP / PLUS)] [QUOT] )
;FRMTFLAGS =/ ([HASH] [(MINUS / ZERO)] [QUOT] [(SP / PLUS)])
;FRMTFLAGS =/ ([(MINUS / ZERO)] [HASH] [QUOT] [(SP / PLUS)])
;FRMTFLAGS =/ ([QUOT] [HASH] [(MINUS / ZERO)] [(SP / PLUS)])
;FRMTFLAGS =/ ([HASH] [QUOT] [(MINUS / ZERO)] [(SP / PLUS)])
;FRMTFLAGS =/ ([(MINUS / ZERO)] [QUOT] [HASH] [(SP / PLUS)])
;FRMTFLAGS =/ ([QUOT] [(MINUS / ZERO)] [HASH] [(SP / PLUS)])
;FRMTFLAGS =/ ([QUOT] [(SP / PLUS)] [HASH] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([(SP / PLUS)] [QUOT] [HASH] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([HASH] [QUOT] [(SP / PLUS)] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([QUOT] [HASH] [(SP / PLUS)] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([(SP / PLUS)] [HASH] [QUOT] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([HASH] [(SP / PLUS)] [QUOT] [(MINUS / ZERO)])
;FRMTFLAGS =/ ([(MINUS / ZERO)] [(SP / PLUS)] [QUOT] [HASH])
;FRMTFLAGS =/ ([(SP / PLUS)] [(MINUS / ZERO)] [QUOT] [HASH])
;FRMTFLAGS =/ ([QUOT] [(MINUS / ZERO)] [(SP / PLUS)] [HASH])
;FRMTFLAGS =/ ([(MINUS / ZERO)] [QUOT] [(SP / PLUS)] [HASH])
;FRMTFLAGS =/ ([(SP / PLUS)] [QUOT] [(MINUS / ZERO)] [HASH])
;FRMTFLAGS =/ ([QUOT] [(SP / PLUS)] [(MINUS / ZERO)] [HASH])
FRMTFLAGS = { (SP / PLUS), (MINUS / ZERO), HASH, QUOT }
; Width
FRMTWIDTH = 1*DIGIT ; length is not arbitrary but restricted (min. 16 bit)
; Precision
FRMTPREC = PERIOD *DIGIT ; no digits -> zero
; Specifier
FRMTINTSPEC = [INTMOD / BIGINTMOD] INTSPEC
FRMTFLTSPEC = [FLOATMOD] FLOATSPEC
FRMTCHRSPEC = [%s'l'] CHARSPEC
FRMTSTRSPEC = [%s'l'] STRINGSPEC
FRMTPTRSPEC = [INTMOD] PTRSPEC
FRMTLENSPEC = LENSPEC
FRMTERRSPEC = ERRSPEC
FRMTSPEC = FRMTINTSPEC / FRMTFLTSPEC / FRMTCHRSPEC / FRMTSTRSPEC / FRMTPTRSPEC / FRMTLENSPEC / FRMTERRSPEC
; All together
FORMAT = (FORMSTART FORMSTART) ; Print a '%'
FORMAT =/ (FORMSTART [FRMTFLAG] [FRMTWIDTH] [FRMTPREC] FRMTSPEC) ; Min. format e.g.: "%d", "%u", "%x" *pooh* |
It might be my purely subjective perception but the Ci seems to have grown a slight lack of stability lastly, doesn't it? |
Implementation of a
mp_(f)printf
with the additional type modifiersZ
(signed big integer),M
(unsigned limb), andN
a printout ofbig_integer->dp
, all formatable (to some extent) as inprintf(3)
and with a binary representation, too. See documentation and source for details.Uses
fprintf(3)
for output and some of the formatting so it is bracketed inside#ifndef MP_NO_FILE
.It is rather large (6k - 12k stripped, depending on
MP_xxBIT
) but the folks over at GMP were right: you basically have to write a completeprintf
. Well, almost complete.