Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.111.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.format

This package provides string formatting functionality usingprintf style format strings.
SubmoduleFunction NameDescription
packageformatConverts its arguments according to a format string into a string.
packagesformatConverts its arguments according to a format string into a buffer.
packageFormatExceptionSignals a problem while formatting.
writeformattedWriteConverts its arguments according to a format string and writes the result to an output range.
writeformatValueFormats a value of any type according to a format specifier and writes the result to an output range.
readformattedReadReads an input range according to a format string and stores the read values into its arguments.
readunformatValueReads a value from the given input range and converts it according to a format specifier.
specFormatSpecA general handler for format strings.
specsingleSpecHelper function that returns aFormatSpec for a single format specifier.

LimitationThis package does not support localization, but adheres to the rounding mode of the floating point unit, if available.

Format Strings

The functions contained in this package useformat strings. Aformat string describes the layout of another string for reading orwriting purposes. A format string is composed of normal textinterspersed withformat specifiers. A format specifier startswith a percentage sign'%', optionally followed by one or moreparameters and ends with aformat indicator. A formatindicator may be a simpleformat character or acompoundindicator.
Format strings are composed according to the following grammar:
FormatString:FormatStringItemFormatStringFormatStringItem:CharacterFormatSpecifierFormatSpecifier:'%'ParametersFormatIndicator
FormatIndicator:FormatCharacterCompoundIndicatorFormatCharacter:see remark belowCompoundIndicator:'('FormatString'%)''('FormatString'%|'Delimiter'%)'DelimiteremptyCharacterDelimiter
Parameters:PositionFlagsWidthPrecisionSeparatorPosition:emptyInteger'$'Integer':'Integer'$'Integer':''$'Flags:emptyFlagFlagsFlag:'-'|'+'|' '|'0'|'#'|'='Width:OptionalPositionalIntegerPrecision:empty'.'OptionalPositionalIntegerSeparator:empty','OptionalInteger','OptionalInteger'?'OptionalInteger:emptyInteger'*'OptionalPositionalInteger:OptionalInteger'*'Integer'$'
Character'%%'AnyCharacterExceptPercentInteger:NonZeroDigitDigitsDigits:emptyDigitDigitsNonZeroDigit:'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'Digit:'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'

NoteFormatCharacter is unspecified. It can be any characterthat has no other purpose in this grammar, but it isrecommended to assign (lower- and uppercase) letters.

NoteTheParameters of aCompoundIndicator are currentlylimited to a'-' flag.

Format Indicator

Theformat indicator can either be a single character or anexpression surrounded by'%(' and'%)'. It specifies thebasic manner in which a value will be formatted and is the minimumrequirement to format a value.
The following characters can be used asformat characters:
FormatCharacterSemantics
's'To be formatted in a human readable format. Can be used with all types.
'c'To be formatted as a character.
'd'To be formatted as a signed decimal integer.
'u'To be formatted as a decimal image of the underlying bit representation.
'b'To be formatted as a binary image of the underlying bit representation.
'o'To be formatted as an octal image of the underlying bit representation.
'x' /'X'To be formatted as a hexadecimal image of the underlying bit representation.
'e' /'E'To be formatted as a real number in decimal scientific notation.
'f' /'F'To be formatted as a real number in decimal natural notation.
'g' /'G'To be formatted as a real number in decimal short notation. Depending on the number, a scientific notation or a natural notation is used.
'a' /'A'To be formatted as a real number in hexadecimal scientific notation.
'r'To be formatted as raw bytes. The output may not be printable and depends on endianness.
Thecompound indicator can be used to describe compound typeslike arrays or structs in more detail. A compound type is enclosedwithin'%(' and'%)'. The enclosed sub-format string isapplied to individual elements. The trailing portion of thesub-format string following the specifier for the element isinterpreted as the delimiter, and is therefore omitted following thelast element. The'%|' specifier may be used to explicitlyindicate the start of the delimiter, so that the preceding portion ofthe string will be included following the last element.
Theformat string inside of thecompound indicator shouldcontain exactly oneformat specifier (two in case of associativearrays), which specifies the formatting mode of the elements of thecompound type. Thisformat specifier can be acompoundindicator itself.

