Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "common.hpp"
10 :
11 218 : void escapeNonAlphaNum (std::string & str)
12 : {
13 1308 : std::replace_if (str.begin (), str.end (), std::not1 (std::ptr_fun (isalnum)), '_');
14 218 : }
15 :
16 158 : std::vector<std::string> getKeyParts (const kdb::Key & key)
17 : {
18 158 : auto rawName = static_cast<const char *> (ckdb::keyUnescapedName (key.getKey ()));
19 158 : size_t size = ckdb::keyGetUnescapedNameSize (key.getKey ());
20 632 : std::vector<char> name (rawName, rawName + size);
21 158 : auto cur = name.begin ();
22 : std::vector<std::string> parts;
23 3871 : while (cur != name.end ())
24 : {
25 2370 : auto next = std::find (cur, name.end (), '\0');
26 1185 : parts.emplace_back (cur, next);
27 1185 : cur = next + 1;
28 : }
29 158 : return parts;
30 : }
31 :
32 109 : bool hasType (const kdb::Key & key)
33 : {
34 840 : return key.hasMeta ("type") && !key.getMeta<std::string> ("type").empty ();
35 : }
36 :
37 101 : std::string getType (const kdb::Key & key)
38 : {
39 404 : return key.getMeta<std::string> ("type");
40 : }
41 :
42 106 : std::string getTagName (std::string name)
43 : {
44 212 : name = std::regex_replace (name, std::regex ("/[#_]/"), "/");
45 212 : name = std::regex_replace (name, std::regex ("[#_]/"), "/");
46 212 : name = std::regex_replace (name, std::regex ("/[#_]"), "/");
47 212 : name = std::regex_replace (name, std::regex (R"(/%(([^/]|(\\\\)*\\/|(\\\\)+)+)%/)"), "/$1/");
48 :
49 212 : if (name[name.length () - 1] == '/')
50 : {
51 24 : name.erase (name.length () - 1);
52 : }
53 :
54 106 : escapeNonAlphaNum (name);
55 :
56 106 : return name;
57 : }
58 :
59 106 : std::string getTagName (const kdb::Key & key, const std::string & parentKey)
60 : {
61 212 : auto name = key.getName ();
62 106 : name.erase (0, parentKey.length () + 1);
63 :
64 318 : return getTagName (name);
65 : }
66 :
67 208 : std::string snakeCaseToCamelCase (const std::string & s, bool upper)
68 : {
69 208 : std::string result;
70 416 : result.resize (s.size ());
71 208 : auto upcase = upper;
72 1420 : std::transform (s.begin (), s.end (), result.begin (), [&upcase](char c) {
73 1420 : int x = upcase ? toupper (c) : c;
74 1420 : upcase = c == '_';
75 : return x;
76 832 : });
77 1456 : result.erase (std::remove (result.begin (), result.end (), '_'), result.end ());
78 208 : return result;
79 : }
80 :
81 192 : std::string snakeCaseToPascalCase (const std::string & s)
82 : {
83 192 : return snakeCaseToCamelCase (s, true);
84 : }
85 :
86 94 : std::string snakeCaseToMacroCase (const std::string & s)
87 : {
88 94 : std::string result;
89 188 : result.resize (s.size ());
90 376 : std::transform (s.begin (), s.end (), result.begin (), ::toupper);
91 94 : return result;
92 : }
93 :
94 48 : std::string camelCaseToMacroCase (const std::string & s)
95 : {
96 96 : std::stringstream ss;
97 614 : std::for_each (s.begin (), s.end (), [&ss](char c) {
98 302 : if (ss.tellp () != std::stringstream::beg && isupper (c))
99 : {
100 10 : ss << '_';
101 : }
102 906 : ss << static_cast<char> (toupper (c));
103 494 : });
104 96 : return ss.str ();
105 : }
106 :
107 0 : std::string upCaseFirst (const std::string & str)
108 : {
109 0 : std::string result = str;
110 0 : result[0] = toupper (result[0]);
111 0 : return result;
112 : }
|