Line data Source code
1 : #include "kiss/string.h"
2 :
3 : #include <iostream>
4 : #include <string>
5 : #include <vector>
6 :
7 : namespace kiss {
8 :
9 0 : std::string trim_right(const std::string &s, const std::string &t) {
10 : std::string::size_type i(s.find_last_not_of(t));
11 :
12 0 : if (i == std::string::npos)
13 0 : return "";
14 : else
15 0 : return std::string(s, 0, i);
16 : }
17 :
18 0 : std::string trim_left(const std::string &s, const std::string &t) {
19 0 : return std::string(s, s.find_first_not_of(t));
20 : }
21 :
22 0 : std::string trim(const std::string &s, const std::string &t) {
23 : std::string::size_type a = s.find_first_not_of(t);
24 : std::string::size_type b = s.find_last_not_of(t);
25 :
26 0 : if (a == std::string::npos || b == std::string::npos)
27 0 : return "";
28 :
29 0 : return std::string(s, a, b - a + 1);
30 : }
31 :
32 0 : void explode(const std::string &s, std::vector<std::string> &v,
33 : const bool trim_spaces, const std::string &t) {
34 : std::string::size_type a, b;
35 :
36 : a = s.find_first_not_of(t), b = s.find_first_of(t, a);
37 :
38 0 : while (a != std::string::npos) {
39 0 : if (trim_spaces)
40 0 : v.push_back(trim(s.substr(a, b - a)));
41 : else
42 0 : v.push_back(s.substr(a, b - a));
43 :
44 : a = s.find_first_not_of(t, b), b = s.find_first_of(t, a);
45 : }
46 0 : }
47 :
48 0 : std::string implode(const std::vector<std::string> &v, const std::string &t) {
49 : unsigned int i;
50 : std::string s;
51 :
52 0 : for (i = 0; i < (v.size() - 1); i++) {
53 : s.append(v[i]);
54 : s.append(t);
55 : }
56 :
57 0 : return s + v[i];
58 : }
59 :
60 3 : bool ends_with(const std::string &s, const std::string &w) {
61 3 : if (s.size() < w.size())
62 : return false;
63 : std::string::const_reverse_iterator si = s.rbegin();
64 : std::string::const_reverse_iterator wi = w.rbegin();
65 3 : while (wi != w.rend()) {
66 3 : if (*wi != *si)
67 : return false;
68 : wi++;
69 : si++;
70 : }
71 :
72 : return true;
73 : }
74 :
75 0 : bool starts_with(const std::string &s, const std::string &w) {
76 0 : if (s.size() < w.size())
77 : return false;
78 : std::string::const_iterator si = s.begin();
79 : std::string::const_iterator wi = w.begin();
80 0 : while (wi != w.end()) {
81 0 : if (*wi != *si)
82 : return false;
83 : wi++;
84 : si++;
85 : }
86 : return true;
87 : }
88 :
89 : } // namespace kiss
|