Skip to content

Commit effb1be

Browse files
authored
Merge pull request #6072 from JackStouffer/timeofday-fromISOExtString
Optimized std.datetime.date.TimeOfDay.fromISOExtString merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
2 parents 242ef22 + 774c141 commit effb1be

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

std/datetime/date.d

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9207,31 +9207,29 @@ public:
92079207
static TimeOfDay fromISOExtString(S)(in S isoExtString) @safe pure
92089208
if (isSomeString!S)
92099209
{
9210-
import std.algorithm.searching : all;
9211-
import std.ascii : isDigit;
9212-
import std.conv : to;
9213-
import std.exception : enforce;
9214-
import std.format : format;
9210+
import std.conv : ConvException, text, to;
92159211
import std.string : strip;
92169212

9217-
auto dstr = to!dstring(strip(isoExtString));
9218-
9219-
enforce(dstr.length == 8, new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9213+
auto str = strip(isoExtString);
9214+
int hours, minutes, seconds;
92209215

9221-
auto hours = dstr[0 .. 2];
9222-
auto minutes = dstr[3 .. 5];
9223-
auto seconds = dstr[6 .. $];
9216+
if (str.length != 8 || str[2] != ':' || str[5] != ':')
9217+
throw new DateTimeException(text("Invalid ISO Extended String: ", isoExtString));
92249218

9225-
enforce(dstr[2] == ':', new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9226-
enforce(dstr[5] == ':', new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9227-
enforce(all!isDigit(hours),
9228-
new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9229-
enforce(all!isDigit(minutes),
9230-
new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9231-
enforce(all!isDigit(seconds),
9232-
new DateTimeException(format("Invalid ISO Extended String: %s", isoExtString)));
9219+
try
9220+
{
9221+
// cast to int from uint is used because it checks for
9222+
// non digits without extra loops
9223+
hours = cast(int) to!uint(str[0 .. 2]);
9224+
minutes = cast(int) to!uint(str[3 .. 5]);
9225+
seconds = cast(int) to!uint(str[6 .. $]);
9226+
}
9227+
catch (ConvException)
9228+
{
9229+
throw new DateTimeException(text("Invalid ISO Extended String: ", isoExtString));
9230+
}
92339231

9234-
return TimeOfDay(to!int(hours), to!int(minutes), to!int(seconds));
9232+
return TimeOfDay(hours, minutes, seconds);
92359233
}
92369234

92379235
///

0 commit comments

Comments
 (0)