FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Mango printf formatting

 
Post new topic   Reply to topic     Forum Index -> Mango
View previous topic :: View next topic  
Author Message
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Wed May 18, 2005 7:08 pm    Post subject: Mango printf formatting Reply with quote

There's been a number of folk who've asked about formatted-output in Mango; along with some who've pointed out the difficulty of formatting with the C++ iostream package (which mango.io supports at the syntactic level).

To get around this, I've been messing with the mango.format package and the mango.io.DisplayWriter module to combine these things together. The upshot is thus ~ DisplayWriter now has print() and println() methods, of which the first argument is a format string, and the rest are arbitrary arguments. There's a few distinctions between this stuff and writef/printf, which I'll note:

- Mango supports all the usual printf formatting characters, except the .* specification. The extended numeric flags such as ?ld do not exist, so the syntax is simpler.

- Mango adds a new formatting character specifically for arrays. For example:
Code:
int[] x = [1, 2, 3, 4, 5, 6, 7];
Stdout.println ("?0@4b ", x);

will emit:
Code:
0001 0010 0011 0100 0101 0110 0111

This works for all single dimension arrays, including strings and so on (you can emit strings as individual formatted chars).

- Mango print routines return a chaining reference, so that one can isolate formatting and targets together:
Code:
Stdout.print ("0x?d", 101)
      .print ("0b?b", 202);


- Only the first argument is considered in terms of '?' formatting tags. All subsequent char[] arguments are treated as text only. This fixes an annoying writef() feature.

- The routines emit char[] instead of char; making them arguably more efficient.

- All formatting is broken out as individually callable routines. You can directly invoke the same Int.format() or Double.format() as called from the print routines.

- Each discrete formatter has a parser, for eating its own dogfood.

- Arguments provided without a format tag are assigned a default one based upon the data type (string, signed, unsigned, etc).


Besides the differences, there's a few other minor things to note:

- All formatting routines are implemented statically.

- There's a struct called Sprintf which provides the same functionality as snprintf(). It's also completely thread-safe.

- Mango.io has a TextFormat class that wraps the format package with an output buffer. This is handy for use with logging, and so on.

- wchar[] and dchar[] are not yet handled. They'll be supported before release, but I'm still vacillating on how to do that properly. Suggestions welcomed.


- All DisplayWriter instances have print() and println(). Stdout and Stderr are just common instances of DisplayWriter.

- This type of formatting is provided as an adjunct to the existing IO styles; you don't have to choose one or the other.


Thought I'd ask for some feedback before bundling it ... comments? Ideas?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Mango All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group