Mercurial > projects > ldc
comparison tango/tango/text/convert/Sprint.d @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
131:5825d48b27d1 | 132:1700239cab2e |
---|---|
1 /******************************************************************************* | |
2 | |
3 copyright: Copyright (c) 2005 Kris Bell. All rights reserved | |
4 | |
5 license: BSD style: $(LICENSE) | |
6 | |
7 version: Nov 2005: Initial release | |
8 | |
9 author: Kris | |
10 | |
11 *******************************************************************************/ | |
12 | |
13 module tango.text.convert.Sprint; | |
14 | |
15 private import tango.text.convert.Layout; | |
16 | |
17 /****************************************************************************** | |
18 | |
19 Constructs sprintf-style output. This is a replacement for the | |
20 vsprintf() family of functions, and writes its output into a | |
21 lookaside buffer: | |
22 --- | |
23 // create a Sprint instance | |
24 auto sprint = new Sprint!(char); | |
25 | |
26 // write formatted text to a logger | |
27 log.info (sprint ("{} green bottles, sitting on a wall\n", 10)); | |
28 --- | |
29 | |
30 Sprint can be handy when you wish to format text for a Logger | |
31 or similar, since it avoids heap activity during conversion by | |
32 hosting a fixed size conversion buffer. This is important when | |
33 debugging since heap activity can be responsible for behavioral | |
34 changes. One would create a Sprint instance ahead of time, and | |
35 utilize it in conjunction with the logging package. | |
36 | |
37 Please note that the class itself is stateful, and therefore a | |
38 single instance is not shareable across multiple threads. The | |
39 returned content is not .dup'd either, so do that yourself if | |
40 you require a persistent copy. | |
41 | |
42 Note also that Sprint is templated, and can be instantiated for | |
43 wide chars through a Sprint!(dchar) or Sprint!(wchar). The wide | |
44 versions differ in that both the output and the format-string | |
45 are of the target type. Variadic text arguments are transcoded | |
46 appropriately. | |
47 | |
48 See also: tango.text.convert.Layout | |
49 | |
50 ******************************************************************************/ | |
51 | |
52 class Sprint(T) | |
53 { | |
54 protected T[] buffer; | |
55 static Layout!(T) global; | |
56 Layout!(T) layout; | |
57 | |
58 alias format opCall; | |
59 | |
60 /********************************************************************** | |
61 | |
62 Create new Sprint instances with a buffer of the specified | |
63 size | |
64 | |
65 **********************************************************************/ | |
66 | |
67 this (int size = 256) | |
68 { | |
69 // Workaround for bug with static ctors in GDC | |
70 if (global is null) | |
71 global = new Layout!(T); | |
72 this (size, global); | |
73 } | |
74 | |
75 /********************************************************************** | |
76 | |
77 Create new Sprint instances with a buffer of the specified | |
78 size, and the provided formatter. The second argument can be | |
79 used to apply cultural specifics (I18N) to Sprint | |
80 | |
81 **********************************************************************/ | |
82 | |
83 this (int size, Layout!(T) formatter) | |
84 { | |
85 buffer = new T[size]; | |
86 this.layout = formatter; | |
87 } | |
88 | |
89 /********************************************************************** | |
90 | |
91 Layout a set of arguments | |
92 | |
93 **********************************************************************/ | |
94 | |
95 T[] format (T[] fmt, ...) | |
96 { | |
97 return layout.vprint (buffer, fmt, _arguments, _argptr); | |
98 } | |
99 | |
100 /********************************************************************** | |
101 | |
102 Layout a set of arguments | |
103 | |
104 **********************************************************************/ | |
105 | |
106 T[] format (T[] fmt, TypeInfo[] arguments, ArgList argptr) | |
107 { | |
108 return layout.vprint (buffer, fmt, arguments, argptr); | |
109 } | |
110 } | |
111 |