英語版
このページの英語版を見る
std.conv
値をある型から別の型に変換するためのワンストップショップ。
カテゴリー | 関数" 区分 |
---|---|
一般的な | asOriginalType castFrom parse to toChars |
文字列 | text wtext dtext hexString |
数値 | octal roundTo signed unsigned |
例外 | ConvException ConvOverflowException |
License:
Authors:
Walter Bright,
Andrei Alexandrescu,
Shin Fujishiro,
Adam D. Ruppe,
Kenji Hara
ソース std/conv.d
- class
ConvException
: object.Exception; - 変換エラー時にスローされる。Examples:
import std.exception : assertThrown; assertThrown!ConvException(to!int("abc"));
- class
ConvOverflowException
: std.conv.ConvException; - 変換オーバーフローエラー時にスローされる。Examples:
import std.exception : assertThrown; assertThrown!ConvOverflowException(to!ubyte(1_000_000));
- template
to
(T) - この
to
テンプレートは値をある型から別の型に変換する。 変換元の型が推論され、変換先の型が指定されなければならない。 式to
!int(42.0)は数値42を double からint 。 変換は "安全"である、 オーバーフローをチェックする;to
!int(4.2e10)は例外をスローする。 ConvOverflowException 例外を投げる。オーバーフローチェックは オーバーフローチェックは必要なときだけ挿入される、to
!double(42)はチェックを行わない。 例えば、int はdouble に収まるので、チェックを行わない。文字列型から数値型への変換は、Cと同等なものとは異なる。 atoi() やatol() と異なり、オーバーフローをチェックし、空白を許さない。 文字列から符号型への変換の場合、認識される文法は以下の通りである:Integer: Sign UnsignedInteger UnsignedInteger Sign: + -
符号なし型への変換の場合、認識される文法は次のとおりである:UnsignedInteger: DecimalDigit DecimalDigit UnsignedInteger
Examples:値をそれ自身の型に変換する(主に汎用的なコードに有用)。 は単にその引数を返す。int a = 42; int b = to!int(a); double c = to!double(3.14); // cはdoubleで3.14の値
Examples:数値型間の変換は、数値型をキャストする安全な方法である。 浮動小数点型から積分型への変換は、精度(浮動小数点数の小数部分)を失う可能性がある。 精度(浮動小数点数の小数部分)を失うことになる。この 変換はゼロに向かって切り捨てられる。 と同じである。(浮動小数点値を整数にキャストする際に丸めるには、 を使用する。 roundTo を使用する)。import std.exception : assertThrown; int a = 420; writeln(to!long(a)); // a assertThrown!ConvOverflowException(to!byte(a)); writeln(to!int(4.2e6)); // 4200000 assertThrown!ConvOverflowException(to!uint(-3.14)); writeln(to!uint(3.14)); // 3 writeln(to!uint(3.99)); // 3 writeln(to!int(-3.99)); // -3
Examples:文字列を数値型に変換する場合、D16進数と2進数のリテラルは扱えないことに注意すること。 リテラルは扱われない。基数を示す接頭辞も、桁のグループを区切るための横棒も認識されない。 も認識されない。これは 型を示す接尾辞も同様である。 これを回避するために、数値を含む変換に基数を指定することができる。auto str = to!string(42, 16); writeln(str); // "2A" auto i = to!int(str, 16); writeln(i); // 42
Examples:積分型から浮動小数点型への変換は常に成功する。 成功するが、精度が落ちる可能性がある。浮動小数点形式で表現可能な 浮動小数点フォーマットで表現可能な前任者を持つ最大の整数は2^24-1 である。 float に対する2^53-1 、double に対する 、real に対する2^64-1 (が80ビットの場合)である。 real が80ビットの場合、例えばIntelマシンの場合)である。// 2^24 - 1, floatとして表現できる最大の整数 int a = 16_777_215; writeln(to!int(to!float(a))); // a writeln(to!int(to!float(-a))); // -a
Examples:文字列型からchar型への変換は、入力を強制する。 を強制する。 でなければならない。そうでなければ ConvExceptionがスローされる。import std.exception : assertThrown; writeln(to!char("a")); // 'a' assertThrown(to!char("ñ")); // 'ñ'はcharに収まらない writeln(to!wchar("ñ")); // 'ñ' assertThrown(to!wchar("😃")); // '😃'はwcharに収まらない writeln(to!dchar("😃")); // '😃' // ソース型としてwstringまたはdstringを使用しても、結果には影響しない writeln(to!char("a"w)); // 'a' writeln(to!char("a"d)); // 'a' // 2つのコードポイントを1つに変換することはできない assertThrown(to!char("ab"));
Examples:配列を別の配列型に変換するには、各要素を順番に変換する。 要素を順番に変換する。連想配列は、キーと値が順番に変換できる限り、連想配列に変換できる。 配列に変換することができる。import std.string : split; int[] a = [1, 2, 3]; auto b = to!(float[])(a); writeln(b); // [1.0f, 2, 3] string str = "1 2 3 4 5 6"; auto numbers = to!(double[])(split(str)); writeln(numbers); // [1.0, 2, 3, 4, 5, 6] int[string] c; c["a"] = 1; c["b"] = 2; auto d = to!(double[wstring])(c); assert(d["a"w] == 1 && d["b"w] == 2);
Examples:変換は推移的に動作する。 連想配列に対して動作する。 この変換が機能するのはto
!shortはint に適用されるからである、to
!wstring はstring に適用される、to
!stringはdouble 、そしてto
!(double[])はint[] に適用される。 例外を投げるかもしれない。to
!short変換は例外を投げるかもしれない。int[string][double[int[]]] a; auto b = to!(short[wstring][string[double[]]])(a);
Examples:動的キャストによるオブジェクト間の変換は、変換元が非NULLで変換先がNULLの場合に例外を投げる。 が発生する。import std.exception : assertThrown; // オブジェクトの変換をテストする class A {} class B : A {} class C : A {} A a1 = new A, a2 = new B, a3 = new C; assert(to!B(a2) is a2); assert(to!C(a3) is a3); assertThrown!ConvException(to!B(a3));
Examples:すべての型からのStringize変換がサポートされている。- 文字列から文字列への変換は、以下の2つの文字列型に対して機能する。 (char,wchar,dchar) の文字幅を持ち、修飾子 (mutable , or ) を持つ任意の2つの文字列型に対して動作する。 mutable,const,immutable )の修飾子の組み合わせで動作する。
- 配列(文字列以外)を文字列に変換する。
各要素は
to
!T. - 連想配列から文字列への変換。
各要素は
to
!T. - オブジェクトから文字列への変換は、オブジェクトに対してtoString 。 オブジェクトから文字列への変換は、オブジェクトに対して"null" を呼び出す。
- 構造体から文字列への変換は、構造体が定義されていれば、構造体に対してtoString を呼び出す。 を呼び出す。
- toString を定義していない構造体の場合、文字列への変換はフィールドのリストを生成する。 はフィールドのリストを生成する。
- 列挙型は、そのシンボル名として文字列に変換される。
- ブール値は、"true" または"false" に変換される。
- char wchar, を文字列型に変換する。dchar
- 符号なし整数または符号付き整数は文字列に変換される。
- [特別な場合]
- 基数radixの整数値を文字列に変換する。 基数は2から36までの値でなければならない。 radixが10の場合のみ、値は符号付き値として扱われる。 文字Aから文字Zは、値10から値36を表すのに使われる。 大文字と小文字はletterCaseパラメータによって決定される。
- すべての浮動小数点型はすべての文字列型に変換される。
- ポインタから文字列への変換は、ポインタをsize_t 値に変換する。 ポインタがchar* の場合、C言語の文字列として扱う。 その場合、この関数は@system となる。
// 動的/静的配列を文字列で表す変換 long[] a = [ 1, 3, 5 ]; writeln(to!string(a)); // "[1, 3, 5]" // 連想配列を文字列で表す変換 int[string] associativeArray = ["0":1, "1":2]; assert(to!string(associativeArray) == `["0":1, "1":2]` || to!string(associativeArray) == `["1":2, "0":1]`); // char*から文字列への変換 writeln(to!string(cast(char*)null)); // "" writeln(to!string("foo\0".ptr)); // "foo" // void 配列を文字列に再変換する auto w = "abcx"w; const(void)[] b = w; writeln(b.length); // 8 auto c = to!(wchar[])(b); writeln(c); // "abcx"
Examples:文字列は列挙型に変換できる。入力文字列と同じ名前の列挙型メンバが返される。 が返される。比較では大文字と小文字が区別される。 A ConvExceptionがスローされる。import std.exception : assertThrown; enum E { a, b, c } writeln(to!E("a")); // E.a writeln(to!E("b")); // E.b assertThrown!ConvException(to!E("A"));
- template
roundTo
(Target) - 浮動小数点から積分への丸め変換。丸め変換は整数でないターゲット型では動作しない。Examples:
writeln(roundTo!int(3.14)); // 3 writeln(roundTo!int(3.49)); // 3 writeln(roundTo!int(3.5)); // 4 writeln(roundTo!int(3.999)); // 4 writeln(roundTo!int(-3.14)); // -3 writeln(roundTo!int(-3.49)); // -3 writeln(roundTo!int(-3.5)); // -4 writeln(roundTo!int(-3.999)); // -4 writeln(roundTo!(const int)(to!(const double)(-3.999))); // -4
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sourcesource
)
if (is(immutable(Target) == immutable(bool)) && isInputRange!Source && isSomeChar!(ElementType!Source)); - この
parse
関数 "ファミリーは、以下のように機能する。 to ファミリーとまったく同じように動作する:- 入力として文字範囲のみを扱う。
- 参照によって入力を受け取る。これはr値(文字列リテラルなど)を受け付けないことを意味する。 文字列リテラルなど)は受け付けられない。代わりにto 。
- 変換後の位置に入力を進める。
- 入力全体を変換できなくてもスローしない。
このオーバーロードは、文字入力範囲からbool を解析する。Parameters:Target に変換するブール型。 Source source
入力範囲のl値 doCount 消費文字数を報告するかどうかを決定するフラグ Returns:- doCount に設定されている場合はbool No.doCount
- doCount に設定されている場合、bool とsize_t を含むtuple 。Yes.doCount
Throws:A ConvExceptionに設定されている場合、その範囲はbool を表していない。注釈 を使ったすべての文字入力範囲の変換は toを使用したすべての文字入力範囲の変換は に転送される。
parse
に転送され、l値を必要としない。Examples:import std.typecons : Flag, Yes, No; auto s = "true"; bool b = parse!bool(s); assert(b); auto s2 = "true"; bool b2 = parse!(bool, string, No.doCount)(s2); assert(b2); auto s3 = "true"; auto b3 = parse!(bool, string, Yes.doCount)(s3); assert(b3.data && b3.count == 4); auto s4 = "falSE"; auto b4 = parse!(bool, string, Yes.doCount)(s4); assert(!b4.data && b4.count == 5);
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref scope Sources
)
if (isIntegral!Target && !is(Target == enum) && isSomeChar!(ElementType!Source));
autoparse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sourcesource
, uintradix
)
if (isIntegral!Target && !is(Target == enum) && isSomeChar!(ElementType!Source)); - 文字入力範囲から整数を解析する。Parameters:
Target に変換する整数型。 Source s
入力範囲のl値 doCount 消費文字数を報告するかどうかのフラグ Returns:- doCount に設定されている場合は、Target 型の数。No.doCount
- doCount が設定されている場合、Target の型とsize_t の型を含むtuple 。Yes.doCount
Throws:A ConvException変換中にオーバーフローが発生した場合、または 入力のどの文字も有意義に変換されなかった場合。Examples:import std.typecons : Flag, Yes, No; string s = "123"; auto a = parse!int(s); writeln(a); // 123 string s1 = "123"; auto a1 = parse!(int, string, Yes.doCount)(s1); assert(a1.data == 123 && a1.count == 3); // 解析はl値のみを受け入れる static assert(!__traits(compiles, parse!int("123")));
Examples:import std.string : tr; import std.typecons : Flag, Yes, No; string test = "123 \t 76.14"; auto a = parse!uint(test); writeln(a); // 123 assert(test == " \t 76.14"); // バンプ文字列を解析する test = tr(test, " \t\n\r", "", "d"); // wsをスキップする writeln(test); // "76.14" auto b = parse!double(test); writeln(b); // 76.14 writeln(test); // "" string test2 = "123 \t 76.14"; auto a2 = parse!(uint, string, Yes.doCount)(test2); assert(a2.data == 123 && a2.count == 3); assert(test2 == " \t 76.14");// バンプ文字列を解析する test2 = tr(test2, " \t\n\r", "", "d"); // wsをスキップする writeln(test2); // "76.14" auto b2 = parse!(double, string, Yes.doCount)(test2); assert(b2.data == 76.14 && b2.count == 5); writeln(test2); // ""
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
)
if (is(Target == enum) && isSomeString!Source && !is(Source == enum)); - 列挙型のメンバー名を表す文字列からenum 型を解析する。Parameters:
Target enum 型に変換する。 Source s
解析する範囲のl値 doCount 消費された文字数を報告するかどうかを決定するフラグ Returns:- doCount が次のように設定されている場合、Target 型のenum を含む。No.doCount
- Target のenum と、doCount に設定されている場合はsize_t を含むtuple 。Yes.doCount
Throws:Examples:import std.typecons : Flag, Yes, No, tuple; enum EnumType : bool { a = true, b = false, c = a } auto str = "a"; writeln(parse!EnumType(str)); // EnumType.a auto str2 = "a"; writeln(parse!(EnumType, string, No.doCount)(str2)); // EnumType.a auto str3 = "a"; writeln(parse!(EnumType, string, Yes.doCount)(str3)); // tuple(EnumType.a, 1)
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sourcesource
)
if (isFloatingPoint!Target && !is(Target == enum) && isInputRange!Source && isSomeChar!(ElementType!Source) && !is(Source == enum)); - 文字範囲から浮動小数点数を解析する。Parameters:
Target 浮動小数点型 Source source
解析する範囲のl値 doCount 消費文字数を報告するかどうかのフラグ Returns:- doCount に設定されている場合は、Target 型の浮動小数点数。No.doCount
- Target 型の浮動小数点数を含むtuple およびsize_t に設定されている場合は、doCount 。Yes.doCount
Throws:A ConvExceptionの場合source
が空であるか が空であるか、オーバーフローが発生した場合である。Examples:import std.math.operations : isClose; import std.math.traits : isNaN, isInfinity; import std.typecons : Flag, Yes, No; auto str = "123.456"; assert(parse!double(str).isClose(123.456)); auto str2 = "123.456"; assert(parse!(double, string, No.doCount)(str2).isClose(123.456)); auto str3 = "123.456"; auto r = parse!(double, string, Yes.doCount)(str3); assert(r.data.isClose(123.456)); writeln(r.count); // 7 auto str4 = "-123.456"; r = parse!(double, string, Yes.doCount)(str4); assert(r.data.isClose(-123.456)); writeln(r.count); // 8 auto str5 = "+123.456"; r = parse!(double, string, Yes.doCount)(str5); assert(r.data.isClose(123.456)); writeln(r.count); // 8 auto str6 = "inf0"; r = parse!(double, string, Yes.doCount)(str6); assert(isInfinity(r.data) && r.count == 3 && str6 == "0"); auto str7 = "-0"; auto r2 = parse!(float, string, Yes.doCount)(str7); assert(r2.data.isClose(0.0) && r2.count == 2); auto str8 = "nan"; auto r3 = parse!(real, string, Yes.doCount)(str8); assert(isNaN(r3.data) && r3.count == 3);
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
)
if (staticIndexOf!(immutable(Target), immutable(dchar), immutable(ElementEncodingType!Source)) >= 0 && isSomeString!Source && !is(Source == enum));
autoparse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
)
if (isSomeChar!Target && (Target.sizeof >= ElementType!Source.sizeof) && !is(Target == enum) && !isSomeString!Source && isInputRange!Source && isSomeChar!(ElementType!Source)); - 文字範囲から1文字を解析する。Parameters:
Target に変換する型。 Source s
入力範囲のl値 doCount 消費文字数を報告するかどうかのフラグ Returns:- doCount に設定されている場合は、Target 型の文字。No.doCount
- doCount が設定されている場合、Target とsize_t を含むtuple 。Yes.doCount
Throws:A ConvExceptionに設定される。Examples:import std.typecons : Flag, Yes, No; auto s = "Hello, World!"; char first = parse!char(s); writeln(first); // 'H' writeln(s); // "ello, World!" char second = parse!(char, string, No.doCount)(s); writeln(second); // 'e' writeln(s); // "llo, World!" auto third = parse!(char, string, Yes.doCount)(s); assert(third.data == 'l' && third.count == 1); writeln(s); // "lo, World!"
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
)
if (is(immutable(Target) == immutable(typeof(null))) && isInputRange!Source && isSomeChar!(ElementType!Source)); - typeof(null) を文字範囲から解析する。 のスペルが"null" の場合、その文字範囲から をパースする。この関数は大文字小文字を区別しない。Parameters:
Target に変換する型。 Source s
入力範囲のl値 doCount 消費文字数を報告するかどうかのフラグ Returns:- null doCount がNo.doCount
- null を含むtuple と、doCount に設定されている場合はsize_t とする。Yes.doCount
Throws:A ConvExceptionに設定されている場合、その範囲はnull を表していない。Examples:import std.exception : assertThrown; import std.typecons : Flag, Yes, No; alias NullType = typeof(null); auto s1 = "null"; assert(parse!NullType(s1) is null); writeln(s1); // "" auto s2 = "NUll"d; assert(parse!NullType(s2) is null); writeln(s2); // "" auto s3 = "nuLlNULl"; assert(parse!(NullType, string, No.doCount)(s3) is null); auto r = parse!(NullType, string, Yes.doCount)(s3); assert(r.data is null && r.count == 4); auto m = "maybe"; assertThrown!ConvException(parse!NullType(m)); assertThrown!ConvException(parse!(NullType, string, Yes.doCount)(m)); assert(m == "maybe"); // 失敗してもmは変わらないはずだ auto s = "NULL"; assert(parse!(const NullType)(s) is null);
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
, dcharlbracket
= '[', dcharrbracket
= ']', dcharcomma
= ',')
if (isDynamicArray!Target && !is(Target == enum) && isSomeString!Source && !is(Source == enum));
autoparse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
, dcharlbracket
= '[', dcharrbracket
= ']', dcharcomma
= ',')
if (isStaticArray!Target && !is(Target == enum) && isExactSomeString!Source); - 左括弧(デフォルト '[' )、右括弧(デフォルト']' )、要素区切り文字(デフォルト デフォルトは',' )。末尾にセパレータを付けることもできる。Parameters:
Source s
解析する文字列 dchar lbracket
配列を開始する文字 dchar rbracket
配列を終了する文字 dchar comma
配列の要素を区切る文字 doCount 消費した文字数を報告するかどうかのフラグ。 Returns:- doCount に設定されている場合は、Target 型の配列。No.doCount
- Target 型の配列を含むtuple と、doCount に設定されている場合はsize_t 型の配列。Yes.doCount
Examples:import std.typecons : Flag, Yes, No; auto s1 = `[['h', 'e', 'l', 'l', 'o'], "world"]`; auto a1 = parse!(string[])(s1); writeln(a1); // ["hello", "world"] auto s2 = `["aaa", "bbb", "ccc"]`; auto a2 = parse!(string[])(s2); writeln(a2); // ["aaa", "bbb", "ccc"] auto s3 = `[['h', 'e', 'l', 'l', 'o'], "world"]`; auto len3 = s3.length; auto a3 = parse!(string[], string, Yes.doCount)(s3); writeln(a3.data); // ["hello", "world"] writeln(a3.count); // len3
- auto
parse
(Target, Source, Flag!"doCount" doCount = No.doCount)(ref Sources
, dcharlbracket
= '[', dcharrbracket
= ']', dcharkeyval
= ':', dcharcomma
= ',')
if (isAssociativeArray!Target && !is(Target == enum) && isSomeString!Source && !is(Source == enum)); - 左括弧(デフォルト '[' )、右括弧(デフォルト']' )、キー値区切り文字(デフォルト ':' )、要素区切り文字(デフォルト',' )を指定して、文字列から連想配列をパースする。Parameters:
Source s
解析する文字列 dchar lbracket
連想配列を開始する文字 dchar rbracket
連想配列を終了する文字 dchar keyval
キーと値を関連付ける文字 dchar comma
連想配列の要素を区切る文字 doCount 消費文字数を報告するかどうかのフラグ Returns:- doCount 、Target 型の連想配列。No.doCount
- Target 、tuple の連想配列を含む。size_t doCount がYes.doCount
Examples:import std.typecons : Flag, Yes, No, tuple; import std.range.primitives : save; import std.array : assocArray; auto s1 = "[1:10, 2:20, 3:30]"; auto copyS1 = s1.save; auto aa1 = parse!(int[int])(s1); writeln(aa1); // [1:10, 2:20, 3:30] // parse!(int[int], string, Yes.doCount)(copyS1) writeln(tuple([1:10, 2:20, 3:30], copyS1.length)); auto s2 = `["aaa":10, "bbb":20, "ccc":30]`; auto copyS2 = s2.save; auto aa2 = parse!(int[string])(s2); writeln(aa2); // ["aaa":10, "bbb":20, "ccc":30] assert(tuple(["aaa":10, "bbb":20, "ccc":30], copyS2.length) == parse!(int[string], string, Yes.doCount)(copyS2)); auto s3 = `["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]`; auto copyS3 = s3.save; auto aa3 = parse!(int[][string])(s3); writeln(aa3); // ["aaa":[1], "bbb":[2, 3], "ccc":[4, 5, 6]] assert(tuple(["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]], copyS3.length) == parse!(int[][string], string, Yes.doCount)(copyS3)); auto s4 = `[]`; int[int] emptyAA; writeln(tuple(emptyAA, s4.length)); // parse!(int[int], string, Yes.doCount)(s4)
- string
text
(T...)(Targs
)
if (T.length > 0);
wstringwtext
(T...)(Targs
)
if (T.length > 0);
dstringdtext
(T...)(Targs
)
if (T.length > 0); - つ以上の引数をテキストに変換するための便利な関数である。 をテキスト(3つの文字幅)に変換するための便利な関数である。Examples:
writeln(text(42, ' ', 1.5, ": xyz")); // "42 1.5: xyz"c writeln(wtext(42, ' ', 1.5, ": xyz")); // "42 1.5: xyz"w writeln(dtext(42, ' ', 1.5, ": xyz")); // "42 1.5: xyz"d
- template
octal
(string num) if (isOctalLiteral(num))
templateoctal
(alias decimalInteger) if (is(typeof(decimalInteger)) && isIntegral!(typeof(decimalInteger))) - この
octal
機能は、8進数を宣言する手段を提供する。 この機能はoctal
!177またはoctal
!"177"を使用すると、8進数 (C言語の0177と同じ)。文字列のルールはリテラルと同じである:に収まる場合、それは である。 intに収まる場合はint 、収まらない場合はlong となる。 long に収まる場合はL 、収まらない場合は となる。 longを与える。U またはu の接尾辞が要求された場合は、符号なしを与える。整数から作成された8進数は、渡された積分の型を保持する。 を保持する。See Also:parse実行時に8進文字列を解析する。Examples:// 0177と同じ auto a = octal!177; // 8進数はコンパイル時のデバイスである enum b = octal!160; // 符号なし8進数を作成する auto c = octal!"1_000_000u"; // 文字列から変換する場合、先頭のゼロは許される auto d = octal!"0001_200_000";
- auto
unsigned
(T)(Tx
)
if (isIntegral!T);
autounsigned
(T)(Tx
)
if (isSomeChar!T); - に対応する符号なし値を返す。
x
(に対応する符号なし値を返す(例えばx
に対応する符号なし値を返す(例えば int の場合、cast(uint) x )を返す。キャスト と比較した場合の利点は、キャストを書き直す必要がないことである。x
が変更された場合、キャストを書き直す必要がないことだ。 (例えば、int からlong) に変更された場合に、キャストを書き直す必要がないことである。元の型がconst型やimmutable型であったとしても、結果は常に変更可能であることに注意されたい。 または不変であったとしても、結果は常に変更可能であることに注意しよう。定数を保持するには std.traits.Unsigned.Examples:import std.traits : Unsigned; immutable int s = 42; auto u1 = unsigned(s); //修飾されていない static assert(is(typeof(u1) == uint)); Unsigned!(typeof(s)) u2 = unsigned(s); // 同じ資格 static assert(is(typeof(u2) == immutable uint)); immutable u3 = unsigned(s); // 明示的に資格がある
- auto
signed
(T)(Tx
)
if (isIntegral!T); - に対応する符号付き値を返す。
x
(に対応する符号付き値を返す(例えばx
に対応する符号付き値を返す(例えば uint の場合、cast(int) x )を返す。キャスト と比較した場合の利点は、キャストを書き直す必要がないことである。x
が変更された場合、キャストを書き直す必要がないことだ。 (例えば、uint からulong) に変更された場合に、キャストを書き直す必要がないことである。元の型がconst型やimmutable型であったとしても、結果は常に変更可能であることに注意されたい。 または不変であったとしても、結果は常に変更可能であることに注意しよう。定数を保持するには std.traits.Signed.Examples:import std.traits : Signed; immutable uint u = 42; auto s1 = signed(u); //資格がない static assert(is(typeof(s1) == int)); Signed!(typeof(u)) s2 = signed(u); // 同じ資格 static assert(is(typeof(s2) == immutable int)); immutable s3 = signed(u); // 明示的に資格がある
- OriginalType!E
asOriginalType
(E)(Evalue
)
if (is(E == enum)); - 列挙値の表現、すなわち列挙の基本型に変換された値を返す。 に変換した値を返す。Examples:
enum A { a = 42 } static assert(is(typeof(A.a.asOriginalType) == int)); writeln(A.a.asOriginalType); // 42 enum B : double { a = 43 } static assert(is(typeof(B.a.asOriginalType) == double)); writeln(B.a.asOriginalType); // 43
- template
castFrom
(From) - 組み込みのキャスト演算子のラッパーで、値の元の型のキャストを制限することができる。 を制限することができる。生のキャストを使用する際の一般的な問題は、リファクタリング中に値の型が変更されても、黙って リファクタリング中に値の型が変更された場合でも、無言でコンパイルが継続される可能性があることだ、 これは、キャストに関する最初の仮定を壊してしまう。Parameters:
From キャスト元の型。プログラマは、このキャストを行うことが合法であることを確認しなければならない であることを確認しなければならない。 Examples:// 通常のcastは、プログラマーによって有効であることが検証されている: { long x; auto y = cast(int) x; } // しかし、'x'をポインタに変更してもコンパイルは可能である: { long* x; auto y = cast(int) x; } // castFromは、castよりも信頼性の高い代替手段を提供する: { long x; auto y = castFrom!long.to!int(x); } // 'x'の型を変更すると、コンパイラエラーが発生するようになったため、 // 手遅れになる前に不正なキャストを検出できるようになる: { long* x; static assert( !__traits(compiles, castFrom!long.to!int(x)) ); // キャストがまだ必要であれば、次のように変更しなければならない: auto y = castFrom!(long*).to!int(x); }
- ref @system auto
to
(To, T)(auto ref Tvalue
); - Parameters:
To キャストする型。 T value
キャストする値。From 型でなければならない、 でなければコンパイル時エラーが発生する。 Returns:可能であれば参照で返される、キャスト後の値。
- template
hexString
(string hexData) if (hexData.isHexLiteral)
templatehexString
(wstring hexData) if (hexData.isHexLiteral)
templatehexString
(dstring hexData) if (hexData.isHexLiteral) - コンパイル時に16進リテラルを文字列に変換する。16進数からなる文字列を受け取り、各桁を文字に変換して一致する文字列を返す。 各桁のペアを文字に変換して、一致する文字列を返す。 入力文字列には白文字を含めることもできる。 を使用することができる。 この関数は、 で始まる16進数リテラル文字列を置き換えることを意図している。 'x'で始まる16進数リテラル文字列を置き換えるためのものである。Parameters:
hexData 文字列を変換する。 Returns:hexDataの型に従って、string 、wstring 、またはdstring 。Examples:// コンパイル時変換 auto string1 = hexString!"304A314B"; writeln(string1); // "0J1K" auto string2 = hexString!"304A314B"w; writeln(string2); // "0J1K"w auto string3 = hexString!"304A314B"d; writeln(string3); // "0J1K"d
- pure nothrow @nogc @safe auto
toChars
(ubyte radix = 10, Char = char, LetterCase letterCase = LetterCase.lower, T)(Tvalue
)
if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && isIntegral!T && (radix == 10 || isUnsigned!T)); - 整数を文字の範囲に変換する。 軽量で高速であることを意図している。Parameters:
radix 2, 8, 10, 16 Char 出力の文字型。 letterCase 下はdeadbeef、上はDEADBEEFを表す。 T value
変換する整数。ubyte、ushort、uint、ulongのいずれか。基数 が10の場合、byte、short、int、longのいずれか。 Returns:スライスを含むランダムアクセス範囲Examples:import std.algorithm.comparison : equal; assert(toChars(1).equal("1")); assert(toChars(1_000_000).equal("1000000")); assert(toChars!(2)(2U).equal("10")); assert(toChars!(16)(255U).equal("ff")); assert(toChars!(16, char, LetterCase.upper)(255U).equal("FF"));
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku