|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: |
| 4 | +
|
| 5 | +Author: Daniel Poetzl |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <cassert> |
| 10 | +#include <stdexcept> |
| 11 | + |
| 12 | +#include "string_utils.h" |
| 13 | + |
| 14 | +/*******************************************************************\ |
| 15 | +
|
| 16 | +Function: |
| 17 | +
|
| 18 | + Inputs: |
| 19 | +
|
| 20 | + Outputs: |
| 21 | +
|
| 22 | + Purpose: |
| 23 | +
|
| 24 | +\*******************************************************************/ |
| 25 | + |
| 26 | +std::string strip_string(const std::string &s) |
| 27 | +{ |
| 28 | + std::string::size_type n=s.length(); |
| 29 | + |
| 30 | + // find first non-space char |
| 31 | + unsigned i; |
| 32 | + for(i=0; i<n; i++) |
| 33 | + { |
| 34 | + if(!std::isspace(s[i])) |
| 35 | + break; |
| 36 | + } |
| 37 | + if(i==n) |
| 38 | + return ""; |
| 39 | + |
| 40 | + // find last non-space char |
| 41 | + long j; // need signed type here |
| 42 | + for(j=n-1; j>=0; j--) |
| 43 | + { |
| 44 | + if(!std::isspace(s[j])) |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + return s.substr(i, (j-i+1)); |
| 49 | +} |
| 50 | + |
| 51 | +/*******************************************************************\ |
| 52 | +
|
| 53 | +Function: |
| 54 | +
|
| 55 | + Inputs: |
| 56 | +
|
| 57 | + Outputs: |
| 58 | +
|
| 59 | + Purpose: |
| 60 | +
|
| 61 | +\*******************************************************************/ |
| 62 | + |
| 63 | +void split_string( |
| 64 | + const std::string &s, |
| 65 | + char delim, |
| 66 | + std::vector<std::string> &result, |
| 67 | + bool strip, |
| 68 | + bool remove_empty) |
| 69 | +{ |
| 70 | + assert(result.empty()); |
| 71 | + assert(!std::isspace(delim)); |
| 72 | + |
| 73 | + if(s.empty()) |
| 74 | + { |
| 75 | + result.push_back(""); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + std::string::size_type n=s.length(); |
| 80 | + assert(n>0); |
| 81 | + |
| 82 | + unsigned start=0; |
| 83 | + unsigned i; |
| 84 | + |
| 85 | + for (i=0; i<n; i++) |
| 86 | + { |
| 87 | + if (s[i]==delim) |
| 88 | + { |
| 89 | + std::string new_s=s.substr(start, i-start); |
| 90 | + |
| 91 | + if(strip) |
| 92 | + new_s=strip_string(new_s); |
| 93 | + |
| 94 | + if(!remove_empty || !new_s.empty()) |
| 95 | + result.push_back(new_s); |
| 96 | + start=i+1; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + std::string new_s=s.substr(start, n-start); |
| 101 | + |
| 102 | + if(strip) |
| 103 | + new_s=strip_string(new_s); |
| 104 | + |
| 105 | + if(!remove_empty || !new_s.empty()) |
| 106 | + result.push_back(new_s); |
| 107 | + |
| 108 | + assert(!result.empty()); |
| 109 | +} |
| 110 | + |
| 111 | +/*******************************************************************\ |
| 112 | +
|
| 113 | +Function: |
| 114 | +
|
| 115 | + Inputs: |
| 116 | +
|
| 117 | + Outputs: |
| 118 | +
|
| 119 | + Purpose: |
| 120 | +
|
| 121 | +\*******************************************************************/ |
| 122 | + |
| 123 | +void split_string( |
| 124 | + const std::string &s, |
| 125 | + char delim, |
| 126 | + std::string &left, |
| 127 | + std::string &right, |
| 128 | + bool strip) |
| 129 | +{ |
| 130 | + assert(!std::isspace(delim)); |
| 131 | + |
| 132 | + std::vector<std::string> result; |
| 133 | + |
| 134 | + split_string(s, delim, result, strip); |
| 135 | + if(result.size()!=2) |
| 136 | + throw std::invalid_argument("number of parts != 2"); |
| 137 | + |
| 138 | + left=result[0]; |
| 139 | + right=result[1]; |
| 140 | +} |
| 141 | + |
| 142 | +/*******************************************************************\ |
| 143 | +
|
| 144 | +Function: |
| 145 | +
|
| 146 | + Inputs: |
| 147 | +
|
| 148 | + Outputs: |
| 149 | +
|
| 150 | + Purpose: |
| 151 | +
|
| 152 | +\*******************************************************************/ |
| 153 | + |
| 154 | +// unit tests |
| 155 | +#if 0 |
| 156 | +int main() |
| 157 | +{ |
| 158 | + assert(strip_string(" x y ")=="xy"); |
| 159 | + |
| 160 | + const std::string test=" a,, x , ,"; |
| 161 | + |
| 162 | + std::vector<std::string> result; |
| 163 | + split_string(test, ',', result, false, false); |
| 164 | + assert(result.size()==5); |
| 165 | + assert(result[0]==" a"); |
| 166 | + assert(result[1]==""); |
| 167 | + assert(result[2]==" x "); |
| 168 | + assert(result[3]==" "); |
| 169 | + assert(result[4]==""); |
| 170 | + |
| 171 | + result.clear(); |
| 172 | + split_string(test, ',', result, true, false); |
| 173 | + assert(result.size()==5); |
| 174 | + assert(result[0]=="a"); |
| 175 | + assert(result[1]==""); |
| 176 | + assert(result[2]=="x"); |
| 177 | + assert(result[3]==""); |
| 178 | + assert(result[4]==""); |
| 179 | + |
| 180 | + result.clear(); |
| 181 | + split_string(test, ',', result, false, true); |
| 182 | + assert(result.size()==3); |
| 183 | + assert(result[0]==" a"); |
| 184 | + assert(result[1]==" x "); |
| 185 | + assert(result[2]==" "); |
| 186 | + |
| 187 | + split_string(test, ',', result, true, true); |
| 188 | + assert(result.size()==2); |
| 189 | + assert(result[0]=="a"); |
| 190 | + assert(result[2]=="x"); |
| 191 | + |
| 192 | + std::string s1; |
| 193 | + std::string s2; |
| 194 | + split_string("a:b", ':', s1, s2, false); |
| 195 | + assert(s1=="a"); |
| 196 | + assert(s2=="b"); |
| 197 | +} |
| 198 | +#endif |
0 commit comments