Skip to content

Commit ae07a90

Browse files
authored
Reduce std.getopt compile time by removing std.format calls (dlang#10839)
calling std.format, even though an awesome library, increases compile time quite a bit. This PR removes most calls to std.format.format.
1 parent 24711b9 commit ae07a90

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

std/getopt.d

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ private pure Option splitAndGet(string opt) @trusted nothrow
542542
assert(olongshort.optLong == "--foo");
543543
}
544544

545+
private string optionValidatorErrorFormat(string msg, size_t idx)
546+
{
547+
import std.conv : to;
548+
return "getopt validator: " ~ msg ~ " (at position " ~ to!(string)(idx) ~
549+
")";
550+
}
551+
545552
/*
546553
This function verifies that the variadic parameters passed in getOpt
547554
follow this pattern:
@@ -555,9 +562,6 @@ follow this pattern:
555562
*/
556563
private template optionValidator(A...)
557564
{
558-
import std.format : format;
559-
560-
enum fmt = "getopt validator: %s (at position %d)";
561565
enum isReceiver(T) = is(T == U*, U) || (is(T == function)) || (is(T == delegate));
562566
enum isOptionStr(T) = isSomeString!T || isSomeChar!T;
563567

@@ -568,11 +572,11 @@ private template optionValidator(A...)
568572
{
569573
static if (isReceiver!(A[0]))
570574
{
571-
msg = format(fmt, "first argument must be a string or a config", 0);
575+
msg = optionValidatorErrorFormat("first argument must be a string or a config", 0);
572576
}
573577
else static if (!isOptionStr!(A[0]) && !is(A[0] == config))
574578
{
575-
msg = format(fmt, "invalid argument type: " ~ A[0].stringof, 0);
579+
msg = optionValidatorErrorFormat("invalid argument type: " ~ A[0].stringof, 0);
576580
}
577581
else
578582
{
@@ -581,25 +585,25 @@ private template optionValidator(A...)
581585
static if (!isReceiver!(A[i]) && !isOptionStr!(A[i]) &&
582586
!(is(A[i] == config)))
583587
{
584-
msg = format(fmt, "invalid argument type: " ~ A[i].stringof, i);
588+
msg = optionValidatorErrorFormat("invalid argument type: " ~ A[i].stringof, i);
585589
goto end;
586590
}
587591
else static if (isReceiver!(A[i]) && !isOptionStr!(A[i-1]))
588592
{
589-
msg = format(fmt, "a receiver can not be preceeded by a receiver", i);
593+
msg = optionValidatorErrorFormat("a receiver can not be preceeded by a receiver", i);
590594
goto end;
591595
}
592596
else static if (i > 1 && isOptionStr!(A[i]) && isOptionStr!(A[i-1])
593597
&& isSomeString!(A[i-2]))
594598
{
595-
msg = format(fmt, "a string can not be preceeded by two strings", i);
599+
msg = optionValidatorErrorFormat("a string can not be preceeded by two strings", i);
596600
goto end;
597601
}
598602
}
599603
}
600604
static if (!isReceiver!(A[$-1]) && !is(A[$-1] == config))
601605
{
602-
msg = format(fmt, "last argument must be a receiver or a config",
606+
msg = optionValidatorErrorFormat("last argument must be a receiver or a config",
603607
A.length -1);
604608
}
605609
}
@@ -614,16 +618,16 @@ private auto getoptTo(R)(string option, string value,
614618
size_t idx, string file = __FILE__, size_t line = __LINE__)
615619
{
616620
import std.conv : to, ConvException;
617-
import std.format : format;
618621
try
619622
{
620623
return to!R(value);
621624
}
622625
catch (ConvException e)
623626
{
624-
throw new ConvException(format("Argument '%s' at position '%u' could "
625-
~ "not be converted to type '%s' as required by option '%s'.",
626-
value, idx, R.stringof, option), e, file, line);
627+
throw new ConvException("Argument '" ~ value ~ "' at position '" ~
628+
to!(string)(idx) ~ "' could not be converted to type '" ~
629+
R.stringof ~ "' as required by option '" ~ option ~ "'.", e, file,
630+
line);
627631
}
628632
}
629633

0 commit comments

Comments
 (0)