diff --git a/source/dub/dub.d b/source/dub/dub.d index 5226a79..6b48aba 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -734,6 +734,10 @@ */ string getModuleNameFromContent(string content) { import std.regex; + import std.string; + + content = content.strip; + if (!content.length) return null; static bool regex_initialized = false; static Regex!char comments_pattern, module_pattern; @@ -745,7 +749,10 @@ } content = replaceAll(content, comments_pattern, ""); - string moduleName = matchFirst(content, module_pattern).front; + auto result = matchFirst(content, module_pattern); + + string moduleName; + if(!result.empty) moduleName = result.front; if (moduleName.length >= 7) moduleName = moduleName[7..$-1]; @@ -753,9 +760,12 @@ } unittest { - import std.stdio; + //test empty string + string name = getModuleNameFromContent(""); + assert(name == "", "can't get module name from empty string"); + //test simple name - string name = getModuleNameFromContent("module myPackage.myModule;"); + name = getModuleNameFromContent("module myPackage.myModule;"); assert(name == "myPackage.myModule", "can't parse module name"); //test if it can ignore module inside comments @@ -778,6 +788,7 @@ string getModuleNameFromFile(string filePath) { string fileContent = filePath.readText; + logDiagnostic("Get module name from path: " ~ filePath); return getModuleNameFromContent(fileContent); }