summaryrefslogtreecommitdiff
path: root/src/bin/eolian_js/eolian/js/format.hh
blob: 5e940d12bac0f1bf3d61c1b5d32f4aa0ecf9b4f9 (plain)
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
#ifndef EOLIAN_JS_FORMAT_HH
#define EOLIAN_JS_FORMAT_HH

#include <eolian/js/domain.hh>

#include <algorithm>
#include <string>
#include <cctype>

namespace eolian { namespace js {

namespace format {

const char* verbs[] =
  {
    "add",
    "get",
    "is",
    "del",
    "thaw",
    "freeze",
    "save",
    "wait",
    "eject",
    "raise",
    "lower",
    "load",
    "dup",
    "reset",
    "unload",
    "close",
    "set",
    "interpolate",
    "has",
    "grab",
    "check",
    "find",
    "ungrab",
    "unset",
    "clear",
    "pop",
    "new",
    "peek",
    "push",
    "update",
    "show",
    "move",
    "hide",
    "calculate",
    "resize",
    "attach",
    "pack",
    "unpack"
  };

const char* not_verbs[] =
  {
    "below",
    "above",
    "name",
    "unfreezable",
    "value",
    "r",
    "g",
    "b",
    "a",
    "finalize",
    "destructor",
    "to",
    "circle",
    "rect",
    "path",
    "commands",
    "type",
    "colorspace"
    "op",
    "type",
    "properties",
    "status",
    "status",
    "relative",
    "ptr",
    "pair",
    "pos"
  };
  
std::string format_method(std::string const& in)
{
   std::string r;
   std::string::const_iterator current = in.begin(), last = in.end();
   do
     {
       std::string::const_iterator word_end = std::find(current, last, '_');
       if(word_end == last)
         {
           bool found_verb = false, found_not_verb = false;
           std::string v(current, word_end);
           v[0] = std::toupper(v[0]);
           for(const char** verb = &format::verbs[0]; verb != &format::verbs
                 [sizeof(format::verbs)/sizeof(format::verbs[0])]; ++verb)
             {
               if(!std::lexicographical_compare
                  (current, word_end, *verb, *verb + std::strlen(*verb))
                  && !std::lexicographical_compare
                  (*verb, *verb + std::strlen(*verb), current, word_end))
                 {
                   found_verb = true;
                 }
             }
           if(!found_verb)
           {
             for(const char** not_verb = &format::not_verbs[0]; not_verb != &format::not_verbs
                   [sizeof(format::not_verbs)/sizeof(format::not_verbs[0])]; ++not_verb)
               {
                 if(!std::lexicographical_compare
                    (current, word_end, *not_verb, *not_verb + std::strlen(*not_verb))
                    && !std::lexicographical_compare
                    (*not_verb, *not_verb + std::strlen(*not_verb), current, word_end))
                   {
                     found_not_verb = true;
                   }
               }
             if(!found_not_verb)
               EINA_CXX_DOM_LOG_WARN(eolian::js::domain)
                 << "Last word is NOT a not-verb " << v << std::endl;
           }
           if(found_verb || !found_not_verb)
               r = v + r;
           else
               r += v;
           current = last;
         }
       else
         {
           r += std::toupper(*current++);
           std::copy(current, word_end, std::back_inserter(r));
           current = word_end + 1;
         }
     }
   while(current != last);

   EINA_CXX_DOM_LOG_DBG(eolian::js::domain)
     << "Formatted method " << r << " with input " << in << std::endl;
   
   return r;
}

std::string format_field(std::string const& in)
{
  return format_method(in);
}

std::string format_class(std::string const& in)
{
   std::string r;
   std::string::const_iterator current = in.begin(), last = in.end();
   std::copy_if(current, last, std::back_insert_iterator<std::string>(r),
                [] (char c)
                {
                   return c != '_';
                });
   return r;
}

std::string format_namespace(std::string const& in)
{
  return format_class(in);
}

std::string format_struct(std::string const& in)
{
  return format_class(in);
}

std::string format_enum(std::string const& in)
{
  return format_class(in);
}

std::string constant(std::string in)
{
   std::transform(in.begin(), in.end(), in.begin(), ::toupper);
   return in;
}

}

} }

#endif