-
Notifications
You must be signed in to change notification settings - Fork 0
/
enry.h
290 lines (235 loc) · 9.11 KB
/
enry.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// enry.h
#pragma once
#include <go-enry.h>
#include <cstring>
#include <string_view>
#include <string>
#include <tuple>
#include <vector>
namespace {
inline GoString to_GoString(std::string_view str)
{
GoString go_string = {
.p = str.data(),
.n = static_cast<ptrdiff_t>(str.length())
};
return go_string;
}
inline GoSlice to_GoByteSlice(std::string_view str)
{
GoSlice go_slice = {
.data = const_cast<char*>(str.data()),
.len = static_cast<GoInt>(str.length()),
.cap = static_cast<GoInt>(str.length())
};
return go_slice;
}
inline std::vector<std::string> from_GoStringSlice(const GoSlice& go_slice)
{
std::vector<std::string> vec(go_slice.len);
auto data = static_cast<char**>(go_slice.data);
for (GoInt i = 0; i < go_slice.len; ++i) {
vec[i].assign(data[i]);
if (data[i]) {
free(data[i]);
data[i] = nullptr;
}
}
return vec;
}
inline GoSlice to_GoStringSlice(const std::initializer_list<std::string>& lst)
{
constexpr std::size_t go_string_size = sizeof(GoString);
const std::size_t lst_size = lst.size();
GoSlice go_slice = {
.data = calloc(lst_size, go_string_size),
.len = static_cast<GoInt>(lst_size),
.cap = static_cast<GoInt>(lst_size)
};
for (std::size_t i = 0; i < lst_size; ++i) {
const auto str = lst.begin() + i;
GoString go_string = {
.p = str->c_str(),
.n = static_cast<ptrdiff_t>(str->length())
};
memcpy(reinterpret_cast<char*>(go_slice.data) + (i * go_string_size), &go_string, go_string_size);
}
return go_slice;
}
} // namespace
namespace enry {
std::string GetLanguage(std::string_view filename, std::string_view content)
{
GoString go_string = GetLanguage(to_GoString(filename), to_GoByteSlice(content));
return std::string(go_string.p, go_string.n);
}
std::tuple<std::string, bool> GetLanguageByContent(std::string_view filename, std::string_view content)
{
GetLanguageByContent_return go_return = GetLanguageByContent(to_GoString(filename), to_GoByteSlice(content));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByEmacsModeline(std::string_view content)
{
GetLanguageByEmacsModeline_return go_return = GetLanguageByEmacsModeline(to_GoByteSlice(content));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByExtension(std::string_view filename)
{
GetLanguageByExtension_return go_return = GetLanguageByExtension(to_GoString(filename));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByFilename(std::string_view filename)
{
GetLanguageByFilename_return go_return = GetLanguageByFilename(to_GoString(filename));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByModeline(std::string_view content)
{
GetLanguageByModeline_return go_return = GetLanguageByModeline(to_GoByteSlice(content));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByShebang(std::string_view content)
{
GetLanguageByShebang_return go_return = GetLanguageByShebang(to_GoByteSlice(content));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::tuple<std::string, bool> GetLanguageByVimModeline(std::string_view content)
{
GetLanguageByVimModeline_return go_return = GetLanguageByVimModeline(to_GoByteSlice(content));
return std::make_tuple(std::string(go_return.r0.p, go_return.r0.n), bool(go_return.r1));
}
std::vector<std::string> GetLanguageExtensions(std::string_view language)
{
GoSlice go_slice{};
GetLanguageExtensions(to_GoString(language), &go_slice);
return from_GoStringSlice(go_slice);
}
std::vector<std::string> GetLanguages(std::string_view filename, std::string_view content)
{
GoSlice go_slice{};
GetLanguages(to_GoString(filename), to_GoByteSlice(content), &go_slice);
return from_GoStringSlice(go_slice);
}
std::vector<std::string> GetLanguagesByContent(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByContent(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByEmacsModeline(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByEmacsModeline(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByExtension(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByExtension(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByFilename(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByFilename(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByModeline(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByModeline(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByShebang(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByShebang(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::vector<std::string> GetLanguagesByVimModeline(std::string_view filename, std::string_view content, const std::initializer_list<std::string>& candidates = {})
{
GoSlice go_candidates = to_GoStringSlice(candidates);
GoSlice go_slice{};
GetLanguagesByVimModeline(to_GoString(filename), to_GoByteSlice(content), go_candidates, &go_slice);
std::vector<std::string> vec = from_GoStringSlice(go_slice);
free(go_candidates.data);
go_candidates.data = nullptr;
return vec;
}
std::string GetMimeType(std::string_view path, std::string_view language)
{
GoString go_string = GetMimeType(to_GoString(path), to_GoString(language));
return std::string(go_string.p, go_string.n);
}
bool IsBinary(const char *data, std::size_t size)
{
GoSlice go_slice = {
.data = const_cast<char*>(data),
.len = static_cast<GoInt>(size),
.cap = static_cast<GoInt>(size)
};
return bool(IsBinary(go_slice));
}
bool IsConfiguration(std::string_view path)
{
return bool(IsConfiguration(to_GoString(path)));
}
bool IsDocumentation(std::string_view path)
{
return bool(IsDocumentation(to_GoString(path)));
}
bool IsDotFile(std::string_view path)
{
return bool(IsDotFile(to_GoString(path)));
}
bool IsImage(std::string_view path)
{
return bool(IsImage(to_GoString(path)));
}
bool IsVendor(std::string_view path)
{
return bool(IsVendor(to_GoString(path)));
}
bool IsGenerated(std::string_view path, std::string_view content)
{
return bool(IsGenerated(to_GoString(path), to_GoByteSlice(content)));
}
std::string GetColor(std::string_view language)
{
GoString go_string = GetColor(to_GoString(language));
return std::string(go_string.p, go_string.n);
}
bool IsTest(std::string_view path)
{
return bool(IsTest(to_GoString(path)));
}
std::string GetLanguageType(std::string_view language)
{
GoString go_string = GetLanguageType(to_GoString(language));
return std::string(go_string.p, go_string.n);
}
} // namespace enry