summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/utils.hh
blob: 9e583df4f9f5b73d439b2ec83a8af718e68901b3 (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
#ifndef EOLIAN_MONO_UTILS_HPP
#define EOLIAN_MONO_UTILS_HPP

#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>

/* Compared to the helpers.hh and name_helpers headers, these functions are
 * lower level, not dealing with binding-specific structures or knowledge */

namespace eolian_mono { namespace utils {

   // Helper method to avoid multiple as_generator calls when mixing case strings
   inline std::string to_uppercase(std::string s)
   {
       std::transform(s.begin(), s.end(), s.begin(), ::toupper);
       return s;
   }
   inline std::string to_lowercase(std::string s)
   {
       std::transform(s.begin(), s.end(), s.begin(), ::tolower);
       return s;
   }
   inline std::string capitalize(std::string const &s)
   {
      std::string ret = s;
      ret[0] = std::toupper(ret[0]);
      return ret;
   }

   std::vector<std::string> split(std::string const &input, std::string delims)
   {
      std::vector<std::string> names;
      size_t pos = 0;

      while(pos != std::string::npos)
        {
           size_t newpos = input.find_first_of(delims, pos);
           names.push_back(input.substr(pos, newpos-pos));
           pos = newpos;

           if (pos != std::string::npos)
             pos++;
        }

      return names;
   }

   std::vector<std::string> split(std::string const &input, char delim)
   {
      return split(input, {1, delim});
   }


   std::string to_pascal_case(const std::vector<std::string> &names, std::string const& delim="")
   {
     std::vector<std::string> outv(names.size());
        std::stringstream osstream;

        std::transform(names.begin(), names.end(), outv.begin(),
              [](std::string name) {
                name[0] = std::toupper(name[0]);
                return name;
              });

        std::copy(outv.begin(), outv.end(), std::ostream_iterator<std::string>(osstream, delim.c_str()));

        std::string ret = osstream.str();

        if (delim != "")
            ret.pop_back(); // We could implement an infix_iterator but this pop is enough for now.

        return ret;
    }

   inline std::string remove_all(std::string name, char target)
   {
       name.erase(std::remove(name.begin(), name.end(), target), name.end());
       return name;
   }

   inline bool ends_with(std::string const& source, std::string suffix)
   {
       if (source.size() > suffix.size())
           return (0 == source.compare(source.size() - suffix.size(), suffix.size(), suffix));
       else
           return false;
   }

   inline std::string replace_all(std::string s, std::string target, std::string replace)
   {
       size_t pos = s.find(target);

       while (pos != std::string::npos)
       {
           s.replace(pos, target.length(), replace);
           pos += replace.length();
           pos = s.find(target, pos);
       }

       return s;
   }
} }

#endif