Skip to content

Commit 451f609

Browse files
committed
Update: Use a better either algorithm that checks for null strings
1 parent 4736850 commit 451f609

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

source/redub/api.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ bool cleanProject(ProjectDetails d, bool showMessages)
580580
ArgsDetails resolveArguments(string[] args, bool isDescribeOnly = false)
581581
{
582582
import core.stdc.stdlib;
583-
import std.algorithm.comparison:either;
583+
import redub.misc.either;
584584
import std.getopt;
585585
import std.file;
586586

@@ -690,7 +690,7 @@ ProjectDetails resolveDependencies(
690690
{
691691
import std.datetime.stopwatch;
692692
import redub.building.cache;
693-
import std.algorithm.comparison;
693+
import redub.misc.either;
694694
import redub.command_generators.commons;
695695
static import redub.parsers.single;
696696
static import redub.parsers.automatic;

source/redub/buildapi.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ struct BuildConfiguration
337337
*/
338338
BuildConfiguration merge()(const auto ref BuildConfiguration other) const
339339
{
340-
import std.algorithm.comparison:either;
340+
import redub.misc.either;
341341
BuildConfiguration ret = clone;
342342
ret.runtimeWorkingDir = either(other.runtimeWorkingDir, ret.runtimeWorkingDir);
343343
ret.targetType = either(other.targetType, ret.targetType);

source/redub/misc/either.d

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module redub.misc.either;
2+
3+
T either(T, Ts...)(T first, lazy Ts alternatives) if(alternatives.length >= 1)
4+
{
5+
static if(is(T : char[]) || is(T : string))
6+
{
7+
if(first.length != 0) return first;
8+
foreach(string alt; alternatives)
9+
if(alt.length != 0)
10+
return alt;
11+
return alternatives[$-1];
12+
}
13+
else
14+
{
15+
if(first) return first;
16+
foreach(alt; alternatives)
17+
if(alt)
18+
return alt;
19+
return alternatives[$-1];
20+
}
21+
}

source/redub/misc/find_executable.d

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ string findExecutable(string executableName)
77
import std.path : isAbsolute, extension, buildPath;
88
import std.file;
99
import std.algorithm.iteration:splitter;
10+
11+
if(executableName.length == 0)
12+
throw new Exception("Needs an executable name to find.");
1013
string pathEnv = getEnvVariable("PATH");
1114

1215
version(Windows)
13-
static string[] EXTENSIONS = [".exe", ".bat", ".cmd", ".com", ""];
16+
static immutable string[] EXTENSIONS = [".exe", ".bat", ".cmd", ".com", ""];
1417
else
15-
static string[] EXTENSIONS = [""];
18+
static immutable string[] EXTENSIONS = [""];
19+
static immutable string[] emptyExt = [""];
1620

17-
string[] extensionsTest = EXTENSIONS;
21+
const(string)[] extensionsTest = EXTENSIONS;
1822
if(extension(executableName) != null)
19-
extensionsTest = [""];
23+
extensionsTest = emptyExt;
2024

2125
static bool isExecutable(string tPath)
2226
{
@@ -44,7 +48,7 @@ string findExecutable(string executableName)
4448
foreach(path; splitter(pathEnv, pathSeparator))
4549
{
4650
string str = redub.misc.path.normalizePath(bufferSink, path, executableName);
47-
foreach(ext; EXTENSIONS)
51+
foreach(ext; extensionsTest)
4852
{
4953
bufferSink[str.length..str.length+ext.length] = ext;
5054
string fullPath = cast(string)buffer[0..str.length+ext.length];

source/redub/parsers/json.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ BuildRequirements parse(JSONValue json, ParseConfig cfg, out BuildConfiguration
302302
{
303303
import std.path;
304304
import std.exception;
305-
import std.algorithm.comparison;
306305
import redub.package_searching.api;
307306
import redub.package_searching.cache;
308307
import std.parallelism;

source/redub/tooling/compiler_identification.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ string tryGetStr(JSONValue v, string key)
141141
*/
142142
Compiler getCompiler(string compilerOrPath = "dmd", string cCompilerOrPath = null, string compilerAssumption = null, string arch = null)
143143
{
144-
import std.algorithm.comparison:either;
144+
import redub.misc.either;
145145
import redub.meta;
146146

147147
JSONValue compilersInfo = getRedubMeta();
148-
bool isDefault = compilerOrPath == null;
148+
bool isDefault = compilerOrPath.length == 0;
149149
compilerOrPath = either(compilerOrPath, tryGetStr(compilersInfo, "defaultCompiler"), "dmd");
150150
cCompilerOrPath = either(cCompilerOrPath, tryGetStr(compilersInfo, "defaultCCompiler"), defaultCCompiler);
151151

0 commit comments

Comments
 (0)