This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
FileCategories.d
66 lines (58 loc) · 1.76 KB
/
FileCategories.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2014- Facebook
// License: Boost License 1.0, http://boost.org/LICENSE_1_0.txt
// @author Andrei Alexandrescu ([email protected])
import std.algorithm, std.range;
immutable string[]
extsHeader = [ ".h", ".hpp", ".hh", ],
extsSourceC = [ ".c", ],
extsSourceCpp = [ ".C", ".cc", ".cpp", ".CPP", ".c++", ".cp", ".cxx", ];
enum FileCategory {
header, inl_header, source_c, source_cpp, unknown,
};
FileCategory getFileCategory(string fpath) {
foreach (ext; extsHeader) {
if (fpath.endsWith(chain("-inl", ext))) return FileCategory.inl_header;
if (fpath.endsWith(ext)) return FileCategory.header;
}
foreach (ext; extsSourceC) {
if (fpath.endsWith(ext)) {
return FileCategory.source_c;
}
}
foreach (ext; extsSourceCpp) {
if (fpath.endsWith(ext)) {
return FileCategory.source_cpp;
}
}
return FileCategory.unknown;
}
bool isHeader(string fpath) {
auto fileCategory = getFileCategory(fpath);
return fileCategory == FileCategory.header ||
fileCategory == FileCategory.inl_header;
}
bool isSource(string fpath) {
auto fileCategory = getFileCategory(fpath);
return fileCategory == FileCategory.source_c ||
fileCategory == FileCategory.source_cpp;
}
bool isTestFile(string fpath) {
import std.string : toLower;
return !fpath.toLower.find("test").empty;
}
string getFileNameBase(string filename) {
foreach (ext; extsHeader) {
auto inlExt = "-inl" ~ ext;
if (filename.endsWith(inlExt)) {
return filename[0 .. $ - inlExt.length];
} else if (filename.endsWith(ext)) {
return filename[0 .. $ - ext.length];
}
}
foreach (ext; chain(extsSourceC, extsSourceCpp)) {
if (filename.endsWith(ext)) {
return filename[0 .. $ - ext.length];
}
}
return filename;
}