summaryrefslogtreecommitdiffstats
path: root/StringTools/src/StringTools.cpp
blob: 8828f729d9e82fc5e5efcdc5650b628859e4284e (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
#include "StringTools/StringTools.h"
#include <sstream>

namespace Leonetienne::StringTools {

  std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) {
      std::stringstream ss;

      for (std::size_t i = 0; i < str.length(); i++)
      {
          if (str[i] != find)
              ss << str[i];
          else
              ss << subst;
      }

      return ss.str();
  }

  std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) {
      if (find.length() == 0)
          return str;

      std::stringstream ss;

      std::size_t posFound = 0;
      std::size_t lastFound = 0;

      while (posFound != std::string::npos)
      {
          lastFound = posFound;
          posFound = str.find(find, posFound);

          if (posFound != std::string::npos)
          {
              ss << str.substr(lastFound, posFound - lastFound) << subst;
              posFound += find.length();
          }
          else
          {
              ss << str.substr(lastFound, (str.length()) - lastFound);
          }
      }

      return ss.str();
  }

  std::string StringTools::Replace(const std::string& str, const char find, const char subst) {
      std::stringstream ss;
      ss << subst;

      return Replace(str, find, ss.str());
  }

  std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) {
      std::stringstream ss;
      ss << subst;

      return Replace(str, find, ss.str());
  }

  std::string StringTools::Lower(const std::string& str) {
      std::stringstream ss;

      for (std::size_t i = 0; i < str.size(); i++)
      {
          const char c = str[i];

          // Quick-accept: regular letters
          if ((c >= 'A') && (c <= 'Z'))
              ss << (char)(c | 32);

              // Else: keep the character as is
          else ss << c;
      }

      return ss.str();
  }

  std::string StringTools::Upper(const std::string& str) {
      std::stringstream ss;

      for (std::size_t i = 0; i < str.size(); i++)
      {
          const char c = str[i];

          // Quick-accept: regular letters
          if ((c >= 'a') && (c <= 'z'))
              ss << (char)(c & ~32);

              // Else: keep the character as is
          else ss << c;
      }

      return ss.str();
  }

  std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
      std::vector<std::string> toRet;
      // Quick-accept: str length is 0
      if (str.length() == 0)
        toRet.push_back("");

      // Quick-accept: seperator length is 0
      else if (seperator.length() == 0) {
        for (const char c : str)
          toRet.push_back(std::string(&c, (&c) + 1));
      }

      else {
        std::size_t idx = 0;
        while (idx != std::string::npos) {
          std::size_t lastIdx = idx;
          idx = str.find(seperator, idx);

          // Grab our substring until the next finding of sep
          if (idx != std::string::npos) {
            toRet.push_back(str.substr(
              lastIdx,
              idx - lastIdx
            ));

            idx += seperator.length();
          }
          // No more seperator found. Grab the rest until the end of the string
          else {
            toRet.push_back(str.substr(
               lastIdx
            ));
          }
        }
      }

      return toRet;
  }

  std::string StringTools::PadLeft(const std::string& str, const char pad, const std::size_t len) {
    std::stringstream ss;

    for (std::size_t i = str.length(); i < len; i++)
      ss << pad;

    ss << str;

    return ss.str();
  }

  std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) {
    std::stringstream ss;

    ss << str;

    for (std::size_t i = str.length(); i < len; i++)
      ss << pad;

    return ss.str();
  }

}