NoteInside acompound indicator, strings and characters areescaped automatically. To avoid this behavior, use"%-("instead of"%(".

Flags

There are several flags that affect the outcome of the formatting.
FlagSemantics
'-'When the formatted result is shorter than the value given by the width parameter, the output is left justified. Without the'-' flag, the output remains right justified.
There are two exceptions where the'-' flag has a different meaning: (1) with'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.
'='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'-' flag is present in addition to the'=' flag, it is moved slightly to the left.
'+' / ' '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'r', a big endian format is used.
'0'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.
'#'Denotes that an alternative output must be used. This depends on the type to be formatted and theformat character used. See the sections below for more information.

Width, Precision and Separator

Thewidth parameter specifies the minimum width of the result.
The meaning ofprecision depends on the format indicator. Forintegers it denotes the minimum number of digits printed, forreal numbers it denotes the number of fractional digits and forstrings and compound types it denotes the maximum number of elementsthat are included in the output.
Aseparator is used for formatting numbers. If it is specified,the output is divided into chunks of three digits, separated by a','. The number of digits in a chunk can be given explicitly byproviding a number or a'*' after the','.
In all three cases the number of digits can be replaced by a'*'. In this scenario, the next argument is used as the number ofdigits. If the argument is a negative number, theprecision andseparator parameters are considered unspecified. Forwidth,the absolute value is used and the'-' flag is set.
Theseparator can also be followed by a'?'. In that case,an additional argument is used to specify the symbol that should beused to separate the chunks.

Position

By default, the arguments are processed in the provided order. Withtheposition parameter it is possible to address argumentsdirectly. It is also possible to denote a series of arguments withtwo numbers separated by':', that are all processed in the sameway. The second number can be omitted. In that case the series endswith the last argument.
It's also possible to use positional arguments forwidth,precision andseparator by adding a number and a'$' after the'*'.

Types

This section describes the result of combining types with formatcharacters. It is organized in 2 subsections: a list of generalinformation regarding the formatting of types in the presence offormat characters and a table that contains details for everyavailable combination of type and format character.
When formatting types, the following rules apply:
  • If the format character is upper case, the resulting string will be formatted using upper case letters.
  • The default precision for floating point numbers is 6 digits.
  • Rounding of floating point numbers adheres to the rounding mode of the floating point unit, if available.
  • The floating point valuesNaN andInfinity are formatted asnan andinf, possibly preceded by'+' or'-' sign.
  • 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 beinf for very large numbers.
  • Characters and strings formatted with the's' format character inside of compound types are surrounded by single and double quotes and unprintable characters are escaped. To avoid this, a'-' flag can be specified for the compound specifier (e.g."%-(%s%)" instead of"%(%s%)" ).
  • Structs, unions, classes and interfaces are formatted by calling atoString method if available. Seemodule std.format.write for more details.
  • Only part of these combinations can be used for reading. Seemodule std.format.read for more detailed information.
