-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.h
More file actions
129 lines (109 loc) · 3.11 KB
/
Copy pathUtil.h
File metadata and controls
129 lines (109 loc) · 3.11 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include <clang-c/Index.h>
#include <string>
namespace metagen {
inline std::string jsifySelector(const std::string& selector) {
std::string jsifiedSelector;
jsifiedSelector.reserve(selector.size());
bool nextupper = false;
for (auto c : selector) {
if (c == ':') {
nextupper = true;
} else if (nextupper) {
jsifiedSelector += toupper(c);
nextupper = false;
} else {
jsifiedSelector += c;
}
}
return jsifiedSelector;
}
inline std::string jsifyName(const std::string& name) {
if (name == "arguments" || name == "function" || name == "DOMException") {
return name + "$";
} else {
return name;
}
}
inline std::vector<std::string> splitCamelCase(const std::string& value) {
std::vector<std::string> result;
result.reserve(value.size() / 4 + 1);
std::string current;
current.reserve(value.size());
for (auto c : value) {
if (isupper(c)) {
if (!current.empty()) {
result.emplace_back(current);
}
current = "";
}
current += c;
}
if (!current.empty()) {
result.emplace_back(current);
}
return result;
}
inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { return !std::isspace(ch); })
.base(),
s.end());
return s;
}
inline std::string stripCInfo(std::string val) {
if (val.find("const ") == 0) {
val = val.substr(6);
}
if (val.find("__kindof ") == 0) {
val = val.substr(9);
}
if (val.find("struct ") == 0) {
val = val.substr(7);
}
if (val.find("enum ") == 0) {
val = val.substr(5);
}
if (val.find("union ") == 0) {
val = val.substr(6);
}
return val;
}
inline std::string transformStructName(std::string name) {
auto find = name.find("unnamed at");
if (find == std::string::npos) {
find = name.find("anonymous at");
}
if (find != std::string::npos) {
// hash the name
std::hash<std::string> hasher;
name = "unnamed_" + std::to_string(hasher(name.substr(find)));
}
return name;
}
inline std::string getFrameworkName(CXCursor cursor) {
CXSourceLocation srcloc = clang_getCursorLocation(cursor);
CXFile file;
clang_getFileLocation(srcloc, &file, nullptr, nullptr, nullptr);
CXString fileName = clang_getFileName(file);
std::string fileNameStr = clang_getCString(fileName);
clang_disposeString(fileName);
auto pos = fileNameStr.find(".framework/");
if (pos == std::string::npos) {
return "Runtime";
}
std::string frameworkName = fileNameStr.substr(0, pos);
frameworkName = frameworkName.substr(frameworkName.find_last_of("/") + 1);
return frameworkName;
}
inline bool isAvailable(CXCursor cursor) {
CXAvailabilityKind availability = clang_getCursorAvailability(cursor);
return availability == CXAvailability_Available ||
availability == CXAvailability_Deprecated;
}
inline bool isSelectorOwned(const std::string& selectorName) {
return selectorName.find("copy") == 0 ||
selectorName.find("mutableCopy") == 0 ||
selectorName.find("new") == 0 || selectorName.find("alloc") == 0;
}
} // namespace metagen