Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 904 lines (764 sloc) 31.289 kB
e842151 Inline docs update
mikescott authored
1 /*
ac1f42c [t/docs] fixup more function docs (mostly signature fixups)
coke authored
2 Copyright (C) 2001-2009, Parrot Foundation.
e842151 Inline docs update
mikescott authored
3 $Id$
4
5 =head1 NAME
6
7 src/spf_render.c - Parrot sprintf
8
9 =head1 DESCRIPTION
10
11 Implements the main function that drives the C<Parrot_sprintf> family
12 and its utility functions.
13
14 =head2 Utility Functions
15
d2ed488 [core] Replaced FUNCDOC with C<=item>s. Also tidied pod so that html…
paultcochrane authored
16 =over 4
17
18 =cut
19
e842151 Inline docs update
mikescott authored
20 */
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
21
22 #define IN_SPF_SYSTEM
23
24 #include "parrot/parrot.h"
95ec1cf fix a couple of segfaults; preliminary tailcallmethod test
leo authored
25 #include "spf_render.str"
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
26
faf5610 moved more spf_render.c stuff local to it
petdance authored
27 typedef enum {
28 PHASE_FLAGS = 0,
29 PHASE_WIDTH,
30 PHASE_PREC,
31 PHASE_TYPE,
32 PHASE_TERM,
33 PHASE_DONE
34 } PHASE;
35
36 typedef struct SpfInfo_tag {
31f3385 Moved SpfInfo internal to the one file that needs it.
petdance authored
37 UINTVAL width;
38 UINTVAL prec;
faf5610 moved more spf_render.c stuff local to it
petdance authored
39 INTVAL flags;
40 INTVAL type;
41 PHASE phase;
31f3385 Moved SpfInfo internal to the one file that needs it.
petdance authored
42 } SpfInfo;
43
ed706b6 More assert() cleanup
petdance authored
44 enum {
faf5610 moved more spf_render.c stuff local to it
petdance authored
45 FLAG_MINUS = (1<<0),
46 FLAG_PLUS = (1<<1),
47 FLAG_ZERO = (1<<2),
48 FLAG_SPACE = (1<<3),
49 FLAG_SHARP = (1<<4),
50 FLAG_WIDTH = (1<<5),
51 FLAG_PREC = (1<<6)
52 };
53
a9385ae Changed HEADERIZER TARGET to HEADERIZER HFILE
petdance authored
54 /* HEADERIZER HFILE: include/parrot/misc.h */
c34df07 headerized spf_render.c
petdance authored
55
4df3d8b All the rest of the source files are now headerized
petdance authored
56 /* HEADERIZER BEGIN: static */
253d20a Added a "DO NOT MODIFY HERE" message at the top & bottom of headerize…
petdance authored
57 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
4df3d8b All the rest of the source files are now headerized
petdance authored
58
de8f69d lots of variable localization
petdance authored
59 static void gen_sprintf_call(
0d9cfb9 headerized src/spf_render.c
petdance authored
60 ARGOUT(char *out),
2a53cc4 Converting to ARGMOD instead of ARGINOUT
petdance authored
61 ARGMOD(SpfInfo *info),
f1fdeab [core] Adjusted parenthesis spacing according to coding standards. I'll
paultcochrane authored
62 int thingy)
de8f69d lots of variable localization
petdance authored
63 __attribute__nonnull__(1)
b9f39d4 Added support for Splint's @modifies keyword via FUNC_MODIFIES() macro
petdance authored
64 __attribute__nonnull__(2)
420e604 ARGOUT has to have FUNC_MODIFIES flag set.
petdance authored
65 FUNC_MODIFIES(*out)
b9f39d4 Added support for Splint's @modifies keyword via FUNC_MODIFIES() macro
petdance authored
66 FUNC_MODIFIES(*info);
4df3d8b All the rest of the source files are now headerized
petdance authored
67
74390c8 updated annotation
petdance authored
68 PARROT_WARN_UNUSED_RESULT
f9badd3 tsq.c and sub.c are now headerizer-clean
petdance authored
69 PARROT_CANNOT_RETURN_NULL
f1fdeab [core] Adjusted parenthesis spacing according to coding standards. I'll
paultcochrane authored
70 static STRING * handle_flags(PARROT_INTERP,
c118f1b headerized all of src/stm/backend.c
petdance authored
71 ARGIN(const SpfInfo *info),
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
72 ARGIN(STRING *str),
4df3d8b All the rest of the source files are now headerized
petdance authored
73 INTVAL is_int_type,
c118f1b headerized all of src/stm/backend.c
petdance authored
74 ARGIN_NULLOK(STRING* prefix))
de8f69d lots of variable localization
petdance authored
75 __attribute__nonnull__(1)
31f3385 Moved SpfInfo internal to the one file that needs it.
petdance authored
76 __attribute__nonnull__(2)
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
77 __attribute__nonnull__(3);
4df3d8b All the rest of the source files are now headerized
petdance authored
78
f9badd3 tsq.c and sub.c are now headerizer-clean
petdance authored
79 PARROT_CANNOT_RETURN_NULL
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
80 static STRING* str_concat_w_flags(PARROT_INTERP,
420e604 ARGOUT has to have FUNC_MODIFIES flag set.
petdance authored
81 ARGOUT(STRING *dest),
0d9cfb9 headerized src/spf_render.c
petdance authored
82 ARGIN(const SpfInfo *info),
857b305 modifying some splint macros
petdance authored
83 ARGMOD(STRING *src),
0d9cfb9 headerized src/spf_render.c
petdance authored
84 ARGIN_NULLOK(STRING *prefix))
de8f69d lots of variable localization
petdance authored
85 __attribute__nonnull__(1)
86 __attribute__nonnull__(2)
31f3385 Moved SpfInfo internal to the one file that needs it.
petdance authored
87 __attribute__nonnull__(3)
420e604 ARGOUT has to have FUNC_MODIFIES flag set.
petdance authored
88 __attribute__nonnull__(4)
857b305 modifying some splint macros
petdance authored
89 FUNC_MODIFIES(*dest)
90 FUNC_MODIFIES(*src);
4df3d8b All the rest of the source files are now headerized
petdance authored
91
8766dd0 [cage] use operator comma in ASSERT_ARGS to avoid problems with short…
NotFound authored
92 #define ASSERT_ARGS_gen_sprintf_call __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
9019630 [core] Merge from assert_args branch, with modifications.
infinoid authored
93 PARROT_ASSERT_ARG(out) \
8766dd0 [cage] use operator comma in ASSERT_ARGS to avoid problems with short…
NotFound authored
94 , PARROT_ASSERT_ARG(info))
95 #define ASSERT_ARGS_handle_flags __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
9019630 [core] Merge from assert_args branch, with modifications.
infinoid authored
96 PARROT_ASSERT_ARG(interp) \
8766dd0 [cage] use operator comma in ASSERT_ARGS to avoid problems with short…
NotFound authored
97 , PARROT_ASSERT_ARG(info) \
98 , PARROT_ASSERT_ARG(str))
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
99 #define ASSERT_ARGS_str_concat_w_flags __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
9019630 [core] Merge from assert_args branch, with modifications.
infinoid authored
100 PARROT_ASSERT_ARG(interp) \
8766dd0 [cage] use operator comma in ASSERT_ARGS to avoid problems with short…
NotFound authored
101 , PARROT_ASSERT_ARG(dest) \
102 , PARROT_ASSERT_ARG(info) \
103 , PARROT_ASSERT_ARG(src))
253d20a Added a "DO NOT MODIFY HERE" message at the top & bottom of headerize…
petdance authored
104 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
4df3d8b All the rest of the source files are now headerized
petdance authored
105 /* HEADERIZER END: static */
106
107
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
108 /* Per Dan's orders, we will not use sprintf if snprintf isn't
109 * around for us.
110 */
978ee60 replace _MSC_VER by _WIN32
fperrad authored
111 #ifdef _MSC_VER
f17920e Indenting fixes, etc.
josh authored
112 # define snprintf _snprintf
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
113 #endif
114
e842151 Inline docs update
mikescott authored
115 /*
116
205d0b5 [cage] run 'make headerizer'
coke authored
117 =item C<static STRING * handle_flags(PARROT_INTERP, const SpfInfo *info, STRING
118 *str, INTVAL is_int_type, STRING* prefix)>
e842151 Inline docs update
mikescott authored
119
120 Handles C<+>, C<->, C<0>, C<#>, space, width, and prec.
121
d2ed488 [core] Replaced FUNCDOC with C<=item>s. Also tidied pod so that html…
paultcochrane authored
122 =cut
123
e842151 Inline docs update
mikescott authored
124 */
125
74390c8 updated annotation
petdance authored
126 PARROT_WARN_UNUSED_RESULT
f9badd3 tsq.c and sub.c are now headerizer-clean
petdance authored
127 PARROT_CANNOT_RETURN_NULL
4e2d997 Parrot_destroy
leo authored
128 static STRING *
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
129 handle_flags(PARROT_INTERP, ARGIN(const SpfInfo *info), ARGIN(STRING *str),
c118f1b headerized all of src/stm/backend.c
petdance authored
130 INTVAL is_int_type, ARGIN_NULLOK(STRING* prefix))
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
131 {
b5aa5dd [cage] Attempt to work around MSVC warnings related to ASSERT_ARGS().
infinoid authored
132 ASSERT_ARGS(handle_flags)
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
133 UINTVAL len = Parrot_str_byte_length(interp, str);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
134
135 if (is_int_type) {
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
136 if (info->flags & FLAG_PREC && info->prec == 0 &&
137 len == 1 &&
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
138 string_ord(interp, str, 0) == '0') {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
139 str = Parrot_str_chopn(interp, str, len);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
140 len = 0;
141 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
142 /* +, space */
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
143 if (!len || string_ord(interp, str, 0) != '-') {
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
144 if (info->flags & FLAG_PLUS) {
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
145 STRING * const cs = CONST_STRING(interp, "+");
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
146 str = Parrot_str_concat(interp, cs, str);
f53b004 manual optimization for old compiler
jimmy authored
147 ++len;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
148 }
149 else if (info->flags & FLAG_SPACE) {
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
150 STRING * const cs = CONST_STRING(interp, " ");
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
151 str = Parrot_str_concat(interp, cs, str);
f53b004 manual optimization for old compiler
jimmy authored
152 ++len;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
153 }
154 }
95ec1cf fix a couple of segfaults; preliminary tailcallmethod test
leo authored
155
156 /* # 0x ... */
157 if ((info->flags & FLAG_SHARP) && prefix) {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
158 str = Parrot_str_concat(interp, prefix, str);
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
159 len += Parrot_str_byte_length(interp, prefix);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
160 }
4e2d997 Parrot_destroy
leo authored
161 /* XXX sharp + fill ??? */
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
162 }
163 else {
164 /* string precision */
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
165 if (info->flags & FLAG_PREC && info->prec == 0) {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
166 str = Parrot_str_chopn(interp, str, len);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
167 len = 0;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
168 }
3731bcd [cage] use else if instead of nested if
cotto authored
169 else if (info->flags & FLAG_PREC && info->prec < len) {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
170 str = Parrot_str_chopn(interp, str, -(INTVAL)(info->prec));
3731bcd [cage] use else if instead of nested if
cotto authored
171 len = info->prec;
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
172 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
173 }
174
e232e78 #18139 sprintf
leo authored
175 if ((info->flags & FLAG_WIDTH) && info->width > len) {
de8f69d lots of variable localization
petdance authored
176 STRING * const filler =
177 ((info->flags & FLAG_ZERO) && !(info->flags & FLAG_MINUS))
178 ? CONST_STRING(interp, "0")
179 : CONST_STRING(interp, " ");
0158beb Implement Parrot_str_repeat and replace usages of string_repeat, TT #…
julianalbo authored
180 STRING * const fill = Parrot_str_repeat(interp, filler, info->width - len);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
181
182 if (info->flags & FLAG_MINUS) { /* left-align */
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
183 str = Parrot_str_concat(interp, str, fill);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
184 }
185 else { /* right-align */
56f77ca #18098
leo authored
186 /* signed and zero padded */
9c36e49 bugfix WRT %%
leo authored
187 if (info->flags & FLAG_ZERO
f084f72 [core] Adding spaces behind commas where appropriate (as per coding
paultcochrane authored
188 && (string_ord(interp, str, 0) == '-' ||
189 string_ord(interp, str, 0) == '+')) {
cfd637d use NULL not 0
petdance authored
190 STRING *temp = NULL;
346aa30 [core] Removed unused result warnings.
paultcochrane authored
191 STRING *ignored;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
192 temp = Parrot_str_substr(interp, str, 1, len-1);
193 str = Parrot_str_chopn(interp, str, -1);
194 str = Parrot_str_concat(interp, str, fill);
195 str = Parrot_str_concat(interp, str, temp);
56f77ca #18098
leo authored
196 }
197 else {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
198 str = Parrot_str_concat(interp, fill, str);
56f77ca #18098
leo authored
199 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
200 }
201 }
4e2d997 Parrot_destroy
leo authored
202 return str;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
203 }
204
3ec76a6 [core] More function documentation
paultcochrane authored
205 /*
206
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
207 =item C<static STRING* str_concat_w_flags(PARROT_INTERP, STRING *dest, const
205d0b5 [cage] run 'make headerizer'
coke authored
208 SpfInfo *info, STRING *src, STRING *prefix)>
3ec76a6 [core] More function documentation
paultcochrane authored
209
f62611e More doc patches from ajr, and some localization goodness
petdance authored
210 Used by Parrot_sprintf_format. Prepends supplied prefix for numeric
211 values. (e.g. 0x for hex.)
212
213 Returns the pointer to the modified string.
3ec76a6 [core] More function documentation
paultcochrane authored
214
215 =cut
216
217 */
218
f9badd3 tsq.c and sub.c are now headerizer-clean
petdance authored
219 PARROT_CANNOT_RETURN_NULL
c34df07 headerized spf_render.c
petdance authored
220 static STRING*
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
221 str_concat_w_flags(PARROT_INTERP, ARGOUT(STRING *dest), ARGIN(const SpfInfo *info),
857b305 modifying some splint macros
petdance authored
222 ARGMOD(STRING *src), ARGIN_NULLOK(STRING *prefix))
7a74eaf cleanup spf_render a bit
leo authored
223 {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
224 ASSERT_ARGS(str_concat_w_flags)
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
225 src = handle_flags(interp, info, src, 1, prefix);
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
226 dest = Parrot_str_concat(interp, dest, src);
7a74eaf cleanup spf_render a bit
leo authored
227 return dest;
228 }
229
e842151 Inline docs update
mikescott authored
230 /*
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
231
b831c01 Get the rest of Coke's function documentation back.
Infinoid authored
232 =item C<static void gen_sprintf_call(char *out, SpfInfo *info, int thingy)>
e842151 Inline docs update
mikescott authored
233
234 Turn the info structure back into an sprintf format. Far from being
235 pointless, this is used to call C<snprintf()> when we're confronted with
236 a float.
237
d2ed488 [core] Replaced FUNCDOC with C<=item>s. Also tidied pod so that html…
paultcochrane authored
238 =cut
239
e842151 Inline docs update
mikescott authored
240 */
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
241
242 static void
2a53cc4 Converting to ARGMOD instead of ARGINOUT
petdance authored
243 gen_sprintf_call(ARGOUT(char *out), ARGMOD(SpfInfo *info), int thingy)
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
244 {
b5aa5dd [cage] Attempt to work around MSVC warnings related to ASSERT_ARGS().
infinoid authored
245 ASSERT_ARGS(gen_sprintf_call)
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
246
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
247 const int flags = info->flags;
248 char *p = out;
249 *p++ = '%';
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
250
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
251 if (flags) {
252 if (flags & FLAG_MINUS)
253 *p++ = '-';
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
254
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
255 if (flags & FLAG_PLUS)
256 *p++ = '+';
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
257
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
258 if (flags & FLAG_ZERO)
259 *p++ = '0';
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
260
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
261 if (flags & FLAG_SPACE)
262 *p++ = ' ';
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
263
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
264 if (flags & FLAG_SHARP)
265 *p++ = '#';
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
266
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
267 if (flags & FLAG_WIDTH) {
268 if (info->width > PARROT_SPRINTF_BUFFER_SIZE - 1)
269 info->width = PARROT_SPRINTF_BUFFER_SIZE;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
270
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
271 p += sprintf(p, "%u", (unsigned)info->width);
272 }
273
274 if (flags & FLAG_PREC) {
275 if (info->prec > PARROT_SPRINTF_MAX_PREC)
276 info->prec = PARROT_SPRINTF_MAX_PREC;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
277
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
278 *p++ = '.';
279 p += sprintf(p, "%u", (unsigned)info->prec);
280 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
281 }
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
282
124c2a4 fix BE borkeness of Parrot_sprintf_format reported by chromatic++
leo authored
283 if (thingy == 'd' || thingy == 'i' ||thingy == 'u') {
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
284 /* the u?int isa HUGEU?INTVAL aka long long
124c2a4 fix BE borkeness of Parrot_sprintf_format reported by chromatic++
leo authored
285 * the 'll' modifier is specced in susv3 - hopefully all our
286 * compilers support it too */
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
287 *p++ = 'l';
288 *p++ = 'l';
124c2a4 fix BE borkeness of Parrot_sprintf_format reported by chromatic++
leo authored
289 }
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
290
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
291 *p++ = (char)thingy;
292 *p = '\0';
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
293 }
294
24861d8 [t] Fixed a compiler warning with a carefully placed cast (RT #50882,…
chromatic authored
295
e842151 Inline docs update
mikescott authored
296 /*
297
d3ebde7 Disallow NULLs in string_ord, and fix const and ARGMOD on Parrot_spri…
petdance authored
298 =item C<STRING * Parrot_sprintf_format(PARROT_INTERP, const STRING *pat,
299 SPRINTF_OBJ *obj)>
e842151 Inline docs update
mikescott authored
300
301 This is the engine that does all the formatting.
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
302
d2ed488 [core] Replaced FUNCDOC with C<=item>s. Also tidied pod so that html…
paultcochrane authored
303 =cut
304
e842151 Inline docs update
mikescott authored
305 */
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
306
a757a0a Converted vast numbers of old-style WARN_UNUSED, CONST, PURE decorati…
petdance authored
307 PARROT_WARN_UNUSED_RESULT
f9badd3 tsq.c and sub.c are now headerizer-clean
petdance authored
308 PARROT_CANNOT_RETURN_NULL
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
309 STRING *
d3ebde7 Disallow NULLs in string_ord, and fix const and ARGMOD on Parrot_spri…
petdance authored
310 Parrot_sprintf_format(PARROT_INTERP, ARGIN(const STRING *pat), ARGMOD(SPRINTF_OBJ *obj))
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
311 {
b5aa5dd [cage] Attempt to work around MSVC warnings related to ASSERT_ARGS().
infinoid authored
312 ASSERT_ARGS(Parrot_sprintf_format)
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
313 INTVAL i;
314 INTVAL len = 0;
315 INTVAL old = 0;
334e1b9 consting
petdance authored
316 const INTVAL pat_len = (INTVAL)Parrot_str_byte_length(interp, pat);
0c0fc40 Amend spf_render to handle negative field width specified using * flag.
petergibbs authored
317 HUGEINTVAL num;
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
318
319 /* start with a buffer; double the pattern length to avoid realloc #1 */
ddc0c08 hoisted up some common subexpressions and reduced a lot of string ind…
petdance authored
320 STRING *targ = Parrot_str_new_noinit(interp, enum_stringrep_one, pat_len * 2);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
321
322 /* ts is used almost universally as an intermediate target;
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
323 * tc is used as a temporary buffer by Parrot_str_from_uint and
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
324 * as a target by gen_sprintf_call.
325 */
41fbaf1 make trace work again - somehow
leo authored
326 STRING *substr = NULL;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
327 char tc[PARROT_SPRINTF_BUFFER_SIZE];
328
f53b004 manual optimization for old compiler
jimmy authored
329 for (i = 0; i < pat_len; ++i) {
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
330 if (string_ord(interp, pat, i) == '%') { /* % */
41fbaf1 make trace work again - somehow
leo authored
331 if (len) {
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
332 substr = Parrot_str_substr(interp, pat, old, len);
a757a0a Converted vast numbers of old-style WARN_UNUSED, CONST, PURE decorati…
petdance authored
333 /* XXX This shouldn't modify targ the pointer */
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
334 targ = Parrot_str_concat(interp, targ, substr);
41fbaf1 make trace work again - somehow
leo authored
335 }
336 len = 0;
337 old = i;
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
338 if (string_ord(interp, pat, i + 1) == '%') {
9c36e49 bugfix WRT %%
leo authored
339 /* skip this one, make next the first char
340 * of literal sequence, starting at old */
f53b004 manual optimization for old compiler
jimmy authored
341 ++i;
342 ++old;
343 ++len;
41fbaf1 make trace work again - somehow
leo authored
344 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
345 }
346 else {
347 /* hoo boy, here we go... */
348
de8f69d lots of variable localization
petdance authored
349 HUGEINTVAL sharedint = 0;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
350
351 /* Storage for flags, etc. */
289670c [PDD 15] Regain C++ compatibility (Steve Peters, RT #44729).
chromatic authored
352 SpfInfo info = { 0, 0, 0, 0, (PHASE)0 };
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
353
354 /* Reset temporaries */
355 tc[0] = '\0';
356
357 /* This can be really hard to understand, so I'll try to explain beforehand.
358 * A rough grammar for a printf format is:
41fbaf1 make trace work again - somehow
leo authored
359 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
360 * grammar Parrot::PrintF_Format {
361 * rule format {
f1fdeab [core] Adjusted parenthesis spacing according to coding standards. I'll
paultcochrane authored
362 * <other_stuff> (<field> <other_stuff>)*
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
363 * }
41fbaf1 make trace work again - somehow
leo authored
364 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
365 * rule other_stuff {
97a6e3f t/src/sprintf.t was being given int formats and INTVAL arguments, cau…
brentdax authored
366 * [<[^\%]> | \%\%]*:
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
367 * }
41fbaf1 make trace work again - somehow
leo authored
368 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
369 * rule field {
370 * \%
371 * <flags>?
372 * <width>?
373 * [\.<prec>]?
374 * <size>?
375 * <term>
376 * }
41fbaf1 make trace work again - somehow
leo authored
377 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
378 * rule flags {
379 * <[
380 * + # prefix with a + if necessary
381 * - # left-align
382 * 0 # zero-pad
383 * <sp> # space-pad
384 * \# # 0, 0x on octal, hex; force decimal point on float
385 * ]>+
386 * }
41fbaf1 make trace work again - somehow
leo authored
387 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
388 * rule width {
389 * [\d|\*]+ # minimum width
390 * }
41fbaf1 make trace work again - somehow
leo authored
391 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
392 * rule prec {
393 * [\d|\*]+ # width on integers;
394 * # number of digits after decimal on floats;
395 * # maximum width on strings
396 * }
41fbaf1 make trace work again - somehow
leo authored
397 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
398 * rule size {
399 * <[
400 * h # short (or float)
401 * l # long
402 * H # HUGEwhateverVAL (long [long]?, [long]? double)
403 * v # whateverVAL
404 * O # opcode_t
405 * P # really a PMC
406 * S # Parrot string (only with %s)
407 * ]>
408 * }
41fbaf1 make trace work again - somehow
leo authored
409 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
410 * rule term {
411 * <[
412 * c # char
413 * d # integer
414 * i # integer
415 * o # octal
416 * x # hex
417 * X # hex with capital X (if #)
418 * b # binary
419 * B # binary with capital B (if #)
420 * u # unsigned integer
421 * p # pointer
41fbaf1 make trace work again - somehow
leo authored
422 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
423 * e # 1e1
424 * E # 1E1
425 * f # 1.0
426 * g # 1, 0.1, 1e1
427 * G # 1, 0.1, 1E1
41fbaf1 make trace work again - somehow
leo authored
428 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
429 * s # string
430 * ]>
431 * }
432 * }
41fbaf1 make trace work again - somehow
leo authored
433 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
434 * Complication: once upon a time, %P existed. Now you should
435 * use %Ps, %Pd or %Pf, but we still need to support the old form.
436 * The same is true of %S--%Ss is the best form, but %S is still
437 * supported.
41fbaf1 make trace work again - somehow
leo authored
438 *
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
439 * The implementation of Parrot_vsprintf is surprisingly similar to this
41fbaf1 make trace work again - somehow
leo authored
440 * regex, even though the two were developed semi-independently.
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
441 * Parrot_vsprintf keeps track of what it expects to see next (the
41fbaf1 make trace work again - somehow
leo authored
442 * 'phase')--flags, width, precision, size, or field type (term). If it
443 * doesn't find a character that fits whatever it's expecting, it sets
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
444 * info.phase to the next thing and tries it. The first four phases just
445 * set flags--the last does all the work.
446 */
447
f53b004 manual optimization for old compiler
jimmy authored
448 for (++i; i < pat_len && info.phase != PHASE_DONE; ++i) {
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
449 const INTVAL ch = string_ord(interp, pat, i);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
450
451 switch (info.phase) {
452 /*@fallthrough@ */ case PHASE_FLAGS:
453 switch (ch) {
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
454 case '-':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
455 info.flags |= FLAG_MINUS;
b1d553c Make the sprintf flag handler consume its input.
sfink authored
456 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
457
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
458 case '+':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
459 info.flags |= FLAG_PLUS;
b1d553c Make the sprintf flag handler consume its input.
sfink authored
460 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
461
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
462 case '0':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
463 info.flags |= FLAG_ZERO;
b1d553c Make the sprintf flag handler consume its input.
sfink authored
464 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
465
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
466 case ' ':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
467 info.flags |= FLAG_SPACE;
b1d553c Make the sprintf flag handler consume its input.
sfink authored
468 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
469
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
470 case '#':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
471 info.flags |= FLAG_SHARP;
b1d553c Make the sprintf flag handler consume its input.
sfink authored
472 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
473
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
474 default:
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
475 info.phase = PHASE_WIDTH;
476 }
477
478
479 /*@fallthrough@ */ case PHASE_WIDTH:
480 switch (ch) {
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
481 case '0':
482 case '1':
483 case '2':
484 case '3':
485 case '4':
486 case '5':
487 case '6':
488 case '7':
489 case '8':
490 case '9':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
491 info.flags |= FLAG_WIDTH;
492 info.width *= 10;
493 info.width += ch - '0';
494 continue;
495
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
496 case '*':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
497 info.flags |= FLAG_WIDTH;
0c0fc40 Amend spf_render to handle negative field width specified using * flag.
petergibbs authored
498 num = obj->getint(interp, SIZE_XVAL, obj);
499 if (num < 0) {
500 info.flags |= FLAG_MINUS;
501 info.width = -num;
502 }
503 else {
504 info.width = num;
505 }
0e81c90 Re-wrote sprintf2.t to simplify adding more tests.
petergibbs authored
506 continue;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
507
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
508 case '.':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
509 info.phase = PHASE_PREC;
510 continue;
511
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
512 default:
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
513 info.phase = PHASE_PREC;
514 }
515
516
517 /*@fallthrough@ */ case PHASE_PREC:
518 switch (ch) {
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
519 case '0':
520 case '1':
521 case '2':
522 case '3':
523 case '4':
524 case '5':
525 case '6':
526 case '7':
527 case '8':
528 case '9':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
529 info.flags |= FLAG_PREC;
530 info.prec *= 10;
531 info.prec += ch - '0';
532 continue;
533
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
534 case '*':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
535 info.flags |= FLAG_PREC;
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
536 info.prec = (UINTVAL)obj->getint(interp,
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
537 SIZE_XVAL, obj);
a20d5b0 fix bogus width(*) and prec(*) sprintf code
leo authored
538 info.phase = PHASE_TYPE;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
539 continue;
540
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
541 default:
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
542 info.phase = PHASE_TYPE;
543 }
544
545 /*@fallthrough@ */ case PHASE_TYPE:
546 switch (ch) {
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
547 case 'h':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
548 info.type = SIZE_SHORT;
549 continue;
550
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
551 case 'l':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
552 info.type = SIZE_LONG;
553 continue;
554
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
555 case 'L':
556 case 'H':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
557 info.type = SIZE_HUGE;
558 continue;
559
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
560 case 'v':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
561 info.type = SIZE_XVAL;
562 continue;
563
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
564 case 'O':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
565 info.type = SIZE_OPCODE;
566 continue;
567
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
568 case 'P':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
569 info.type = SIZE_PMC;
570 continue;
571
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
572 case 'S':
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
573 info.type = SIZE_PSTR;
574 continue;
575
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
576 default:
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
577 info.phase = PHASE_TERM;
578 }
579
580
581 /*@fallthrough@ */ case PHASE_TERM:
582 switch (ch) {
583 /* INTEGERS */
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
584 case 'c':
de8f69d lots of variable localization
petdance authored
585 {
586 STRING * const ts = string_chr(interp,
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
587 (UINTVAL)obj->getint(interp, info.type, obj));
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
588 targ = str_concat_w_flags(interp, targ, &info, ts, NULL);
de8f69d lots of variable localization
petdance authored
589 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
590 break;
591
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
592 case 'o':
de8f69d lots of variable localization
petdance authored
593 {
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
594 const UHUGEINTVAL theuint =
595 obj->getuint(interp, info.type, obj);
596 STRING * const ts =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
597 Parrot_str_from_uint(interp, tc, theuint, 8, 0);
de8f69d lots of variable localization
petdance authored
598 STRING * const prefix = CONST_STRING(interp, "0");
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
599
bd83e3c fix 6 sprintf.t (%X, %+b..)
leo authored
600 /* unsigned conversion - no plus */
601 info.flags &= ~FLAG_PLUS;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
602 targ = str_concat_w_flags(interp, targ,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
603 &info, ts, prefix);
de8f69d lots of variable localization
petdance authored
604 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
605 break;
606
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
607 case 'x':
de8f69d lots of variable localization
petdance authored
608 {
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
609 const UHUGEINTVAL theuint =
610 obj->getuint(interp, info.type, obj);
611 STRING * const ts =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
612 Parrot_str_from_uint(interp, tc, theuint, 16, 0);
de8f69d lots of variable localization
petdance authored
613 STRING * const prefix = CONST_STRING(interp, "0x");
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
614
bd83e3c fix 6 sprintf.t (%X, %+b..)
leo authored
615 /* unsigned conversion - no plus */
616 info.flags &= ~FLAG_PLUS;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
617 targ = str_concat_w_flags(interp, targ,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
618 &info, ts, prefix);
de8f69d lots of variable localization
petdance authored
619 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
620 break;
621
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
622 case 'X':
de8f69d lots of variable localization
petdance authored
623 {
624 STRING * const prefix = CONST_STRING(interp, "0X");
625 const UHUGEINTVAL theuint =
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
626 obj->getuint(interp, info.type, obj);
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
627 STRING * ts =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
628 Parrot_str_from_uint(interp, tc, theuint, 16, 0);
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
629 ts = Parrot_str_upcase(interp, ts);
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
630
bd83e3c fix 6 sprintf.t (%X, %+b..)
leo authored
631 /* unsigned conversion - no plus */
632 info.flags &= ~FLAG_PLUS;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
633 targ = str_concat_w_flags(interp, targ,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
634 &info, ts, prefix);
de8f69d lots of variable localization
petdance authored
635 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
636 break;
637
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
638 case 'b':
de8f69d lots of variable localization
petdance authored
639 {
640 STRING * const prefix = CONST_STRING(interp, "0b");
641 const UHUGEINTVAL theuint =
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
642 obj->getuint(interp, info.type, obj);
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
643 STRING * const ts =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
644 Parrot_str_from_uint(interp, tc, theuint, 2, 0);
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
645
bd83e3c fix 6 sprintf.t (%X, %+b..)
leo authored
646 /* unsigned conversion - no plus */
647 info.flags &= ~FLAG_PLUS;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
648 targ = str_concat_w_flags(interp, targ,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
649 &info, ts, prefix);
de8f69d lots of variable localization
petdance authored
650 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
651 break;
652
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
653 case 'B':
de8f69d lots of variable localization
petdance authored
654 {
655 STRING * const prefix = CONST_STRING(interp, "0B");
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
656 const HUGEINTVAL theint =
657 obj->getint(interp, info.type, obj);
658 STRING * const ts =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
659 Parrot_str_from_int_base(interp, tc, theint, 2);
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
660
bd83e3c fix 6 sprintf.t (%X, %+b..)
leo authored
661 /* unsigned conversion - no plus */
662 info.flags &= ~FLAG_PLUS;
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
663 targ = str_concat_w_flags(interp, targ,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
664 &info, ts, prefix);
de8f69d lots of variable localization
petdance authored
665 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
666 break;
667
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
668 case 'u':
de8f69d lots of variable localization
petdance authored
669 {
670 const UHUGEINTVAL theuint =
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
671 obj->getuint(interp, info.type, obj);
de8f69d lots of variable localization
petdance authored
672 sharedint = theuint;
673 }
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
674 goto do_sprintf;
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
675 case 'd':
676 case 'i':
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
677
678 /* EVIL: Work around bug in glibc that makes %0lld
679 * sometimes output an empty string. */
d69e0e5 Fix sprintf('%o', ...) by treating the value as unsigned. (!)
chip authored
680 if (!(info.flags & FLAG_WIDTH))
681 info.flags &= ~FLAG_ZERO;
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
682
de8f69d lots of variable localization
petdance authored
683 sharedint = obj->getint(interp, info.type, obj);
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
684 do_sprintf:
de8f69d lots of variable localization
petdance authored
685 {
686 STRING *ts;
2231b0d removed unused interp parm from gen_sprintf_call
petdance authored
687 gen_sprintf_call(tc, &info, ch);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
688 ts = cstr2pstr(tc);
689 {
7c17462 [PDD07]: ws cleanup in src/, src/dynpmc/, and src/pmc/
particle authored
690 char * const tempstr =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
691 Parrot_str_to_cstring(interp, ts);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
692
693 #ifdef PARROT_HAS_SNPRINTF
694 snprintf(tc, PARROT_SPRINTF_BUFFER_SIZE,
de8f69d lots of variable localization
petdance authored
695 tempstr, sharedint);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
696 #else
697 /* the buffer is 4096, so no problem here */
de8f69d lots of variable localization
petdance authored
698 sprintf(tc, tempstr, sharedint);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
699 #endif
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
700 Parrot_str_free_cstring(tempstr);
957c8dc fix 8 more sprintf.t (%3.2d, %+u..); adjust t/op/string.t
leo authored
701 }
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
702 targ = Parrot_str_concat(interp, targ, cstr2pstr(tc));
de8f69d lots of variable localization
petdance authored
703 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
704 break;
705
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
706 case 'p':
de8f69d lots of variable localization
petdance authored
707 {
708 STRING * const prefix = CONST_STRING(interp, "0x");
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
709 const void * const ptr =
710 obj->getptr(interp, info.type, obj);
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
711 STRING * const ts = Parrot_str_from_uint(interp, tc,
4f197a1 [src] Changed base parameter in int_to_str and uint_to_str to an unsi…
chromatic authored
712 (UHUGEINTVAL) (size_t) ptr, 16, 0);
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
713
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
714 targ = str_concat_w_flags(interp, targ, &info,
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
715 ts, prefix);
de8f69d lots of variable localization
petdance authored
716 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
717 break;
718
719 /* FLOATS - We cheat on these and use snprintf. */
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
720 case 'e':
721 case 'E':
722 case 'f':
723 case 'g':
724 case 'G':
de8f69d lots of variable localization
petdance authored
725 {
726 STRING *ts;
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
727 const HUGEFLOATVAL thefloat =
728 obj->getfloat(interp, info.type, obj);
729
5117087 [core] portable Inf/NaN support
particle authored
730 /* check for Inf and NaN values */
731 if (thefloat == PARROT_FLOATVAL_INF_POSITIVE) {
732 ts = cstr2pstr(PARROT_CSTRING_INF_POSITIVE);
733 }
734 else if (thefloat == PARROT_FLOATVAL_INF_NEGATIVE) {
735 ts = cstr2pstr(PARROT_CSTRING_INF_NEGATIVE);
736 }
737 else if (thefloat != thefloat) {
738 ts = cstr2pstr(PARROT_CSTRING_NAN_QUIET);
739 }
740 else {
741 /* turn -0.0 into 0.0 */
742 gen_sprintf_call(tc, &info, ch);
743 ts = cstr2pstr(tc);
744 }
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
745
746 /* XXX lost precision if %Hg or whatever */
7455364 Start patching up memory leaks from string_to_cstring behaviour change
dan authored
747 {
7c17462 [PDD07]: ws cleanup in src/, src/dynpmc/, and src/pmc/
particle authored
748 char * const tempstr =
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
749 Parrot_str_to_cstring(interp, ts);
7455364 Start patching up memory leaks from string_to_cstring behaviour change
dan authored
750
dfda541 snprintf test
leo authored
751 #ifdef PARROT_HAS_SNPRINTF
7455364 Start patching up memory leaks from string_to_cstring behaviour change
dan authored
752 snprintf(tc, PARROT_SPRINTF_BUFFER_SIZE,
753 tempstr,
754 (double)thefloat);
6e7a4fd various patches 2
leo authored
755 #else
756 /* the buffer is 4096, so no problem here */
757 sprintf(tc, tempstr, (double)thefloat);
758 #endif
b6b4ec2 [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allison authored
759 Parrot_str_free_cstring(tempstr);
7455364 Start patching up memory leaks from string_to_cstring behaviour change
dan authored
760 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
761
5badc83 Extend Win32 fix/hack/workaround for sprintf("%g") and three-digit ex…
chip authored
762 #ifdef WIN32
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
763
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
764 /* Microsoft defaults to three digits for
765 * exponents, even when fewer digits would suffice.
766 * For the sake of portability, we will here
767 * attempt to hide that. */
768 if (ch == 'g' || ch == 'G'
769 || ch == 'e' || ch == 'E') {
2231b0d removed unused interp parm from gen_sprintf_call
petdance authored
770 const size_t tclen = strlen(tc);
d4e18b8 Refine the Win32 sprintf("%e") kludge. But it's getting to the point…
chip authored
771 size_t j;
f53b004 manual optimization for old compiler
jimmy authored
772 for (j = 0; j < tclen; ++j) {
41bb8b7 [src] Correcting parenthesis-space issues as per coding standards (pa…
paultcochrane authored
773 if ((tc[j] == 'e' || tc[j] == 'E')
d4e18b8 Refine the Win32 sprintf("%e") kludge. But it's getting to the point…
chip authored
774 && (tc[j+1] == '+' || tc[j+1] == '-')
775 && tc[j+2] == '0'
776 && isdigit((unsigned char)tc[j+3])
777 && isdigit((unsigned char)tc[j+4]))
3dd6632 Consistently name interpreter "interp" instead of "interpreter".
chip authored
778 {
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
779 mem_sys_memmove(&tc[j+2], &tc[j+3],
780 strlen(&tc[j+2]));
5310bec Improve Win32 sprintf('%e') hack to readjust length after removing zero.
chip authored
781
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
782 /* now fix any broken length */
783
784 if ((info.flags & FLAG_WIDTH)
785 && strlen(tc) < info.width) {
5310bec Improve Win32 sprintf('%e') hack to readjust length after removing zero.
chip authored
786 if (info.flags & FLAG_MINUS)
787 strcat(tc, " ");
788 else {
8a9068c [src] Replaced a magic constant about the size of the destination STR…
chromatic authored
789 mem_sys_memmove(&tc[1], &tc[0],
790 strlen(tc) + 1);
72fbaf7 Just a little more kludge. But I can quit any time.
chip authored
791 tc[0] = (info.flags & FLAG_ZERO) ? '0' : ' ';
5310bec Improve Win32 sprintf('%e') hack to readjust length after removing zero.
chip authored
792 }
793 }
d4e18b8 Refine the Win32 sprintf("%e") kludge. But it's getting to the point…
chip authored
794
795 /* only one fix required per string */
796 break;
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
797 }
798 }
799 }
5badc83 Extend Win32 fix/hack/workaround for sprintf("%g") and three-digit ex…
chip authored
800 #endif /* WIN32 */
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
801
baa7674 Everybody freeze! Immutable strings enters the building.
bacek authored
802 targ = Parrot_str_concat(interp, targ, cstr2pstr(tc));
de8f69d lots of variable localization
petdance authored
803 }
53b5369 This is that huge sprintf thing I've been working on all week.
brentdax authored
804 break;
805
806 /* STRINGS */
47864fd [cage] improve C indentation, see TT #1329
fperrad authored
807 case 'r': /* Python repr */
b6eaa11 t/pie/b0 now completes parse
rubys authored
808 /* XXX the right fix is to add a getrepr entry *
809 * to SPRINTF_OBJ, but for now, getstring_pmc *
810 * is inlined and modified to call get_repr */
811 if (obj->getstring == pmc_core.getstring) {
7c17462 [PDD07]: ws cleanup in src/, src/dynp