This table contains descriptions for every possible combination oftype and format character:
TypeFormat CharacterFormatted as...
null's'null
bool's'false ortrue
'b','d','o','u','x','X'As the integrals 0 or 1 with the same format character.
Please note, that'o' and'x' with'#' flag might produce unexpected results due to special handling of the value 0.
'r'\0 or\1
Integral's','d'A signed decimal number. The'#' flag is ignored.
'b','o','u','x','X'An unsigned binary, decimal, octal or hexadecimal number.
In case of'o' and'x', the'#' flag denotes that the number must be preceded by0 and0x, with the exception of the value 0, where this does not apply. For'b' and'u' the'#' flag has no effect.
'e','E','f','F','g','G','a','A'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'a' and'A', the integral digit can be any hexadecimal digit.
'r'Characters taken directly from the binary representation.
Floating Point'e','E'Scientific notation: Exactly one integral digit followed by a dot and fractional digits, followed by the exponent. The exponent is formatted as'e' followed by a'+' or'-' sign, followed by at least two digits.
When there are no fractional digits and the'#' flag isnot present, the dot is omitted.
'f','F'Natural notation: Integral digits followed by a dot and fractional digits.
When there are no fractional digits and the'#' flag isnot present, the dot is omitted.
Please note: the difference between'f' and'F' is only visible forNaN andInfinity.
's','g','G'Short notation: If the absolute value is larger than10 ^^ precision or smaller than0.0001, the scientific notation is used. If not, the natural notation is applied.
In both casesprecision denotes the count of all digits, including the integral digits. Trailing zeros (including a trailing dot) are removed.
If'#' flag is present, trailing zeros are not removed.
'a','A'Hexadecimal scientific notation:0x followed by1 (or0 in case of value zero or denormalized number) followed by a dot, fractional digits in hexadecimal notation and an exponent. The exponent is build byp, followed by a sign and the exponent indecimal notation.
When there are no fractional digits and the'#' flag isnot present, the dot is omitted.
'r'Characters taken directly from the binary representation.
Character's','c'As the character.
Inside of a compound indicator'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'-' flag (e.g."%-(%s%)").
'b','d','o','u','x','X'As the integral that represents the character.
'r'Characters taken directly from the binary representation.
String's'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'-' flag (e.g."%-(%s%)").
'r'The sequence of characters, each formatted with'r'.
compoundAs an array of characters.
Array's'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.
'r'The sequence of the elements, each formatted with'r'.
compoundThe sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Associative Array's'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 askey:value.
compoundAs 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.
Enum's'The name of the value. If the name is not available, the base value is used, preceeded by a cast.
All, but's'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.
Input Range's'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.
'r'The sequence of the elements, each formatted with'r'.
compoundThe sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Struct's'When the struct has neither an applicabletoString nor is an input range, it is formatted as follows:StructType(field1, field2, ...).
Class's'When the class has neither an applicabletoString nor is an input range, it is formatted as the fully qualified name of the class.
Union's'When the union has neither an applicabletoString nor is an input range, it is formatted as its base name.
Pointer's'A null pointer is formatted as 'null'. All other pointers are formatted as hexadecimal numbers with the format character'X'.
'x','X'Formatted as a hexadecimal number.
SIMD vector's'The array is surrounded by square brackets and the elements are separated by a comma and a space.
'r'The sequence of the elements, each formatted with'r'.
compoundThe sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Delegate's','r', compoundAs the.stringof of this delegate treated as a string.
Please note: The implementation is currently buggy and its use is discouraged.

License:
Boost License 1.0.
Authors:
Walter Bright,Andrei Alexandrescu, and Kenji Hara

Sourcestd/format/package.d

Examples:
Simple use:
// Easiest way is to use `%s` everywhere:// "I got 30 eggs for 5.27 euros."writeln(format("I got %s %s for %s euros.", 30,"eggs", 5.27));// Other format characters provide more control:// "I got 11110 65676773 for 5.270000 euros."writeln(format("I got %b %(%X%) for %f euros.", 30,"eggs", 5.27));
Examples:
Compound specifiers allow formatting arrays and other compound types:
/*The trailing end of the sub-format string following the specifier foreach item is interpreted as the array delimiter, and is thereforeomitted following the last array item: */    writeln(format("My items are %(%s %).", [1, 2, 3]));// "My items are 1 2 3."    writeln(format("My items are %(%s, %).", [1, 2, 3]));// "My items are 1, 2, 3."/*The "%|" delimiter specifier may be used to indicate where thedelimiter begins, so that the portion of the format string prior toit will be retained in the last array element: */    writeln(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 anested 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 compoundformat specifiers. To avoid this behavior, use "%-(" instead of "%(": */// `My friends are ["John", "Nancy"].`    writeln(format("My friends are %s.", ["John","Nancy"]));// `My friends are "John", "Nancy".`    writeln(format("My friends are %(%s, %).", ["John","Nancy"]));// `My friends are John, Nancy.`    writeln(format("My friends are %-(%s, %).", ["John","Nancy"]));
Examples:
Using parameters:
// Flags can be used to influence to outcome:writeln(format("%g != %+#g", 3.14, 3.14));// "3.14 != +3.14000"// Width and precision help to arrange the formatted result:writeln(format(">%10.2f<", 1234.56789));// ">   1234.57<"// Numbers can be grouped:writeln(format("%,4d",int.max));// "21,4748,3647"// It's possible to specify the position of an argument:writeln(format("%3$s %1$s", 3, 17, 5));// "5 3"
Examples:
Providing parameters as arguments:
// Width as argumentwriteln(format(">%*s<", 10,"abc"));// ">       abc<"// Precision as argumentwriteln(format(">%.*f<", 5, 123.2));// ">123.20000<"// Grouping as argumentwriteln(format("%,*d", 1,int.max));// "2,1,4,7,4,8,3,6,4,7"// Grouping separator as argumentwriteln(format("%,3?d", '_',int.max));// "2_147_483_647"// All at oncewriteln(format("%*.*,*?d", 20, 15, 6, '/',int.max));// "   000/002147/483647"
classFormatException:object.Exception;
Signals an issue encountered while formatting.
Examples:
import std.exception : assertThrown;assertThrown!FormatException(format("%d","foo"));
pure nothrow @nogc @safe this();
Generic constructor.
pure nothrow @nogc @safe this(stringmsg, stringfn = __FILE__, size_tln = __LINE__, Throwablenext = null);
Creates a new instance ofFormatException.
Parameters:
stringmsgmessage of the exception
stringfnfile name of the file where the exception was created (optional)
size_tlnline number of the file where the exception was created (optional)
Throwablenextfor internal use, should always be null (optional)
immutable(Char)[]format(Char, Args...)(in Char[]fmt, Argsargs)
if (isSomeChar!Char);

