1 #include "StrFns.h"
2
is_whitespace(const std::string & str)3 bool is_whitespace(const std::string& str)
4 {
5 for(size_t i=0; i<str.size(); i++)
6 if(str[i] != ' ' && str[i] != '\t')
7 return 0;
8
9 return 1;
10 }
11
into_whitespace(const std::string & str)12 std::string into_whitespace(const std::string& str)
13 {
14 std::string whitespace = "";
15
16 for(size_t i=0; i<str.size(); i++)
17 {
18 if(str[i] == '\t')
19 whitespace += "\t";
20 else
21 whitespace += " ";
22 }
23
24 return whitespace;
25 }
26
strip_leading_line(std::string & str)27 void strip_leading_line(std::string& str)
28 {
29 size_t pos = str.find_first_of('\n');
30
31 if(pos != std::string::npos)
32 str = str.substr(pos+1, str.size()-pos+1);
33 else
34 str = "";
35 }
36
strip_trailing_line(std::string & str)37 void strip_trailing_line(std::string& str)
38 {
39 size_t pos = str.find_last_of('\n');
40
41 if(pos != std::string::npos)
42 str = str.substr(0, pos);
43 else
44 str = "";
45 }
46
strip_leading_whitespace(std::string & str)47 void strip_leading_whitespace(std::string& str)
48 {
49 size_t pos=0;
50
51 for(; pos<str.size(); ++pos)
52 if(str[pos] != ' ' && str[pos] != '\t')
53 break;
54
55 str = str.substr(pos, str.size()-pos);
56 }
57
strip_leading_whitespace_multiline(std::string & str)58 void strip_leading_whitespace_multiline(std::string& str)
59 {
60 size_t pos=0;
61
62 for(; pos<str.size(); ++pos)
63 if(str[pos] != ' ' && str[pos] != '\t' && str[pos] != '\n')
64 break;
65
66 str = str.substr(pos, str.size()-pos);
67 }
68
strip_trailing_whitespace(std::string & str)69 void strip_trailing_whitespace(std::string& str)
70 {
71 int pos=str.size()-1;
72
73 for(; pos>=0; pos--)
74 if(str[pos] != ' ' && str[pos] != '\t')
75 break;
76
77 str = str.substr(0, pos+1);
78 }
79
strip_trailing_whitespace_multiline(std::string & str)80 void strip_trailing_whitespace_multiline(std::string& str)
81 {
82 int pos=str.size()-1;
83
84 for(; pos>=0; pos--)
85 if(str[pos] != ' ' && str[pos] != '\t' && str[pos] != '\n')
86 break;
87
88 str = str.substr(0, pos+1);
89 }
90
strip_surrounding_whitespace(std::string & str)91 void strip_surrounding_whitespace(std::string& str)
92 {
93 size_t spos=0, epos=str.size()-1;
94
95 for(; spos<str.size(); ++spos)
96 if(str[spos] != ' ' && str[spos] != '\t')
97 break;
98
99 if(spos > epos)
100 str = "";
101 else
102 {
103 for(; spos < epos; --epos)
104 if(str[epos] != ' ' && str[epos] != '\t')
105 break;
106
107 str = str.substr(spos, 1+epos-spos);
108 }
109 }
110
strip_surrounding_whitespace_multiline(std::string & str)111 void strip_surrounding_whitespace_multiline(std::string& str)
112 {
113 size_t spos=0, epos=str.size()-1;
114
115 for(; spos<str.size(); ++spos)
116 if(str[spos] != ' ' && str[spos] != '\t' && str[spos] != '\n')
117 break;
118
119 if(spos > epos)
120 str = "";
121 else
122 {
123 for(; spos < epos; --epos)
124 if(str[epos] != ' ' && str[epos] != '\t' && str[epos] != '\n')
125 break;
126
127 str = str.substr(spos, 1+epos-spos);
128 }
129 }
130
join(const std::vector<std::string> & vec,const std::string & str)131 std::string join(const std::vector<std::string>& vec, const std::string& str)
132 {
133 std::string ans = "";
134
135 if(vec.size())
136 {
137 ans += vec[0];
138 for(size_t v=1; v<vec.size(); v++)
139 ans += str + vec[v];
140 }
141
142 return ans;
143 }
144
join(const std::vector<std::string> & vec,const std::string & str,const size_t & spos)145 std::string join(const std::vector<std::string>& vec, const std::string& str, const size_t& spos)
146 {
147 std::string ans = "";
148
149 if(vec.size() > spos)
150 {
151 ans += vec[spos];
152 for(size_t v=spos+1; v<vec.size(); v++)
153 ans += str + vec[v];
154 }
155
156 return ans;
157 }
158
findAndReplaceAll(const std::string & orig,const std::string & toSearch,const std::string & replaceStr)159 std::string findAndReplaceAll(const std::string& orig, const std::string& toSearch, const std::string& replaceStr)
160 {
161 std::string data = orig;
162
163 // Get the first occurrence
164 size_t pos = data.find(toSearch);
165
166 // Repeat till end is reached
167 while( pos != std::string::npos)
168 {
169 // Replace this occurrence of Sub String
170 data.replace(pos, toSearch.size(), replaceStr);
171 // Get the next occurrence from the current position
172 pos =data.find(toSearch, pos + replaceStr.size());
173 }
174
175 return data;
176 }
177