From 962db5447f6b05a3c130ac25382dac97832e003e Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Thu, 25 Jan 2018 10:34:51 -0500 Subject: [PATCH] Optimized std.datetime.date.Date.fromSimpleString --- std/datetime/date.d | 49 +++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/std/datetime/date.d b/std/datetime/date.d index 0132b5350a3..451f8c0c004 100644 --- a/std/datetime/date.d +++ b/std/datetime/date.d @@ -7687,37 +7687,37 @@ public: static Date fromSimpleString(S)(in S simpleString) @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(simpleString)); - - enforce(dstr.length >= 11, new DateTimeException(format("Invalid string format: %s", simpleString))); + auto str = strip(simpleString); - auto day = dstr[$-2 .. $]; - auto month = monthFromString(to!string(dstr[$-6 .. $-3])); - auto year = dstr[0 .. $-7]; + if (str.length < 11 || str[$-3] != '-' || str[$-7] != '-') + throw new DateTimeException(format!"Invalid string format: %s"(simpleString)); - enforce(dstr[$-3] == '-', new DateTimeException(format("Invalid string format: %s", simpleString))); - enforce(dstr[$-7] == '-', new DateTimeException(format("Invalid string format: %s", simpleString))); - enforce(all!isDigit(day), new DateTimeException(format("Invalid string format: %s", simpleString))); + int year; + uint day; + auto month = monthFromString(str[$ - 6 .. $ - 3]); + auto yearStr = str[0 .. $ - 7]; + auto signAtBegining = cast(bool) yearStr.startsWith('-', '+'); + if ((yearStr.length > 4) != signAtBegining) + { + throw new DateTimeException(format!"Invalid string format: %s"(simpleString)); + } - if (year.length > 4) + try { - enforce(year.startsWith('-', '+'), - new DateTimeException(format("Invalid string format: %s", simpleString))); - enforce(all!isDigit(year[1..$]), - new DateTimeException(format("Invalid string format: %s", simpleString))); + day = to!uint(str[$ - 2 .. $]); + year = to!int(yearStr); + } + catch (ConvException) + { + throw new DateTimeException(format!"Invalid string format: %s"(simpleString)); } - else - enforce(all!isDigit(year), - new DateTimeException(format("Invalid string format: %s", simpleString))); - return Date(to!short(year), month, to!ubyte(day)); + return Date(year, month, day); } /// @@ -10244,7 +10244,8 @@ string monthToString(Month month) @safe pure $(REF DateTimeException,std,datetime,date) if the given month is not a valid month string. +/ -Month monthFromString(string monthStr) @safe pure +Month monthFromString(T)(T monthStr) @safe pure +if (isSomeString!T) { import std.format : format; switch (monthStr) @@ -10274,7 +10275,7 @@ Month monthFromString(string monthStr) @safe pure case "Dec": return Month.dec; default: - throw new DateTimeException(format("Invalid month %s", monthStr)); + throw new DateTimeException(format!"Invalid month %s"(monthStr)); } }