-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters.cc
361 lines (302 loc) · 7.03 KB
/
counters.cc
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: GPL-2.0+
/*
* Fast Lines of Code Counter
*
* Copyright (C) 2021 SUSE
*
* Author: Jörg Rödel <[email protected]>
*/
#include "classifier.h"
#include "counters.h"
struct ml_comment {
const char *start;
const char *end;
};
struct src_spec {
struct ml_comment ml_comment;
const char *sl_comment[];
};
struct src_spec c_spec {
.ml_comment = {
.start = "/*",
.end = "*/"
},
.sl_comment = { "//", nullptr },
};
struct src_spec asm_spec {
.ml_comment = {
.start = "/*",
.end = "*/"
},
.sl_comment = { "#", nullptr },
};
struct src_spec python_spec {
.ml_comment = {
.start = "\"\"\"",
.end = "\"\"\""
},
.sl_comment = { "#", nullptr },
};
struct src_spec shell_spec {
.ml_comment = {
.start = nullptr,
.end = nullptr
},
.sl_comment = { "#", nullptr },
};
struct src_spec xml_spec {
.ml_comment = {
.start = "<!--",
.end = "-->"
},
.sl_comment = { },
};
struct src_spec latex_spec {
.ml_comment = {
.start = nullptr,
.end = nullptr
},
.sl_comment = { "%", nullptr },
};
struct src_spec text_spec {
.ml_comment = {
.start = nullptr,
.end = nullptr
},
.sl_comment = { nullptr },
};
struct src_spec asn1_spec {
.ml_comment = {
.start = nullptr,
.end = nullptr
},
.sl_comment = { "--", nullptr },
};
struct src_spec rust_spec {
.ml_comment = {
.start = nullptr,
.end = nullptr
},
.sl_comment = { "//", nullptr },
};
struct src_spec css_spec {
.ml_comment = {
.start = "/*",
.end = "*/"
},
.sl_comment = { nullptr },
};
struct src_spec ruby_spec {
.ml_comment = {
.start = "=begin",
.end = "=end"
},
.sl_comment = { "#", nullptr },
};
enum state {
BEGIN,
STRING,
SLCOMMENT,
MLCOMMENT,
};
file_result::file_result(std::string n)
: code(0), comment(0), whitespace(0), duplicate(false),
type(file_type::unknown), name(n)
{
}
/*
* Use static variants for strlen and strncmp for performance reasons.
* Performance counting LOC for C/C++ dropped by factor 4 using the
* variants from the C library.
*/
static bool str_eq(const char *s1, const char *s2, size_t len)
{
for (size_t i = 0; i < len; ++i) {
if (s1[i] != s2[i])
return false;
}
return true;
}
static size_t str_len(const char *s)
{
size_t len = 0;
for (len = 0; s[len] != 0; ++len);
return len;
}
static bool sl_comment_start(const struct src_spec &spec,
const char *buffer, size_t index, size_t size, size_t &l)
{
size_t i = 0;
while (spec.sl_comment[i] != nullptr) {
const auto pattern = spec.sl_comment[i++];
auto len = str_len(pattern);
auto buf_len = size - index;
if (len > buf_len)
continue;
if (str_eq(pattern, &buffer[index], len)) {
l = len;
return true;
}
}
return false;
}
static bool ml_comment_start(const struct src_spec &spec,
const char *buffer, size_t index, size_t size, size_t &l)
{
const auto str = spec.ml_comment.start;
if (str == nullptr)
return false;
auto len = str_len(str);
auto buf_len = size - index;
l = len;
return ((len <= buf_len) && str_eq(str, &buffer[index], len));
}
static bool ml_comment_end(const struct src_spec &spec,
const char *buffer, size_t index, size_t size, size_t &l)
{
const auto str = spec.ml_comment.end;
if (str == nullptr)
return false;
auto len = str_len(str);
auto buf_len = size - index;
l = len;
return ((len <= buf_len) && str_eq(str, &buffer[index], len));
}
static void finish_line(struct file_result &r, bool &code, bool &comment, size_t &counter)
{
if (counter == 0)
return;
if (code)
r.code += 1;
else if (comment)
r.comment += 1;
else
r.whitespace += 1;
counter = 0;
}
static void generic_count_source(const struct src_spec &spec,
struct file_result &r,
const char *buffer,
size_t size)
{
bool code = false, comment = false;
enum state state = BEGIN;
size_t counter = 0, len;
char c = 0, lc;
if (size == 0)
return;
for (size_t index = 0; index < size; ++index, ++counter) {
lc = c;
c = buffer[index];
if (state == BEGIN) {
if (ml_comment_start(spec, buffer, index, size, len)) {
comment = true;
state = MLCOMMENT;
index += len - 1;
} else if (sl_comment_start(spec, buffer, index, size, len)) {
comment = true;
state = SLCOMMENT;
index += len - 1;
} else if (c == '"') {
code = true;
state = STRING;
} else if (c == '\n') {
finish_line(r, code, comment, counter);
code = comment = false;
} else if (!isspace(c) && c != '/') {
code = true;
}
} else if (state == STRING) {
if (c == '"' && lc != '\\') {
state = BEGIN;
} else if (c == '\n') {
finish_line(r, code, comment, counter);
comment = false;
code = true;
}
} else if (state == SLCOMMENT) {
if (c == '\n') {
finish_line(r, code, comment, counter);
code = comment = false;
state = BEGIN;
}
} else if (state == MLCOMMENT) {
if (ml_comment_end(spec, buffer, index, size, len)) {
state = BEGIN;
index += len - 1;
} else if (c == '\n') {
finish_line(r, code, comment, counter);
code = false;
comment = true;
}
}
}
// Finish the last line
if (c != '\n')
finish_line(r, code, comment, counter);
}
static size_t perl_strip__END__(const char *buffer, size_t size)
{
const char *pattern = "__END__";
size_t pattern_len = 7;
for (size_t ret = 0; ret < size; ++ret) {
if (buffer[ret] != '\n')
continue;
auto remain = size - ret;
if (remain < pattern_len)
break;
if (str_eq(&buffer[ret + 1], pattern, pattern_len))
return ret - 1;
}
return size;
}
void count_c(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(c_spec, r, buffer, size);
}
void count_asm(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(asm_spec, r, buffer, size);
}
void count_python(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(python_spec, r, buffer, size);
}
void count_perl(struct file_result &r, const char *buffer, size_t size)
{
// Perl needs some pre-processing
size = perl_strip__END__(buffer, size);
generic_count_source(shell_spec, r, buffer, size);
}
void count_xml(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(xml_spec, r, buffer, size);
}
void count_shell(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(shell_spec, r, buffer, size);
}
void count_latex(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(latex_spec, r, buffer, size);
}
void count_text(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(text_spec, r, buffer, size);
}
void count_asn1(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(asn1_spec, r, buffer, size);
}
void count_rust(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(rust_spec, r, buffer, size);
}
void count_css(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(css_spec, r, buffer, size);
}
void count_ruby(struct file_result &r, const char *buffer, size_t size)
{
generic_count_source(ruby_spec, r, buffer, size);
}