-
-
Notifications
You must be signed in to change notification settings - Fork 706
/
package.d
1832 lines (1550 loc) · 58.7 KB
/
package.d
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
// Written in the D programming language.
/**
This package provides string formatting functionality using
`printf` style format strings.
$(BOOKTABLE ,
$(TR $(TH Submodule) $(TH Function Name) $(TH Description))
$(TR
$(TD $(I package))
$(TD $(LREF format))
$(TD Converts its arguments according to a format string into a string.)
)
$(TR
$(TD $(I package))
$(TD $(LREF sformat))
$(TD Converts its arguments according to a format string into a buffer.)
)
$(TR
$(TD $(I package))
$(TD $(LREF FormatException))
$(TD Signals a problem while formatting.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D write), std, format, write))
$(TD $(REF_ALTTEXT $(D formattedWrite), formattedWrite, std, format, write))
$(TD Converts its arguments according to a format string and writes
the result to an output range.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D write), std, format, write))
$(TD $(REF_ALTTEXT $(D formatValue), formatValue, std, format, write))
$(TD Formats a value of any type according to a format specifier and
writes the result to an output range.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D read), std, format, read))
$(TD $(REF_ALTTEXT $(D formattedRead), formattedRead, std, format, read))
$(TD Reads an input range according to a format string and stores the read
values into its arguments.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D read), std, format, read))
$(TD $(REF_ALTTEXT $(D unformatValue), unformatValue, std, format, read))
$(TD Reads a value from the given input range and converts it according to
a format specifier.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D spec), std, format, spec))
$(TD $(REF_ALTTEXT $(D FormatSpec), FormatSpec, std, format, spec))
$(TD A general handler for format strings.)
)
$(TR
$(TD $(MREF_ALTTEXT $(D spec), std, format, spec))
$(TD $(REF_ALTTEXT $(D singleSpec), singleSpec, std, format, spec))
$(TD Helper function that returns a `FormatSpec` for a single format specifier.)
))
Limitation: This package does not support localization, but
adheres to the rounding mode of the floating point unit, if
available.
$(H3 $(LNAME2 format-strings, Format Strings))
The functions contained in this package use $(I format strings). A
format string describes the layout of another string for reading or
writing purposes. A format string is composed of normal text
interspersed with $(I format specifiers). A format specifier starts
with a percentage sign $(B '%'), optionally followed by one or more
$(I parameters) and ends with a $(I format indicator). A format
indicator may be a simple $(I format character) or a $(I compound
indicator).
$(I Format strings) are composed according to the following grammar:
$(PRE
$(I FormatString):
$(I FormatStringItem) $(I FormatString)
$(I FormatStringItem):
$(I Character)
$(I FormatSpecifier)
$(I FormatSpecifier):
$(B '%') $(I Parameters) $(I FormatIndicator)
$(I FormatIndicator):
$(I FormatCharacter)
$(I CompoundIndicator)
$(I FormatCharacter):
$(I see remark below)
$(I CompoundIndicator):
$(B '$(LPAREN)') $(I FormatString) $(B '%$(RPAREN)')
$(B '$(LPAREN)') $(I FormatString) $(B '%|') $(I Delimiter) $(B '%$(RPAREN)')
$(I Delimiter)
$(I empty)
$(I Character) $(I Delimiter)
$(I Parameters):
$(I Position) $(I Flags) $(I Width) $(I Precision) $(I Separator)
$(I Position):
$(I empty)
$(I Integer) $(B '$')
$(I Integer) $(B ':') $(I Integer) $(B '$')
$(I Integer) $(B ':') $(B '$')
$(I Flags):
$(I empty)
$(I Flag) $(I Flags)
$(I Flag):
$(B '-')|$(B '+')|$(B ' ')|$(B '0')|$(B '#')|$(B '=')
$(I Width):
$(I OptionalPositionalInteger)
$(I Precision):
$(I empty)
$(B '.') $(I OptionalPositionalInteger)
$(I Separator):
$(I empty)
$(B ',') $(I OptionalInteger)
$(B ',') $(I OptionalInteger) $(B '?')
$(I OptionalInteger):
$(I empty)
$(I Integer)
$(B '*')
$(I OptionalPositionalInteger):
$(I OptionalInteger)
$(B '*') $(I Integer) $(B '$')
$(I Character)
$(B '%%')
$(I AnyCharacterExceptPercent)
$(I Integer):
$(I NonZeroDigit) $(I Digits)
$(I Digits):
$(I empty)
$(I Digit) $(I Digits)
$(I NonZeroDigit):
$(B '1')|$(B '2')|$(B '3')|$(B '4')|$(B '5')|$(B '6')|$(B '7')|$(B '8')|$(B '9')
$(I Digit):
$(B '0')|$(B '1')|$(B '2')|$(B '3')|$(B '4')|$(B '5')|$(B '6')|$(B '7')|$(B '8')|$(B '9')
)
Note: $(I FormatCharacter) is unspecified. It can be any character
that has no other purpose in this grammar, but it is
recommended to assign (lower- and uppercase) letters.
Note: The $(I Parameters) of a $(I CompoundIndicator) are currently
limited to a $(B '-') flag.
$(H4 $(LNAME2 format-indicator, Format Indicator))
The $(I format indicator) can either be a single character or an
expression surrounded by $(B '%$(LPAREN)') and $(B '%$(RPAREN)'). It specifies the
basic manner in which a value will be formatted and is the minimum
requirement to format a value.
The following characters can be used as $(I format characters):
$(BOOKTABLE ,
$(TR $(TH FormatCharacter) $(TH Semantics))
$(TR $(TD $(B 's'))
$(TD To be formatted in a human readable format.
Can be used with all types.))
$(TR $(TD $(B 'c'))
$(TD To be formatted as a character.))
$(TR $(TD $(B 'd'))
$(TD To be formatted as a signed decimal integer.))
$(TR $(TD $(B 'u'))
$(TD To be formatted as a decimal image of the underlying bit representation.))
$(TR $(TD $(B 'b'))
$(TD To be formatted as a binary image of the underlying bit representation.))
$(TR $(TD $(B 'o'))
$(TD To be formatted as an octal image of the underlying bit representation.))
$(TR $(TD $(B 'x') / $(B 'X'))
$(TD To be formatted as a hexadecimal image of the underlying bit representation.))
$(TR $(TD $(B 'e') / $(B 'E'))
$(TD To be formatted as a real number in decimal scientific notation.))
$(TR $(TD $(B 'f') / $(B 'F'))
$(TD To be formatted as a real number in decimal natural notation.))
$(TR $(TD $(B 'g') / $(B 'G'))
$(TD To be formatted as a real number in decimal short notation.
Depending on the number, a scientific notation or
a natural notation is used.))
$(TR $(TD $(B 'a') / $(B 'A'))
$(TD To be formatted as a real number in hexadecimal scientific notation.))
$(TR $(TD $(B 'r'))
$(TD To be formatted as raw bytes.
The output may not be printable and depends on endianness.))
)
The $(I compound indicator) can be used to describe compound types
like arrays or structs in more detail. A compound type is enclosed
within $(B '%$(LPAREN)') and $(B '%$(RPAREN)'). The enclosed sub-format string is
applied to individual elements. The trailing portion of the
sub-format string following the specifier for the element is
interpreted as the delimiter, and is therefore omitted following the
last element. The $(B '%|') specifier may be used to explicitly
indicate the start of the delimiter, so that the preceding portion of
the string will be included following the last element.
The $(I format string) inside of the $(I compound indicator) should
contain exactly one $(I format specifier) (two in case of associative
arrays), which specifies the formatting mode of the elements of the
compound type. This $(I format specifier) can be a $(I compound
indicator) itself.
Note: Inside a $(I compound indicator), strings and characters are
escaped automatically. To avoid this behavior, use `"%-$(LPAREN)"`
instead of `"%$(LPAREN)"`.
$(H4 $(LNAME2 flags, Flags))
There are several flags that affect the outcome of the formatting.
$(BOOKTABLE ,
$(TR $(TH Flag) $(TH Semantics))
$(TR $(TD $(B '-'))
$(TD When the formatted result is shorter than the value
given by the width parameter, the output is left
justified. Without the $(B '-') flag, the output remains
right justified.
There are two exceptions where the $(B '-') flag has a
different meaning: (1) with $(B 'r') it denotes to use little
endian and (2) in case of a compound indicator it means that
no special handling of the members is applied.))
$(TR $(TD $(B '='))
$(TD When the formatted result is shorter than the value
given by the width parameter, the output is centered.
If the central position is not possible it is moved slightly
to the right. In this case, if $(B '-') flag is present in
addition to the $(B '=') flag, it is moved slightly to the left.))
$(TR $(TD $(B '+') / $(B ' '))
$(TD Applies to numerical values. By default, positive numbers are not
formatted to include the `+` sign. With one of these two flags present,
positive numbers are preceded by a plus sign or a space.
When both flags are present, a plus sign is used.
In case of $(B 'r'), a big endian format is used.))
$(TR $(TD $(B '0'))
$(TD Is applied to numerical values that are printed right justified.
If the zero flag is present, the space left to the number is
filled with zeros instead of spaces.))
$(TR $(TD $(B '#'))
$(TD Denotes that an alternative output must be used. This depends on the type
to be formatted and the $(I format character) used. See the
sections below for more information.))
)
$(H4 $(LNAME2 width-precision-separator, Width, Precision and Separator))
The $(I width) parameter specifies the minimum width of the result.
The meaning of $(I precision) depends on the format indicator. For
integers it denotes the minimum number of digits printed, for
real numbers it denotes the number of fractional digits and for
strings and compound types it denotes the maximum number of elements
that are included in the output.
A $(I separator) is used for formatting numbers. If it is specified,
the output is divided into chunks of three digits, separated by a $(B
','). The number of digits in a chunk can be given explicitly by
providing a number or a $(B '*') after the $(B ',').
In all three cases the number of digits can be replaced by a $(B
'*'). In this scenario, the next argument is used as the number of
digits. If the argument is a negative number, the $(I precision) and
$(I separator) parameters are considered unspecified. For $(I width),
the absolute value is used and the $(B '-') flag is set.
The $(I separator) can also be followed by a $(B '?'). In that case,
an additional argument is used to specify the symbol that should be
used to separate the chunks.
$(H4 $(LNAME2 position, Position))
By default, the arguments are processed in the provided order. With
the $(I position) parameter it is possible to address arguments
directly. It is also possible to denote a series of arguments with
two numbers separated by $(B ':'), that are all processed in the same
way. The second number can be omitted. In that case the series ends
with the last argument.
It's also possible to use positional arguments for $(I width), $(I
precision) and $(I separator) by adding a number and a $(B
'$(DOLLAR)') after the $(B '*').
$(H4 $(LNAME2 types, Types))
This section describes the result of combining types with format
characters. It is organized in 2 subsections: a list of general
information regarding the formatting of types in the presence of
format characters and a table that contains details for every
available combination of type and format character.
When formatting types, the following rules apply:
$(UL
$(LI If the format character is upper case, the resulting string will
be formatted using upper case letters.)
$(LI The default precision for floating point numbers is 6 digits.)
$(LI Rounding of floating point numbers adheres to the rounding mode
of the floating point unit, if available.)
$(LI The floating point values `NaN` and `Infinity` are formatted as
`nan` and `inf`, possibly preceded by $(B '+') or $(B '-') sign.)
$(LI Formatting reals is only supported for 64 bit reals and 80 bit reals.
All other reals are cast to double before they are formatted. This will
cause the result to be `inf` for very large numbers.)
$(LI Characters and strings formatted with the $(B 's') format character
inside of compound types are surrounded by single and double quotes
and unprintable characters are escaped. To avoid this, a $(B '-')
flag can be specified for the compound specifier
$(LPAREN)e.g. `"%-$(LPAREN)%s%$(RPAREN)"` instead of `"%$(LPAREN)%s%$(RPAREN)"` $(RPAREN).)
$(LI Structs, unions, classes and interfaces are formatted by calling a
`toString` method if available.
See $(MREF_ALTTEXT $(D module std.format.write), std, format, write) for more
details.)
$(LI Only part of these combinations can be used for reading. See
$(MREF_ALTTEXT $(D module std.format.read), std, format, read) for more
detailed information.)
)
This table contains descriptions for every possible combination of
type and format character:
$(BOOKTABLE ,
$(TR $(THMINWIDTH Type) $(THMINWIDTH Format Character) $(TH Formatted as...))
$(TR $(MULTIROW_CELL 1, `null`)
$(TD $(B 's'))
$(TD `null`)
)
$(TR $(MULTIROW_CELL 3, `bool`)
$(TD $(B 's'))
$(TD `false` or `true`)
)
$(TR $(TD $(B 'b'), $(B 'd'), $(B 'o'), $(B 'u'), $(B 'x'), $(B 'X'))
$(TD As the integrals 0 or 1 with the same format character.
$(I Please note, that $(B 'o') and $(B 'x') with $(B '#') flag
might produce unexpected results due to special handling of
the value 0.))
)
$(TR $(TD $(B 'r'))
$(TD `\0` or `\1`)
)
$(TR $(MULTIROW_CELL 4, $(I Integral))
$(TD $(B 's'), $(B 'd'))
$(TD A signed decimal number. The $(B '#') flag is ignored.)
)
$(TR $(TD $(B 'b'), $(B 'o'), $(B 'u'), $(B 'x'), $(B 'X'))
$(TD An unsigned binary, decimal, octal or hexadecimal number.
In case of $(B 'o') and $(B 'x'), the $(B '#') flag
denotes that the number must be preceded by `0` and `0x`, with
the exception of the value 0, where this does not apply. For
$(B 'b') and $(B 'u') the $(B '#') flag has no effect.)
)
$(TR $(TD $(B 'e'), $(B 'E'), $(B 'f'), $(B 'F'), $(B 'g'), $(B 'G'), $(B 'a'), $(B 'A'))
$(TD As a floating point value with the same specifier.
Default precision is large enough to add all digits
of the integral value.
In case of $(B 'a') and $(B 'A'), the integral digit can be
any hexadecimal digit.
)
)
$(TR $(TD $(B 'r'))
$(TD Characters taken directly from the binary representation.)
)
$(TR $(MULTIROW_CELL 5, $(I Floating Point))
$(TD $(B 'e'), $(B 'E'))
$(TD Scientific notation: Exactly one integral digit followed by a dot
and fractional digits, followed by the exponent.
The exponent is formatted as $(B 'e') followed by
a $(B '+') or $(B '-') sign, followed by at least
two digits.
When there are no fractional digits and the $(B '#') flag
is $(I not) present, the dot is omitted.)
)
$(TR $(TD $(B 'f'), $(B 'F'))
$(TD Natural notation: Integral digits followed by a dot and
fractional digits.
When there are no fractional digits and the $(B '#') flag
is $(I not) present, the dot is omitted.
$(I Please note: the difference between $(B 'f') and $(B 'F')
is only visible for `NaN` and `Infinity`.))
)
$(TR $(TD $(B 's'), $(B 'g'), $(B 'G'))
$(TD Short notation: If the absolute value is larger than `10 ^^ precision`
or smaller than `0.0001`, the scientific notation is used.
If not, the natural notation is applied.
In both cases $(I precision) denotes the count of all digits, including
the integral digits. Trailing zeros (including a trailing dot) are removed.
If $(B '#') flag is present, trailing zeros are not removed.)
)
$(TR $(TD $(B 'a'), $(B 'A'))
$(TD Hexadecimal scientific notation: `0x` followed by `1`
(or `0` in case of value zero or denormalized number)
followed by a dot, fractional digits in hexadecimal
notation and an exponent. The exponent is build by `p`,
followed by a sign and the exponent in $(I decimal) notation.
When there are no fractional digits and the $(B '#') flag
is $(I not) present, the dot is omitted.)
)
$(TR $(TD $(B 'r'))
$(TD Characters taken directly from the binary representation.)
)
$(TR $(MULTIROW_CELL 3, $(I Character))
$(TD $(B 's'), $(B 'c'))
$(TD As the character.
Inside of a compound indicator $(B 's') is treated differently: The
character is surrounded by single quotes and non printable
characters are escaped. This can be avoided by preceding
the compound indicator with a $(B '-') flag
$(LPAREN)e.g. `"%-$(LPAREN)%s%$(RPAREN)"`$(RPAREN).)
)
$(TR $(TD $(B 'b'), $(B 'd'), $(B 'o'), $(B 'u'), $(B 'x'), $(B 'X'))
$(TD As the integral that represents the character.)
)
$(TR $(TD $(B 'r'))
$(TD Characters taken directly from the binary representation.)
)
$(TR $(MULTIROW_CELL 3, $(I String))
$(TD $(B 's'))
$(TD The sequence of characters that form the string.
Inside of a compound indicator the string is surrounded by double quotes
and non printable characters are escaped. This can be avoided
by preceding the compound indicator with a $(B '-') flag
$(LPAREN)e.g. `"%-$(LPAREN)%s%$(RPAREN)"`$(RPAREN).)
)
$(TR $(TD $(B 'r'))
$(TD The sequence of characters, each formatted with $(B 'r').)
)
$(TR $(TD compound)
$(TD As an array of characters.)
)
$(TR $(MULTIROW_CELL 3, $(I Array))
$(TD $(B 's'))
$(TD When the elements are characters, the array is formatted as
a string. In all other cases the array is surrounded by square brackets
and the elements are separated by a comma and a space. If the elements
are strings, they are surrounded by double quotes and non
printable characters are escaped.)
)
$(TR $(TD $(B 'r'))
$(TD The sequence of the elements, each formatted with $(B 'r').)
)
$(TR $(TD compound)
$(TD The sequence of the elements, each formatted according to the specifications
given inside of the compound specifier.)
)
$(TR $(MULTIROW_CELL 2, $(I Associative Array))
$(TD $(B 's'))
$(TD As a sequence of the elements in unpredictable order. The output is
surrounded by square brackets. The elements are separated by a
comma and a space. The elements are formatted as `key:value`.)
)
$(TR $(TD compound)
$(TD As a sequence of the elements in unpredictable order. Each element
is formatted according to the specifications given inside of the
compound specifier. The first specifier is used for formatting
the key and the second specifier is used for formatting the value.
The order can be changed with positional arguments. For example
`"%(%2$s (%1$s), %)"` will write the value, followed by the key in
parenthesis.)
)
$(TR $(MULTIROW_CELL 2, $(I Enum))
$(TD $(B 's'))
$(TD The name of the value. If the name is not available, the base value
is used, preceeded by a cast.)
)
$(TR $(TD All, but $(B 's'))
$(TD Enums can be formatted with all format characters that can be used
with the base value. In that case they are formatted like the base value.)
)
$(TR $(MULTIROW_CELL 3, $(I Input Range))
$(TD $(B 's'))
$(TD When the elements of the range are characters, they are written like a string.
In all other cases, the elements are enclosed by square brackets and separated
by a comma and a space.)
)
$(TR $(TD $(B 'r'))
$(TD The sequence of the elements, each formatted with $(B 'r').)
)
$(TR $(TD compound)
$(TD The sequence of the elements, each formatted according to the specifications
given inside of the compound specifier.)
)
$(TR $(MULTIROW_CELL 1, $(I Struct))
$(TD $(B 's'))
$(TD When the struct has neither an applicable `toString`
nor is an input range, it is formatted as follows:
`StructType(field1, field2, ...)`.)
)
$(TR $(MULTIROW_CELL 1, $(I Class))
$(TD $(B 's'))
$(TD When the class has neither an applicable `toString`
nor is an input range, it is formatted as the
fully qualified name of the class.)
)
$(TR $(MULTIROW_CELL 1, $(I Union))
$(TD $(B 's'))
$(TD When the union has neither an applicable `toString`
nor is an input range, it is formatted as its base name.)
)
$(TR $(MULTIROW_CELL 2, $(I Pointer))
$(TD $(B 's'))
$(TD A null pointer is formatted as 'null'. All other pointers are
formatted as hexadecimal numbers with the format character $(B 'X').)
)
$(TR $(TD $(B 'x'), $(B 'X'))
$(TD Formatted as a hexadecimal number.)
)
$(TR $(MULTIROW_CELL 3, $(I SIMD vector))
$(TD $(B 's'))
$(TD The array is surrounded by square brackets
and the elements are separated by a comma and a space.)
)
$(TR $(TD $(B 'r'))
$(TD The sequence of the elements, each formatted with $(B 'r').)
)
$(TR $(TD compound)
$(TD The sequence of the elements, each formatted according to the specifications
given inside of the compound specifier.)
)
$(TR $(MULTIROW_CELL 1, $(I Delegate))
$(TD $(B 's'), $(B 'r'), compound)
$(TD As the `.stringof` of this delegate treated as a string.
$(I Please note: The implementation is currently buggy
and its use is discouraged.))
)
)
Copyright: Copyright The D Language Foundation 2000-2021.
Macros:
SUBREF = $(REF_ALTTEXT $2, $2, std, format, $1)$(NBSP)
MULTIROW_CELL = <td rowspan="$1">$+</td>
THMINWIDTH = <th scope="col" width="20%">$0</th>
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(HTTP walterbright.com, Walter Bright), $(HTTP erdani.com,
Andrei Alexandrescu), and Kenji Hara
Source: $(PHOBOSSRC std/format/package.d)
*/
module std.format;
/// Simple use:
@safe unittest
{
// Easiest way is to use `%s` everywhere:
assert(format("I got %s %s for %s euros.", 30, "eggs", 5.27) == "I got 30 eggs for 5.27 euros.");
// Other format characters provide more control:
assert(format("I got %b %(%X%) for %f euros.", 30, "eggs", 5.27) == "I got 11110 65676773 for 5.270000 euros.");
}
/// Compound specifiers allow formatting arrays and other compound types:
@safe unittest
{
/*
The trailing end of the sub-format string following the specifier for
each item is interpreted as the array delimiter, and is therefore
omitted following the last array item:
*/
assert(format("My items are %(%s %).", [1,2,3]) == "My items are 1 2 3.");
assert(format("My items are %(%s, %).", [1,2,3]) == "My items are 1, 2, 3.");
/*
The "%|" delimiter specifier may be used to indicate where the
delimiter begins, so that the portion of the format string prior to
it will be retained in the last array element:
*/
assert(format("My items are %(-%s-%|, %).", [1,2,3]) == "My items are -1-, -2-, -3-.");
/*
These compound format specifiers may be nested in the case of a
nested array argument:
*/
auto mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
assert(format("%(%(%d %) - %)", mat), "1 2 3 - 4 5 6 - 7 8 9");
assert(format("[%(%(%d %) - %)]", mat), "[1 2 3 - 4 5 6 - 7 8 9]");
assert(format("[%([%(%d %)]%| - %)]", mat), "[1 2 3] - [4 5 6] - [7 8 9]");
/*
Strings and characters are escaped automatically inside compound
format specifiers. To avoid this behavior, use "%-(" instead of "%(":
*/
assert(format("My friends are %s.", ["John", "Nancy"]) == `My friends are ["John", "Nancy"].`);
assert(format("My friends are %(%s, %).", ["John", "Nancy"]) == `My friends are "John", "Nancy".`);
assert(format("My friends are %-(%s, %).", ["John", "Nancy"]) == `My friends are John, Nancy.`);
}
/// Using parameters:
@safe unittest
{
// Flags can be used to influence to outcome:
assert(format("%g != %+#g", 3.14, 3.14) == "3.14 != +3.14000");
// Width and precision help to arrange the formatted result:
assert(format(">%10.2f<", 1234.56789) == "> 1234.57<");
// Numbers can be grouped:
assert(format("%,4d", int.max) == "21,4748,3647");
// It's possible to specify the position of an argument:
assert(format("%3$s %1$s", 3, 17, 5) == "5 3");
}
/// Providing parameters as arguments:
@safe unittest
{
// Width as argument
assert(format(">%*s<", 10, "abc") == "> abc<");
// Precision as argument
assert(format(">%.*f<", 5, 123.2) == ">123.20000<");
// Grouping as argument
assert(format("%,*d", 1, int.max) == "2,1,4,7,4,8,3,6,4,7");
// Grouping separator as argument
assert(format("%,3?d", '_', int.max) == "2_147_483_647");
// All at once
assert(format("%*.*,*?d", 20, 15, 6, '/', int.max) == " 000/002147/483647");
}
public import std.format.read;
public import std.format.spec;
public import std.format.write;
import std.exception : enforce;
import std.range.primitives : isInputRange;
import std.traits : CharTypeOf, isSomeChar, isSomeString, StringTypeOf;
import std.format.internal.write : hasToString;
/**
Signals an issue encountered while formatting.
*/
class FormatException : Exception
{
/// Generic constructor.
@safe @nogc pure nothrow
this()
{
super("format error");
}
/**
Creates a new instance of `FormatException`.
Params:
msg = message of the exception
fn = file name of the file where the exception was created (optional)
ln = line number of the file where the exception was created (optional)
next = for internal use, should always be null (optional)
*/
@safe @nogc pure nothrow
this(string msg, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(msg, fn, ln, next);
}
}
///
@safe unittest
{
import std.exception : assertThrown;
assertThrown!FormatException(format("%d", "foo"));
}
package alias enforceFmt = enforce!FormatException;
// @@@DEPRECATED_[2.107.0]@@@
deprecated("formatElement was accidentally made public and will be removed in 2.107.0")
void formatElement(Writer, T, Char)(auto ref Writer w, T val, scope const ref FormatSpec!Char f)
if (is(StringTypeOf!T) && !hasToString!(T, Char) && !is(T == enum))
{
import std.format.internal.write : fe = formatElement;
fe(w, val, f);
}
// @@@DEPRECATED_[2.107.0]@@@
deprecated("formatElement was accidentally made public and will be removed in 2.107.0")
void formatElement(Writer, T, Char)(auto ref Writer w, T val, scope const ref FormatSpec!Char f)
if (is(CharTypeOf!T) && !is(T == enum))
{
import std.format.internal.write : fe = formatElement;
fe(w, val, f);
}
// @@@DEPRECATED_[2.107.0]@@@
deprecated("formatElement was accidentally made public and will be removed in 2.107.0")
void formatElement(Writer, T, Char)(auto ref Writer w, auto ref T val, scope const ref FormatSpec!Char f)
if ((!is(StringTypeOf!T) || hasToString!(T, Char)) && !is(CharTypeOf!T) || is(T == enum))
{
import std.format.internal.write : fe = formatElement;
fe(w, val, f);
}
// Like NullSink, but toString() isn't even called at all. Used to test the format string.
package struct NoOpSink
{
void put(E)(scope const E) pure @safe @nogc nothrow {}
}
// @@@DEPRECATED_[2.107.0]@@@
deprecated("unformatElement was accidentally made public and will be removed in 2.107.0")
T unformatElement(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range)
{
import std.format.internal.read : ue = unformatElement;
return ue(input, spec);
}
// Used to check format strings are compatible with argument types
package(std) enum checkFormatException(alias fmt, Args...) =
{
import std.conv : text;
try
{
auto n = .formattedWrite(NoOpSink(), fmt, Args.init);
enforceFmt(n == Args.length, text("Orphan format arguments: args[", n, "..", Args.length, "]"));
}
catch (Exception e)
return e.msg;
return null;
}();
/**
Converts its arguments according to a format string into a string.
The second version of `format` takes the format string as template
argument. In this case, it is checked for consistency at
compile-time and produces slightly faster code, because the length of
the output buffer can be estimated in advance.
Params:
fmt = a $(MREF_ALTTEXT format string, std,format)
args = a variadic list of arguments to be formatted
Char = character type of `fmt`
Args = a variadic list of types of the arguments
Returns:
The formatted string.
Throws:
A $(LREF FormatException) if formatting did not succeed.
See_Also:
$(LREF sformat) for a variant, that tries to avoid garbage collection.
*/
immutable(Char)[] format(Char, Args...)(in Char[] fmt, Args args)
if (isSomeChar!Char)
{
import std.array : appender;
auto w = appender!(immutable(Char)[]);
auto n = formattedWrite(w, fmt, args);
version (all)
{
// In the future, this check will be removed to increase consistency
// with formattedWrite
import std.conv : text;
enforceFmt(n == args.length, text("Orphan format arguments: args[", n, "..", args.length, "]"));
}
return w.data;
}
///
@safe pure unittest
{
assert(format("Here are %d %s.", 3, "apples") == "Here are 3 apples.");
assert("Increase: %7.2f %%".format(17.4285) == "Increase: 17.43 %");
}
@safe pure unittest
{
import std.exception : assertCTFEable, assertThrown;
assertCTFEable!(
{
assert(format("foo") == "foo");
assert(format("foo%%") == "foo%");
assert(format("foo%s", 'C') == "fooC");
assert(format("%s foo", "bar") == "bar foo");
assert(format("%s foo %s", "bar", "abc") == "bar foo abc");
assert(format("foo %d", -123) == "foo -123");
assert(format("foo %d", 123) == "foo 123");
assertThrown!FormatException(format("foo %s"));
assertThrown!FormatException(format("foo %s", 123, 456));
assert(format("hel%slo%s%s%s", "world", -138, 'c', true) == "helworldlo-138ctrue");
});
assert(is(typeof(format("happy")) == string));
assert(is(typeof(format("happy"w)) == wstring));
assert(is(typeof(format("happy"d)) == dstring));
}
// https://issues.dlang.org/show_bug.cgi?id=16661
@safe pure unittest
{
assert(format("%.2f"d, 0.4) == "0.40");
assert("%02d"d.format(1) == "01"d);
}
@safe unittest
{
int i;
string s;
s = format("hello world! %s %s %s%s%s", true, 57, 1_000_000_000, 'x', " foo");
assert(s == "hello world! true 57 1000000000x foo");
s = format("%s %A %s", 1.67, -1.28, float.nan);
assert(s == "1.67 -0X1.47AE147AE147BP+0 nan", s);
s = format("%x %X", 0x1234AF, 0xAFAFAFAF);
assert(s == "1234af AFAFAFAF");
s = format("%b %o", 0x1234AF, 0xAFAFAFAF);
assert(s == "100100011010010101111 25753727657");
s = format("%d %s", 0x1234AF, 0xAFAFAFAF);
assert(s == "1193135 2947526575");
}
@safe unittest
{
import std.conv : octal;
string s;
int i;
s = format("%#06.*f", 2, 12.345);
assert(s == "012.35");
s = format("%#0*.*f", 6, 2, 12.345);
assert(s == "012.35");
s = format("%7.4g:", 12.678);
assert(s == " 12.68:");
s = format("%7.4g:", 12.678L);
assert(s == " 12.68:");
s = format("%04f|%05d|%#05x|%#5x", -4.0, -10, 1, 1);
assert(s == "-4.000000|-0010|0x001| 0x1");
i = -10;
s = format("%d|%3d|%03d|%1d|%01.4f", i, i, i, i, cast(double) i);
assert(s == "-10|-10|-10|-10|-10.0000");
i = -5;
s = format("%d|%3d|%03d|%1d|%01.4f", i, i, i, i, cast(double) i);
assert(s == "-5| -5|-05|-5|-5.0000");
i = 0;
s = format("%d|%3d|%03d|%1d|%01.4f", i, i, i, i, cast(double) i);
assert(s == "0| 0|000|0|0.0000");
i = 5;
s = format("%d|%3d|%03d|%1d|%01.4f", i, i, i, i, cast(double) i);
assert(s == "5| 5|005|5|5.0000");
i = 10;
s = format("%d|%3d|%03d|%1d|%01.4f", i, i, i, i, cast(double) i);
assert(s == "10| 10|010|10|10.0000");
s = format("%.0d", 0);
assert(s == "0");
s = format("%.g", .34);
assert(s == "0.3");
s = format("%.0g", .34);
assert(s == "0.3");
s = format("%.2g", .34);
assert(s == "0.34");
s = format("%0.0008f", 1e-08);
assert(s == "0.00000001");
s = format("%0.0008f", 1e-05);
assert(s == "0.00001000");
s = "helloworld";
string r;
r = format("%.2s", s[0 .. 5]);
assert(r == "he");
r = format("%.20s", s[0 .. 5]);
assert(r == "hello");
r = format("%8s", s[0 .. 5]);
assert(r == " hello");
byte[] arrbyte = new byte[4];
arrbyte[0] = 100;
arrbyte[1] = -99;
arrbyte[3] = 0;
r = format("%s", arrbyte);
assert(r == "[100, -99, 0, 0]");
ubyte[] arrubyte = new ubyte[4];
arrubyte[0] = 100;
arrubyte[1] = 200;
arrubyte[3] = 0;
r = format("%s", arrubyte);
assert(r == "[100, 200, 0, 0]");
short[] arrshort = new short[4];
arrshort[0] = 100;
arrshort[1] = -999;
arrshort[3] = 0;
r = format("%s", arrshort);
assert(r == "[100, -999, 0, 0]");
ushort[] arrushort = new ushort[4];
arrushort[0] = 100;
arrushort[1] = 20_000;
arrushort[3] = 0;
r = format("%s", arrushort);
assert(r == "[100, 20000, 0, 0]");
int[] arrint = new int[4];
arrint[0] = 100;
arrint[1] = -999;
arrint[3] = 0;
r = format("%s", arrint);
assert(r == "[100, -999, 0, 0]");
long[] arrlong = new long[4];
arrlong[0] = 100;
arrlong[1] = -999;
arrlong[3] = 0;
r = format("%s", arrlong);
assert(r == "[100, -999, 0, 0]");
ulong[] arrulong = new ulong[4];
arrulong[0] = 100;
arrulong[1] = 999;
arrulong[3] = 0;
r = format("%s", arrulong);
assert(r == "[100, 999, 0, 0]");
string[] arr2 = new string[4];
arr2[0] = "hello";
arr2[1] = "world";
arr2[3] = "foo";
r = format("%s", arr2);
assert(r == `["hello", "world", "", "foo"]`);
r = format("%.8d", 7);
assert(r == "00000007");
r = format("%.8x", 10);
assert(r == "0000000a");
r = format("%-3d", 7);
assert(r == "7 ");
r = format("%-1*d", 4, 3);
assert(r == "3 ");
r = format("%*d", -3, 7);
assert(r == "7 ");
r = format("%.*d", -3, 7);
assert(r == "7");
r = format("%-1.*f", 2, 3.1415);
assert(r == "3.14");
r = format("abc"c);
assert(r == "abc");
//format() returns the same type as inputted.
wstring wr;