-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathGSFormat.m
2063 lines (1774 loc) · 51.9 KB
/
GSFormat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Implementation of GNUSTEP printf-style formatting
Copyright (C) 1994-2000, 2001-2013 Free Software Foundation, Inc.
Hacked together by Kai Henningsen <kai@cats.ms>
from the glibc 2.2.1 sources
_i18n_number.h
_itowa.h
itowa-digits.c
outdigits.h
outdigitswc.h
printf-parse.h
printf.h
vfprintf.c
which were contributed by Ulrich Drepper <drepper@gnu.org>, 2000,
Date: January 2001
FIXME: I wasn't brave enough to include floating point formatting -
glibc has CPU dependent routines and also uses gmp. So floating point
formats (AaFfGgAa) simply use sprintf.
FIXME: This needs to use length, not '\0', when dealing with format
and %@
No register_printf_functions in this thing.
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#include "common.h"
#if !defined(LLONG_MAX)
# if defined(__LONG_LONG_MAX__)
# define LLONG_MAX __LONG_LONG_MAX__
# define LLONG_MIN (-LLONG_MAX-1)
# define ULLONG_MAX (LLONG_MAX * 2ULL + 1)
# else
# error Neither LLONG_MAX nor __LONG_LONG_MAX__ found
# endif
#endif
#ifdef HAVE_MALLOC_H
#if !defined(__OpenBSD__)
#include <malloc.h>
#endif
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <stdio.h>
#import "Foundation/NSArray.h"
#import "Foundation/NSCharacterSet.h"
#import "Foundation/NSException.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSPortCoder.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSRange.h"
#import "Foundation/NSException.h"
#import "Foundation/NSData.h"
#import "Foundation/NSBundle.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSLock.h"
#import "GNUstepBase/GSLocale.h"
#import "GSPrivate.h"
#include <string.h> // for strstr()
#include <sys/stat.h>
#include <sys/types.h>
#if defined(HAVE_SYS_FCNTL_H)
# include <sys/fcntl.h>
#elif defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#include <stdio.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#else
typedef uint32_t wint_t;
#endif
#ifdef HAVE_SYS_INTTYPES_H
#include <sys/inttypes.h>
#endif
#ifndef HAVE_UINTMAX_T
typedef unsigned long long uintmax_t;
#endif
#import "GNUstepBase/Unicode.h"
struct printf_info
{
int prec; /* Precision. */
int width; /* Width. */
unichar spec; /* Format letter. */
unsigned int is_long_double:1;/* L flag. */
unsigned int is_short:1; /* h flag. */
unsigned int is_long:1; /* l flag. */
unsigned int alt:1; /* # flag. */
unsigned int space:1; /* Space flag. */
unsigned int left:1; /* - flag. */
unsigned int showsign:1; /* + flag. */
unsigned int group:1; /* ' flag. */
unsigned int extra:1; /* For special use. */
unsigned int is_char:1; /* hh flag. */
unsigned int wide:1; /* Nonzero for wide character streams. */
unsigned int i18n:1; /* I flag. */
unichar pad; /* Padding character. */
};
/* Type of a printf specifier-handler function.
STREAM is the GSStr on which to write output.
INFO gives information about the format specification.
ARGS is a vector of pointers to the argument data;
the number of pointers will be the number returned
by the associated arginfo function for the same INFO.
The function should return the number of characters written,
or -1 for errors. */
/* Type of a printf specifier-arginfo function.
INFO gives information about the format specification.
N, ARGTYPES, and return value are as for printf_parse_format. */
/* Register FUNC to be called to format SPEC specifiers; ARGINFO must be
specified to determine how many arguments a SPEC conversion requires and
what their types are. */
/* Parse FMT, and fill in N elements of ARGTYPES with the
types needed for the conversions FMT specifies. Returns
the number of arguments required by FMT.
The ARGINFO function registered with a user-defined format is passed a
`struct printf_info' describing the format spec being parsed. A width
or precision of INT_MIN means a `*' was used to indicate that the
width/precision will come from an arg. The function should fill in the
array it is passed with the types of the arguments it wants, and return
the number of arguments it wants. */
/* Codes returned by `parse_printf_format' for basic types.
These values cover all the standard format specifications.
Users can add new values after PA_LAST for their own types. */
enum
{ /* C type: */
PA_INT, /* int */
PA_CHAR, /* int, cast to char */
PA_WCHAR, /* wide char */
PA_STRING, /* const char *, a '\0'-terminated string */
PA_WSTRING, /* const wchar_t *, wide character string */
PA_POINTER, /* void * */
PA_FLOAT, /* float */
PA_DOUBLE, /* double */
PA_OBJECT, /* id */
PA_LAST
};
/* Flag bits that can be set in a type returned by `parse_printf_format'. */
#define PA_FLAG_MASK 0xff00
#define PA_FLAG_LONG_LONG (1 << 8)
#define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG
#define PA_FLAG_LONG (1 << 9)
#define PA_FLAG_SHORT (1 << 10)
#define PA_FLAG_PTR (1 << 11)
/* Function which can be registered as `printf'-handlers. */
/* Print floating point value using using abbreviations for the orders
of magnitude used for numbers ('k' for kilo, 'm' for mega etc). If
the format specifier is a uppercase character powers of 1000 are
used. Otherwise powers of 1024. */
/* This is the appropriate argument information function for `printf_size'. */
/* Digits. */
/* Lower-case digits. */
static const char _itowa_lower_digits[36]
= "0123456789abcdefghijklmnopqrstuvwxyz";
/* Upper-case digits. */
static const char _itowa_upper_digits[36]
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* This code is shared between the standard stdio implementation found
in GNU C library and the libio implementation originally found in
GNU libg++.
Beside this it is also shared between the normal and wide character
implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
# define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
/* Internal function for converting integers to ASCII. */
/* Convert VALUE into ASCII in base BASE (2..36).
Write backwards starting the character just before BUFLIM.
Return the address of the first (left-to-right) character in the number.
Use upper case letters iff UPPER_CASE is nonzero. */
static unichar *_itowa (unsigned long long int value, unichar *buflim,
unsigned int base, int upper_case)
{
const char *digits = (upper_case
? _itowa_upper_digits : _itowa_lower_digits);
unichar *bp = buflim;
switch (base)
{
#define SPECIAL(Base) \
case Base: \
do \
*--bp = digits[value % Base]; \
while ((value /= Base) != 0); \
break
SPECIAL (10);
SPECIAL (16);
SPECIAL (8);
default:
do
*--bp = digits[value % base];
while ((value /= base) != 0);
break;
}
return bp;
}
static inline unichar *
_itowa_word (unsigned long value, unichar *buflim,
unsigned int base, int upper_case)
{
const char *digits = (upper_case
? _itowa_upper_digits : _itowa_lower_digits);
unichar *bp = buflim;
switch (base)
{
#define SPECIAL(Base) \
case Base: \
do \
*--bp = digits[value % Base]; \
while ((value /= Base) != 0); \
break
SPECIAL (10);
SPECIAL (16);
SPECIAL (8);
default:
do
*--bp = digits[value % base];
while ((value /= base) != 0);
break;
}
return bp;
}
#define PAD(Padchar) \
{ \
int w = width; \
while (w-- > 0) outchar(Padchar); \
}
/* Look up the value of the next multibyte character and return its numerical
value if it is one of the digits known in the locale. If *DECIDED is
-1 this means it is not yet decided which form it is and we have to
search through all available digits. Otherwise we know which script
the digits are from. */
static unichar *
_i18n_number_rewrite (unichar *w, unichar *rear_ptr, NSString *locale_digits)
{
unichar *src;
unichar *s;
unichar buf[10];
unichar *ld = 0;
/* Copy existing string so that nothing gets overwritten. */
src = (unichar *) alloca ((rear_ptr - w) * sizeof (unichar));
s = (unichar *) memcpy (src, w, (rear_ptr - w) * sizeof (unichar));
w = rear_ptr;
/* Process all characters in the string. */
while (--s >= src)
{
if (*s >= '0' && *s <= '9')
{
if (ld == 0)
{
if (!locale_digits || [locale_digits length] != 10)
{
locale_digits = @"0123456789";
}
[locale_digits getCharacters: buf];
ld = buf;
}
*--w = ld[*s - '0'];
}
else
{
*--w = *s;
}
}
return w;
}
/* Include the shared code for parsing the format string. */
/* Internal header for parsing printf format strings. */
struct printf_spec
{
/* Information parsed from the format spec. */
struct printf_info info;
/* Pointers into the format string for the end of this format
spec and the next (or to the end of the string if no more). */
const unichar *end_of_fmt, *next_fmt;
/* Position of arguments for precision and width, or -1 if `info' has
the constant value. */
int prec_arg, width_arg;
int data_arg; /* Position of data argument. */
int data_arg_type; /* Type of first argument. */
/* Number of arguments consumed by this format specifier. */
size_t ndata_args;
};
/* The various kinds off arguments that can be passed to printf. */
union printf_arg
{
unsigned char pa_char;
wchar_t pa_wchar;
short int pa_short_int;
int pa_int;
long int pa_long_int;
long long int pa_long_long_int;
unsigned short int pa_u_short_int;
unsigned int pa_u_int;
unsigned long int pa_u_long_int;
unsigned long long int pa_u_long_long_int;
float pa_float;
double pa_double;
long double pa_long_double;
const char *pa_string;
const wchar_t *pa_wstring;
id pa_object;
void *pa_pointer;
};
/* Read a simple integer from a string and update the string pointer.
It is assumed that the first character is a digit. */
static inline unsigned int
read_int (const unichar * *pstr)
{
unsigned int retval = **pstr - '0';
while (ISDIGIT (*++(*pstr)))
{
retval *= 10;
retval += **pstr - '0';
}
return retval;
}
/* Find the next spec in FORMAT, or the end of the string. Returns
a pointer into FORMAT, to a '%' or a '\0'. */
static inline const unichar *
find_spec (const unichar *format)
{
while (*format && *format != '%') format++;
return format;
}
/* These are defined in reg-printf.c. */
/* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
with the parsed details. POSN is the number of arguments already
consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
the number of args consumed by this spec; *MAX_REF_ARG is updated so it
remains the highest argument index used. */
static inline size_t
parse_one_spec (const unichar *format, size_t posn, struct printf_spec *spec,
size_t *max_ref_arg)
{
unsigned int n;
size_t nargs = 0;
/* Skip the '%'. */
++format;
/* Clear information structure. */
spec->data_arg = -1;
spec->info.alt = 0;
spec->info.space = 0;
spec->info.left = 0;
spec->info.showsign = 0;
spec->info.group = 0;
spec->info.i18n = 0;
spec->info.pad = ' ';
spec->info.wide = sizeof (unichar) > 1;
/* Test for positional argument. */
if (ISDIGIT (*format))
{
const unichar *begin = format;
n = read_int (&format);
if (n > 0 && *format == '$')
/* Is positional parameter. */
{
++format; /* Skip the '$'. */
spec->data_arg = n - 1;
*max_ref_arg = MAX (*max_ref_arg, n);
}
else
/* Oops; that was actually the width and/or 0 padding flag.
Step back and read it again. */
format = begin;
}
/* Check for spec modifiers. */
do
{
switch (*format)
{
case ' ':
/* Output a space in place of a sign, when there is no sign. */
spec->info.space = 1;
continue;
case '+':
/* Always output + or - for numbers. */
spec->info.showsign = 1;
continue;
case '-':
/* Left-justify things. */
spec->info.left = 1;
continue;
case '#':
/* Use the "alternate form":
Hex has 0x or 0X, FP always has a decimal point. */
spec->info.alt = 1;
continue;
case '0':
/* Pad with 0s. */
spec->info.pad = '0';
continue;
case '\'':
/* Show grouping in numbers if the locale information
indicates any. */
spec->info.group = 1;
continue;
case 'I':
/* Use the internationalized form of the output. Currently
means to use the `outdigits' of the current locale. */
spec->info.i18n = 1;
continue;
default:
break;
}
break;
}
while (*++format);
if (spec->info.left)
spec->info.pad = ' ';
/* Get the field width. */
spec->width_arg = -1;
spec->info.width = 0;
if (*format == '*')
{
/* The field width is given in an argument.
A negative field width indicates left justification. */
const unichar *begin = ++format;
if (ISDIGIT (*format))
{
/* The width argument might be found in a positional parameter. */
n = read_int (&format);
if (n > 0 && *format == '$')
{
spec->width_arg = n - 1;
*max_ref_arg = MAX (*max_ref_arg, n);
++format; /* Skip '$'. */
}
}
if (spec->width_arg < 0)
{
/* Not in a positional parameter. Consume one argument. */
spec->width_arg = posn++;
++nargs;
format = begin; /* Step back and reread. */
}
}
else if (ISDIGIT (*format))
/* Constant width specification. */
spec->info.width = read_int (&format);
/* Get the precision. */
spec->prec_arg = -1;
/* -1 means none given; 0 means explicit 0. */
spec->info.prec = -1;
if (*format == '.')
{
++format;
if (*format == '*')
{
/* The precision is given in an argument. */
const unichar *begin = ++format;
if (ISDIGIT (*format))
{
n = read_int (&format);
if (n > 0 && *format == '$')
{
spec->prec_arg = n - 1;
*max_ref_arg = MAX (*max_ref_arg, n);
++format;
}
}
if (spec->prec_arg < 0)
{
/* Not in a positional parameter. */
spec->prec_arg = posn++;
++nargs;
format = begin;
}
}
else if (ISDIGIT (*format))
spec->info.prec = read_int (&format);
else
/* "%.?" is treated like "%.0?". */
spec->info.prec = 0;
}
/* Check for type modifiers. */
spec->info.is_long_double = 0;
spec->info.is_short = 0;
spec->info.is_long = 0;
spec->info.is_char = 0;
switch (*format++)
{
case 'h':
/* ints are short ints or chars. */
if (*format != 'h')
spec->info.is_short = 1;
else
{
++format;
spec->info.is_char = 1;
}
break;
case 'l':
/* ints are long ints. */
spec->info.is_long = 1;
if (*format != 'l')
break;
++format;
/* FALLTHROUGH */
case 'L':
/* doubles are long doubles, and ints are long long ints. */
case 'q':
/* 4.4 uses this for long long. */
spec->info.is_long_double = 1;
break;
case 'z':
case 'Z':
/* ints are size_ts. */
NSCParameterAssert (sizeof (size_t) <= sizeof (unsigned long long int));
#if defined(LLONG_MAX)
#if LONG_MAX != LLONG_MAX
spec->info.is_long_double = sizeof (size_t) > sizeof (unsigned long int);
#endif
#endif
spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
break;
case 't':
NSCParameterAssert (sizeof (ptrdiff_t) <= sizeof (long long int));
#if defined(LLONG_MAX)
#if LONG_MAX != LLONG_MAX
spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
#endif
#endif
spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
break;
case 'j':
NSCParameterAssert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
#if defined(LLONG_MAX)
#if LONG_MAX != LLONG_MAX
spec->info.is_long_double = (sizeof (uintmax_t)
> sizeof (unsigned long int));
#endif
#endif
spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
break;
default:
/* Not a recognized modifier. Backup. */
--format;
break;
}
/* Get the format specification. */
spec->info.spec = (unichar) *format++;
{
/* Find the data argument types of a built-in spec. */
spec->ndata_args = 1;
switch (spec->info.spec)
{
case 'i':
case 'd':
case 'u':
case 'o':
case 'X':
case 'x':
#if defined(LLONG_MAX)
#if LONG_MAX != LLONG_MAX
if (spec->info.is_long_double)
spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
else
#endif
#endif
if (spec->info.is_long)
spec->data_arg_type = PA_INT|PA_FLAG_LONG;
else if (spec->info.is_short)
spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
else if (spec->info.is_char)
spec->data_arg_type = PA_CHAR;
else
spec->data_arg_type = PA_INT;
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
if (spec->info.is_long_double)
spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
else
spec->data_arg_type = PA_DOUBLE;
break;
case 'c':
spec->data_arg_type = PA_CHAR;
break;
case 'C':
spec->data_arg_type = PA_WCHAR;
break;
case 's':
spec->data_arg_type = PA_STRING;
break;
case 'S':
spec->data_arg_type = PA_WSTRING;
break;
case '@':
spec->data_arg_type = PA_OBJECT;
break;
case 'p':
spec->data_arg_type = PA_POINTER;
break;
case 'n':
spec->data_arg_type = PA_INT|PA_FLAG_PTR;
break;
case 'm':
default:
/* An unknown spec will consume no args. */
spec->ndata_args = 0;
break;
}
}
if (spec->data_arg == -1 && spec->ndata_args > 0)
{
/* There are args consumed, but no positional spec. Use the
next sequential arg position. */
spec->data_arg = posn;
nargs += spec->ndata_args;
}
if (spec->info.spec == '\0')
/* Format ended before this spec was complete. */
spec->end_of_fmt = spec->next_fmt = format - 1;
else
{
/* Find the next format spec. */
spec->end_of_fmt = format;
spec->next_fmt = find_spec (format);
}
return nargs;
}
static inline void GSStrAppendUnichar(GSStr s, unichar u)
{
GSPrivateStrAppendUnichars(s, &u, 1);
}
#define outchar(Ch) GSStrAppendUnichar(s, Ch)
#define outstring(String, Len) GSPrivateStrAppendUnichars(s, String, Len)
/* For handling long_double and longlong we use the same flag. If
`long' and `long long' are effectively the same type define it to
zero. */
#if defined(LLONG_MAX)
#if LONG_MAX == LLONG_MAX
# define is_longlong 0
#else
# define is_longlong is_long_double
#endif
#else
# define is_longlong 0
#endif
/* If `long' and `int' is effectively the same type we don't have to
handle `long separately. */
#if INT_MAX == LONG_MAX
# define is_long_num 0
#else
# define is_long_num is_long
#endif
static NSString *locale_sep()
{
static NSString *sep = nil;
if (sep == nil)
{
char buf[32];
char *from = buf;
char *to;
snprintf(buf, sizeof(buf), "%g", 1.2);
if (*from == '1') from++;
to = from;
while (*to != '\0' && *to != '2')
to++;
*to = '\0';
sep = [[NSString alloc] initWithCString: from];
}
return sep;
}
/* Global variables. */
static const unichar null[] = {'(','n','u','l','l',')','\0'};
/* Handle unknown format specifier. */
static int printf_unknown (GSStr, const struct printf_info *,
const void *const *);
/* Group digits of number string. */
static unichar *group_number (unichar *, unichar *, const char *, NSString *);
/* The function itself. */
void
GSPrivateFormat (GSStr s, const unichar *format, va_list ap,
NSDictionary *locale)
{
/* The character used as thousands separator. */
NSString *thousands_sep = @"";
/* The string describing the size of groups of digits. */
const char *grouping;
/* Place to accumulate the result. */
int done;
/* Current character in format string. */
const unichar *f;
/* End of leading constant string. */
const unichar *lead_str_end;
/* Points to next format specifier. */
/* Buffer intermediate results. */
unichar work_buffer[1000];
unichar *workend;
int workend_malloced = 0;
/* State for restartable multibyte character handling functions. */
/* We have to save the original argument pointer. */
va_list ap_save;
/* Count number of specifiers we already processed. */
int nspecs_done;
/* For the %m format we may need the current `errno' value. */
int save_errno = errno;
/* This table maps a character into a number representing a
class. In each step there is a destination label for each
class. */
static const int jump_table[] =
{
/* ' ' */ 1, 0, 0, /* '#' */ 4,
0, /* '%' */ 14, 0, /* '\''*/ 6,
0, 0, /* '*' */ 7, /* '+' */ 2,
0, /* '-' */ 3, /* '.' */ 9, 0,
/* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
/* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
/* '8' */ 8, /* '9' */ 8, 0, 0,
0, 0, 0, 0,
/* '@' */ 30, /* 'A' */ 26, 0, /* 'C' */ 25,
0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
0, /* 'I' */ 29, 0, 0,
/* 'L' */ 12, 0, 0, 0,
0, 0, 0, /* 'S' */ 21,
0, 0, 0, 0,
/* 'X' */ 18, 0, /* 'Z' */ 13, 0,
0, 0, 0, 0,
0, /* 'a' */ 26, 0, /* 'c' */ 20,
/* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
/* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
/* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
/* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
/* 't' */ 27, /* 'u' */ 16, 0, 0,
/* 'x' */ 18, 0, /* 'z' */ 13
};
#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'z')
#define CHAR_CLASS(Ch) (jump_table[(wint_t) (Ch) - ' '])
# define JUMP_TABLE_TYPE const void *const
/* Initialize local variables. */
done = 0;
grouping = (const char *) -1;
#ifdef __va_copy
/* This macro will be available soon in gcc's <stdarg.h>. We need it
since on some systems `va_list' is not an integral type. */
__va_copy (ap_save, ap);
#else
ap_save = ap;
#endif
nspecs_done = 0;
/* Find the first format specifier. */
f = lead_str_end = find_spec ((const unichar *) format);
/* Write the literal text before the first format. */
outstring ((const unichar *) format,
lead_str_end - (const unichar *) format);
/* If we only have to print a simple string, return now. */
if (*f == '\0')
goto all_done;
/* Process whole format string. */
workend = &work_buffer[sizeof (work_buffer) / sizeof (unichar)];
/* Here starts the more complex loop to handle positional parameters. */
{
/* Array with information about the needed arguments. This has to
be dynamically extensible. */
size_t nspecs = 0;
size_t nspecs_max = 32; /* A more or less arbitrary start value. */
struct printf_spec *specs
= alloca (nspecs_max * sizeof (struct printf_spec));
/* The number of arguments the format string requests. This will
determine the size of the array needed to store the argument
attributes. */
size_t nargs = 0;
int *args_type;
union printf_arg *args_value = NULL;
/* Positional parameters refer to arguments directly. This could
also determine the maximum number of arguments. Track the
maximum number. */
size_t max_ref_arg = 0;
/* Just a counter. */
size_t cnt;
if (grouping == (const char *) -1)
{
thousands_sep = [locale objectForKey: NSThousandsSeparator];
if (thousands_sep == nil) thousands_sep = @",";
grouping = ""; // FIXME: grouping info missing in locale?
if (*grouping == '\0' || *grouping == CHAR_MAX)
grouping = NULL;
}
for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
{
if (nspecs >= nspecs_max)
{
/* Extend the array of format specifiers. */
struct printf_spec *old = specs;
nspecs_max *= 2;
specs = alloca (nspecs_max * sizeof (struct printf_spec));
if (specs == &old[nspecs])
/* Stack grows up, OLD was the last thing allocated;
extend it. */
nspecs_max += nspecs_max / 2;
else
{
/* Copy the old array's elements to the new space. */
memcpy (specs, old, nspecs * sizeof (struct printf_spec));
if (old == &specs[nspecs])
/* Stack grows down, OLD was just below the new
SPECS. We can use that space when the new space
runs out. */
nspecs_max += nspecs_max / 2;
}
}
/* Parse the format specifier. */
nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg);
}
/* Determine the number of arguments the format string consumes. */
nargs = MAX (nargs, max_ref_arg);
/* Allocate memory for the argument descriptions. */
args_type = alloca (nargs * sizeof (int));
memset (args_type, 0, nargs * sizeof (int));
args_value = alloca (nargs * sizeof (union printf_arg));
/* XXX Could do sanity check here: If any element in ARGS_TYPE is
still zero after this loop, format is invalid. For now we
simply use 0 as the value. */
/* Fill in the types of all the arguments. */
for (cnt = 0; cnt < nspecs; ++cnt)
{
/* If the width is determined by an argument this is an int. */
if (specs[cnt].width_arg != -1)
args_type[specs[cnt].width_arg] = PA_INT;
/* If the precision is determined by an argument this is an int. */
if (specs[cnt].prec_arg != -1)
args_type[specs[cnt].prec_arg] = PA_INT;
switch (specs[cnt].ndata_args)
{