Report a bug
If you spot a problem with this page, click here to create a GitHub 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 using
a local clone.
mir.format
@nogc Formatting Utilities
License:
Authors:
Ilia Ki
- enum
StringStyle: int; - Scalar styles.
none- Uninitialized style
longMultiLine- Literal block style, |
longSingleLine- Folded block style, >
plain- Plain scalar
asSingleQuoted- Single quoted scalar
asEscapedString- Double quoted scalar
- enum
CollectionStyle: int; - Collection stylesExamples:mir.conv: to extension.
import mir.conv: to; import mir.small_string; alias S = SmallString!32; // Floating-point numbers are formatted to // the shortest precise exponential notation. assert(123.0.to!S == "123.0"); assert(123.to!(immutable S) == "123"); assert(true.to!S == "true"); assert(true.to!string == "true"); assert((cast(S)"str")[] == "str");
Examples:mir.conv: to extension.import mir.conv: to; import mir.small_string; alias S = SmallString!32; auto str = S("str"); assert(str.to!(const(char)[]) == "str"); // GC allocated result assert(str.to!(char[]) == "str"); // GC allocated result
Examples:dittoimport mir.conv: to; import mir.small_string; alias S = SmallString!32; // Floating-point numbers are formatted to // the shortest precise exponential notation. assert(123.0.to!string == "123.0"); assert(123.to!(char[]) == "123"); assert(S("str").to!string == "str"); // GC allocated result
none- Uninitialized style
block- Block style
flow- Flow style
- string
text(string separator = "", Args...)(auto ref scope const Argsargs)
if (Args.length > 0); - Concatenated string resultsExamples:
const i = 100; assert(text("str ", true, " ", i, " ", 124.1) == "str true 100 124.1", text("str ", true, " ", 100, " ", 124.1)); assert(text!" "("str", true, 100, 124.1, null) == "str true 100 124.1 null"); assert(text(null) == "null", text(null));
- struct
GetData; - enum GetData
getData; - struct
StringBuf(C, uint scopeSize = 256) if (is(C == char) || is(C == wchar) || is(C == dchar));
pure nothrow @nogc @property @trusted autostringBuf(C = char, uint scopeSize = 256)()
if (is(C == char) || is(C == wchar) || is(C == dchar)); -
- ScopedBuffer!(C, scopeSize)
buffer;
- template
StreamFormatOp(C) - Examples:
auto name = "D"; auto ver = 2.0; assert(stringBuf << "Hi " << name << ver << "!\n" << getData == "Hi D2.0!\n");
Examples:auto name = "D"w; auto ver = 2; assert(stringBuf!wchar << "Hi "w << name << ver << "!\n"w << getData == "Hi D2!\n"w);
Examples:auto name = "D"d; auto ver = 2UL; assert(stringBuf!dchar << "Hi "d << name << ver << "!\n"d << getData == "Hi D2!\n"d);
- ref typeof(this)
opBinary(string op : "<<", T)(ref scope const Tc) scope; - ref typeof(this)
opBinary(string op : "<<", T)(scope const Tc) scope;
const(C)[]opBinary(string op : "<<", T : GetData)(scope const Tc) scope;
- struct
NumericSpec; - Mir's numeric format specification
Note the specification isn't complete an may be extended in the future.
- enum
Format: int; -
human- Human-frindly precise output.Examples:0.000001, 600000.0, but 1e-7 and 6e7.
exponent- Precise output with explicit exponent.Examples:1e-6, 6e6, 1.23456789e-100.
- Format
format; - char
separatorChar; - Default valus is '\0' (no separators)
- char
exponentChar; - Defaults to 'e'
- bool
plus; - Adds '+' to positive numbers and +0.
- ubyte
separatorCount; - Separator count
- enum NumericSpec
exponent; - Precise output with explicit exponent.Examples:1e-6, 6e6, 1.23456789e-100.
- enum NumericSpec
human; - Human-frindly precise output.
- struct
FormatSpec; - C's compatible format specifier.
- bool
dash; - bool
plus; - bool
space; - bool
hash; - bool
zero; - char
format; - char
separator; - ubyte
unitSize; - int
width; - int
precision;
- enum
SwitchLU: bool; -
lowerupper
- struct
FormattedFloating(T) if (is(T == float) || is(T == double) || is(T == real));
FormattedFloating!TwithFormat(T)(const Tvalue, FormatSpecspec); - Wrapper to format floating point numbers using C's library.
- T
value; - FormatSpec
spec; - const void
toString(C = char, W)(ref scope Ww) scope
if (isSomeChar!C);
- struct
HexAddress(T) if (isUnsigned!T && !is(T == enum));
HexAddress!ThexAddress(T)(const Tvalue, SwitchLUswitchLU= SwitchLU.upper)
if (isUnsigned!T && !is(T == enum)); -
- T
value; - SwitchLU
switchLU; - const void
toString(C = char, W)(ref scope Ww) scope
if (isSomeChar!C);
- enum
EscapeFormat: int; - Escaped string formats
json- JSON escaped string format
ionClob- Amzn Ion CLOB format
ionSymbol- Amzn Ion symbol format
ion- Amzn Ion string format
- void
printEscaped(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(ref scope Ww, scope const(C)[]str)
if (isOutputRange!(W, C)); - Examples:
import mir.format: stringBuf; auto w = stringBuf; w.printEscaped("Hi \a\v\0\f\t\b \\\r\n" ~ `"@nogc"`); assert(w.data == `Hi \a\v\0\f\t\b \\\r\n\"@nogc\"`); w.reset; w.printEscaped("\x03"); assert(w.data == `\x03`);
- void
printReplaced(C, W)(ref scope Ww, scope const(C)[]str, Cc, scope const(C)[]to); - Examples:
import mir.test: should; auto csv = stringBuf; csv.put('"'); csv.printReplaced(`some string with " double quotes "!`, '"', `""`); csv.put('"'); csv.data.should == `"some string with "" double quotes ""!"`;
- void
put_xXX(C = char, W)(ref scope Ww, charc)
if (isSomeChar!C); - Decodes char
cto the form u00XX, where XX is 2 hexadecimal characters. - void
put_uXXXX(C = char, W)(ref scope Ww, charc)
if (isSomeChar!C); - Decodes char
cto the form \u00XX, where XX is 2 hexadecimal characters. - void
put_uXXXX(C = char, W)(ref scope Ww, ushortc)
if (isSomeChar!C); - Decodes ushort
cto the form \uXXXX, where XXXX is 2 hexadecimal characters. - void
put_UXXXXXXXX(C = char, W)(ref scope Ww, uintc)
if (isSomeChar!C); - Decodes uint
cto the form \UXXXXXXXX, where XXXXXXXX is 2 hexadecimal characters. - void
printElement(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(ref scope Ww, scope const(C)[]c)
if (isSomeChar!C); - void
printElement(C = char, EscapeFormat escapeFormat = EscapeFormat.ion, W, T)(ref scope Ww, auto ref scope const Tc)
if (!isSomeString!T); - void
print(C = char, W, Args...)(ref scope Ww, auto ref scope const Argsargs)
if (isSomeChar!C && (Args.length > 1)); - Multiargument overload.
- @nogc void
print(C = char, W, T)(ref scope Ww, scope const Tc)
if (isSomeChar!C && is(T == enum)); - Prints enumsExamples:
enum Flag { no, yes, } import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print(Flag.yes); assert(w.data == "yes");
- void
print(C = char, W)(ref scope Ww, boolc)
if (isSomeChar!C); - Prints booleanExamples:
import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print(true); assert(w.data == `true`); w.reset; w.print(false); assert(w.data == `false`);
- void
print(C = char, W, V, K)(ref scope Ww, scope const V[K]c)
if (isSomeChar!C); - Prints associative arrayExamples:
import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print(["a": 1, "b": 2]); assert(w.data == `["a": 1, "b": 2]` || w.data == `["b": 2, "a": 1]`);
- void
print(C = char, W, V)(ref scope Ww, const Vc)
if (is(V == typeof(null))); - Prints nullExamples:
import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print(null); assert(w.data == `null`);
- void
printArray(C = char, W, T)(ref scope Ww, scope const(T)[]c, scope const(C)[]lb= "[", scope const(C)[]rb= "]", scope const(C)[]sep= ", ")
if (isSomeChar!C && !isSomeChar!T);
voidprint(C = char, W, T)(ref scope Ww, scope const(T)[]c)
if (isSomeChar!C && !isSomeChar!T); - Prints arrayExamples:
import mir.appender: scopedBuffer; auto w = scopedBuffer!char; string[2] array = ["a\na", "b"]; w.print(array[]); assert(w.data == `["a\na", "b"]`);
- void
printHexArray(C = char, W, T)(ref scope Ww, scope const(T)[]c, scope const(C)[]lb= "", scope const(C)[]rb= "", scope const(C)[]sep= " ")
if (isSomeChar!C && !isSomeChar!T && isUnsigned!T); - Prints array as hex valuesExamples:
import mir.test; import mir.appender: scopedBuffer; auto w = scopedBuffer!char; ubyte[2] array = [0x34, 0x32]; w.printHexArray(array[]); w.data.should == `34 32`;
- void
print(C = char, W)(ref scope Ww, charc)
if (isSomeChar!C); - Prints escaped character in the form '
c'.Examples:import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print('\n'); w.print('\''); w.print('a'); w.print('\xF4'); assert(w.data == `'\n''\'''a''\xF4'`);
- void
print(C = char, W)(ref scope Ww, dcharc)
if (isSomeChar!C); - Prints escaped character in the form '
c'.Examples:import mir.appender: scopedBuffer; auto w = scopedBuffer!char; w.print('щ'); w.print('\U0010FFFE'); assert(w.data == `'щ''\U0010FFFE'`);
- void
print(C = char, W)(ref scope Ww, scope const(C)[]c)
if (isSomeChar!C); - Prints some string
- void
print(C = char, W, I)(ref scope Ww, const Ic)
if (isSomeChar!C && isIntegral!I && !is(I == enum)); - Prints integers
- void
print(C = char, W, T)(ref scope Ww, const Tc, NumericSpecspec= NumericSpec.init)
if (isSomeChar!C && is(T == float) || is(T == double) || is(T == real)); - Prints floating point numbersExamples:Human friendly precise output (default)
auto spec = NumericSpec.human; auto buffer = stringBuf; void check(double num, string value) { buffer.print(num, spec); assert(buffer.data == value, value); buffer.reset; } check(-0.0, "-0.0"); check(0.0, "0.0"); check(-0.01, "-0.01"); check(0.0125, "0.0125"); check(0.000003, "0.000003"); check(-3e-7, "-3e-7"); check(123456.0, "123456.0"); check(123456.1, "123456.1"); check(12.3456, "12.3456"); check(-0.123456, "-0.123456"); check(0.1234567, "0.1234567"); check(0.01234567, "0.01234567"); check(0.001234567, "0.001234567"); check(1.234567e-4, "1.234567e-4"); check(-1234567.0, "-1.234567e+6"); check(123456.7890123, "123456.7890123"); check(1234567.890123, "1.234567890123e+6"); check(1234567890123.0, "1.234567890123e+12"); check(0.30000000000000004, "0.30000000000000004"); check(0.030000000000000002, "0.030000000000000002"); check(0.0030000000000000005, "0.0030000000000000005"); check(3.0000000000000003e-4, "3.0000000000000003e-4"); check(+double.nan, "nan"); check(-double.nan, "nan"); check(+double.infinity, "+inf"); check(-double.infinity, "-inf"); spec.separatorChar = ','; check(-0.0, "-0.0"); check(0.0, "0.0"); check(-0.01, "-0.01"); check(0.0125, "0.0125"); check(0.000003, "0.000003"); check(-3e-7, "-3e-7"); check(123456.0, "123,456.0"); check(123456e5, "12,345,600,000.0"); check(123456.1, "123,456.1"); check(12.3456, "12.3456"); check(-0.123456, "-0.123456"); check(0.1234567, "0.1234567"); check(0.01234567, "0.01234567"); check(0.001234567, "0.001234567"); check(1.234567e-4, "0.0001234567"); check(-1234567.0, "-1,234,567.0"); check(123456.7890123, "123,456.7890123"); check(1234567.890123, "1,234,567.890123"); check(123456789012.0, "123,456,789,012.0"); check(1234567890123.0, "1.234567890123e+12"); check(0.30000000000000004, "0.30000000000000004"); check(0.030000000000000002, "0.030000000000000002"); check(0.0030000000000000005, "0.0030000000000000005"); check(3.0000000000000003e-4, "0.00030000000000000003"); check(3.0000000000000005e-6, "0.0000030000000000000005"); check(3.0000000000000004e-7, "3.0000000000000004e-7"); check(+double.nan, "nan"); check(-double.nan, "nan"); check(+double.infinity, "+inf"); check(-double.infinity, "-inf"); spec.separatorChar = '_'; spec.separatorCount = 2; check(123456e5, "1_23_45_60_00_00.0"); spec.plus = true; check(0.0125, "+0.0125"); check(-0.0125, "-0.0125");
- void
print(C = char, W, T)(ref scope Ww, ref scope const Tc)
if (isSomeChar!C && is(T == struct) || is(T == union) && !is(T : NumericSpec));
voidprint(C = char, W, T)(ref scope Ww, scope const Tc)
if (isSomeChar!C && is(T == struct) || is(T == union)); - Prints structs and unionsExamples:
static struct A { scope void toString(C, W)(scope ref W w) const { w.put(C('a')); } } static struct S { scope void toString(W)(scope ref W w) const { w.put("s"); } } static struct D { scope void toString(Dg)(scope Dg sink) const { sink("d"); } } static struct F { scope const(char)[] toString()() const return { return "f"; } } static struct G { const(char)[] s = "g"; alias s this; } import mir.appender: scopedBuffer; auto w = scopedBuffer!char; assert(stringBuf << A() << S() << D() << F() << G() << getData == "asdfg");
- void
print(C = char, W, T)(ref scope Ww, scope const Tc)
if (isSomeChar!C && is(T == class) || is(T == interface)); - Prints classes and interfacesExamples:
static class A { void toString(C, W)(scope ref W w) const { w.put(C('a')); } } static class S { void toString(W)(scope ref W w) const { w.put("s"); } } static class D { void toString(Dg)(scope Dg sink) const { sink("d"); } } static class F { const(char)[] toString()() const return { return "f"; } } static class G { const(char)[] s = "g"; alias s this; } assert(stringBuf << new A() << new S() << new D() << new F() << new G() << getData == "asdfg");
- void
printStaticString(C, size_t N, W)(ref scope Ww, ref scope const C[N]c)
if (isSomeChar!C && is(C == char) || is(C == wchar) || is(C == dchar)); - void
printZeroPad(C = char, W, I)(ref scope Ww, const Ic, size_tminimalLength)
if (isSomeChar!C && isIntegral!I && !is(I == enum)); - Examples:
import mir.appender; auto w = scopedBuffer!char; w.printZeroPad(-123, 5); w.put(' '); w.printZeroPad(123, 5); assert(w.data == "-0123 00123");
- size_t
printBoolean(C)(boolc, ref C[5]buf)
if (is(C == char) || is(C == wchar) || is(C == dchar)); - void
print(C = char, W, T)(ref scope Ww, scope const T*c); - Prints pointers
Copyright © 2016-2026 by Ilya Yaroshenko | Page generated by
Ddoc on Wed Apr 8 13:47:03 2026