typeof(fmt)format(alias fmt, Args...)(Argsargs)
if (isSomeString!(typeof(fmt)));
Converts its arguments according to a format string into a string.
The second version offormat takes the format string as templateargument. In this case, it is checked for consistency atcompile-time and produces slightly faster code, because the length ofthe output buffer can be estimated in advance.
Parameters:
Char[]fmtaformat string
Argsargsa variadic list of arguments to be formatted
Charcharacter type offmt
Argsa variadic list of types of the arguments
Returns:
The formatted string.
Throws:
AFormatException if formatting did not succeed.
See Also:
sformat for a variant, that tries to avoid garbage collection.
Examples:
writeln(format("Here are %d %s.", 3,"apples"));// "Here are 3 apples."writeln("Increase: %7.2f %%".format(17.4285));// "Increase:   17.43 %"
Examples:
The format string can be checked at compile-time:
auto s =format!"%s is %s"("Pi", 3.14);writeln(s);// "Pi is 3.14"// This line doesn't compile, because 3.14 cannot be formatted with %d:// s = format!"%s is %d"("Pi", 3.14);
char[]sformat(Char, Args...)(return scope char[]buf, scope const(Char)[]fmt, Argsargs);

char[]sformat(alias fmt, Args...)(char[]buf, Argsargs)
if (isSomeString!(typeof(fmt)));
Converts its arguments according to a format string into a buffer.The buffer has to be large enough to hold the formatted string.
The second version ofsformat takes the format string as a templateargument. In this case, it is checked for consistency atcompile-time.
Parameters:
char[]bufthe buffer where the formatted string should go
const(Char)[]fmtaformat string
Argsargsa variadic list of arguments to be formatted
Charcharacter type offmt
Argsa variadic list of types of the arguments
Returns:
A slice ofbuf containing the formatted string.
Throws:
ARangeError ifbuf isn't large enough to hold the formatted string and aFormatException if formatting did not succeed.

NoteIn theory this function should be@nogc. But with the current implementation there are some cases where allocations occur:

  • An exception is thrown.
  • A customtoString function of a compound type allocates.

Examples:
char[20]buf;writeln(sformat(buf[],"Here are %d %s.", 3,"apples"));// "Here are 3 apples."writeln(buf[].sformat("Increase: %7.2f %%", 17.4285));// "Increase:   17.43 %"
Examples:
The format string can be checked at compile-time:
char[20]buf;writeln(sformat!"Here are %d %s."(buf[], 3,"apples"));// "Here are 3 apples."// This line doesn't compile, because 3.14 cannot be formatted with %d:// writeln(sformat!"Here are %d %s."(buf[], 3.14, "apples"));
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Fri Oct 10 22:10:41 2025

[8]ページ先頭

©2009-2025 Movatter.jp