-
Notifications
You must be signed in to change notification settings - Fork 7
/
module_util.cc
58 lines (41 loc) · 1.48 KB
/
module_util.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
//
// See the file "COPYING" in the main distribution directory for copyright.
#include "module_util.h"
#include <string.h>
#include <string>
static int streq(const char* s1, const char* s2) { return ! strcmp(s1, s2); }
// Returns it without trailing "::".
string extract_module_name(const char* name) {
string module_name = name;
string::size_type pos = module_name.rfind("::");
if ( pos == string::npos )
return string(GLOBAL_MODULE_NAME);
module_name.erase(pos);
return module_name;
}
string extract_var_name(const char* name) {
string var_name = name;
string::size_type pos = var_name.rfind("::");
if ( pos == string::npos )
return var_name;
if ( pos + 2 > var_name.size() )
return string("");
return var_name.substr(pos + 2);
}
string normalized_module_name(const char* module_name) {
int mod_len;
if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") )
mod_len -= 2;
return string(module_name, mod_len);
}
string make_full_var_name(const char* module_name, const char* var_name) {
if ( ! module_name || streq(module_name, GLOBAL_MODULE_NAME) || strstr(var_name, "::") ) {
if ( streq(GLOBAL_MODULE_NAME, extract_module_name(var_name).c_str()) )
return extract_var_name(var_name);
return string(var_name);
}
string full_name = normalized_module_name(module_name);
full_name += "::";
full_name += var_name;
return full_name;
}