diff --git a/std/datetime/date.d b/std/datetime/date.d index 31407c589af..0132b5350a3 100644 --- a/std/datetime/date.d +++ b/std/datetime/date.d @@ -7549,40 +7549,37 @@ public: static Date fromISOExtString(S)(in S isoExtString) @safe pure if (isSomeString!(S)) { - import std.algorithm.searching : all, startsWith; - import std.ascii : isDigit; - import std.conv : to; - import std.exception : enforce; + import std.algorithm.searching : startsWith; + import std.conv : to, ConvException; import std.format : format; import std.string : strip; - auto dstr = to!dstring(strip(isoExtString)); - - enforce(dstr.length >= 10, new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); + auto str = strip(isoExtString); + short year; + ubyte month, day; - auto day = dstr[$-2 .. $]; - auto month = dstr[$-5 .. $-3]; - auto year = dstr[0 .. $-6]; + if (str.length < 10 || str[$-3] != '-' || str[$-6] != '-') + throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)); - enforce(dstr[$-3] == '-', new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); - enforce(dstr[$-6] == '-', new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); - enforce(all!isDigit(day), - new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); - enforce(all!isDigit(month), - new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); + auto yearStr = str[0 .. $-6]; + auto signAtBegining = cast(bool) yearStr.startsWith('-', '+'); + if ((yearStr.length > 4) != signAtBegining) + { + throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)); + } - if (year.length > 4) + try { - enforce(year.startsWith('-', '+'), - new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); - enforce(all!isDigit(year[1..$]), - new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); + day = to!ubyte(str[$-2 .. $]); + month = to!ubyte(str[$-5 .. $-3]); + year = to!short(yearStr); + } + catch (ConvException) + { + throw new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)); } - else - enforce(all!isDigit(year), - new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString))); - return Date(to!short(year), to!ubyte(month), to!ubyte(day)); + return Date(year, month, day); } ///