|
31 | 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 | /////////////////////////////////////////////////////////////////////////////// |
33 | 33 |
|
34 | | -#include <pystring.h> |
35 | 34 | #include <ctype.h> |
36 | 35 | #include <string.h> |
37 | 36 | #include <iostream> |
| 37 | +#include <algorithm> |
| 38 | +#include "pystring.h" |
38 | 39 |
|
39 | 40 | namespace pystring |
40 | 41 | { |
41 | | - |
42 | | - ////////////////////////////////////////////////////////////////////////////////////////////// |
43 | | - /// |
44 | | - /// |
45 | | - void split_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit ) |
46 | | - { |
47 | | - std::string::size_type i, j, len = str.size(); |
48 | | - for (i = j = 0; i < len; ) |
49 | | - { |
50 | | - |
51 | | - while ( i < len && ::isspace( str[i] ) ) i++; |
52 | | - j = i; |
53 | | - |
54 | | - while ( i < len && ! ::isspace( str[i]) ) i++; |
55 | | - |
56 | | - |
57 | | - |
58 | | - if (j < i) |
59 | | - { |
60 | | - if ( maxsplit-- <= 0 ) break; |
61 | | - |
62 | | - result.push_back( str.substr( j, i - j )); |
63 | | - |
64 | | - while ( i < len && ::isspace( str[i])) i++; |
65 | | - j = i; |
66 | | - } |
67 | | - } |
68 | | - if (j < len) |
69 | | - { |
70 | | - |
71 | | - result.push_back( str.substr( j, len - j )); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - |
| 42 | + |
| 43 | + namespace { |
| 44 | + |
| 45 | + ////////////////////////////////////////////////////////////////////////////////////////////// |
| 46 | + /// why doesn't the std::reverse work? |
| 47 | + /// |
| 48 | + void reverse_strings( std::vector< std::string > & result) |
| 49 | + { |
| 50 | + for (std::vector< std::string >::size_type i = 0; i < result.size() / 2; i++ ) |
| 51 | + { |
| 52 | + std::swap(result[i], result[result.size() - 1 - i]); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + ////////////////////////////////////////////////////////////////////////////////////////////// |
| 57 | + /// |
| 58 | + /// |
| 59 | + void split_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit ) |
| 60 | + { |
| 61 | + std::string::size_type i, j, len = str.size(); |
| 62 | + for (i = j = 0; i < len; ) |
| 63 | + { |
| 64 | + |
| 65 | + while ( i < len && ::isspace( str[i] ) ) i++; |
| 66 | + j = i; |
| 67 | + |
| 68 | + while ( i < len && ! ::isspace( str[i]) ) i++; |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + if (j < i) |
| 73 | + { |
| 74 | + if ( maxsplit-- <= 0 ) break; |
| 75 | + |
| 76 | + result.push_back( str.substr( j, i - j )); |
| 77 | + |
| 78 | + while ( i < len && ::isspace( str[i])) i++; |
| 79 | + j = i; |
| 80 | + } |
| 81 | + } |
| 82 | + if (j < len) |
| 83 | + { |
| 84 | + result.push_back( str.substr( j, len - j )); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + ////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 | + /// |
| 91 | + /// |
| 92 | + void rsplit_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit ) |
| 93 | + { |
| 94 | + std::string::size_type len = str.size(); |
| 95 | + std::string::size_type i, j; |
| 96 | + for (i = j = len; i > 0; ) |
| 97 | + { |
| 98 | + |
| 99 | + while ( i > 0 && ::isspace( str[i - 1] ) ) i--; |
| 100 | + j = i; |
| 101 | + |
| 102 | + while ( i > 0 && ! ::isspace( str[i - 1]) ) i--; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + if (j > i) |
| 107 | + { |
| 108 | + if ( maxsplit-- <= 0 ) break; |
| 109 | + |
| 110 | + result.push_back( str.substr( i, j - i )); |
| 111 | + |
| 112 | + while ( i > 0 && ::isspace( str[i - 1])) i--; |
| 113 | + j = i; |
| 114 | + } |
| 115 | + } |
| 116 | + if (j > 0) |
| 117 | + { |
| 118 | + result.push_back( str.substr( 0, j )); |
| 119 | + } |
| 120 | + //std::reverse( result, result.begin(), result.end() ); |
| 121 | + reverse_strings( result ); |
| 122 | + } |
| 123 | + |
| 124 | + } //anonymous namespace |
| 125 | + |
| 126 | + |
76 | 127 | ////////////////////////////////////////////////////////////////////////////////////////////// |
77 | 128 | /// |
78 | 129 | /// |
@@ -111,6 +162,51 @@ namespace pystring |
111 | 162 | result.push_back( str.substr( j, len-j ) ); |
112 | 163 | } |
113 | 164 |
|
| 165 | + ////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | + /// |
| 167 | + /// |
| 168 | + void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep, int maxsplit ) |
| 169 | + { |
| 170 | + if ( maxsplit == 0 ) |
| 171 | + { |
| 172 | + split( str, result, sep, 0 ); |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + result.clear(); |
| 177 | + |
| 178 | + if ( maxsplit < 0 ) maxsplit = MAX_32BIT_INT;//result.max_size(); |
| 179 | + |
| 180 | + |
| 181 | + if ( sep.size() == 0 ) |
| 182 | + { |
| 183 | + rsplit_whitespace( str, result, maxsplit ); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + std::string::size_type i,j, len = str.size(), n = sep.size(); |
| 188 | + |
| 189 | + i = j = len; |
| 190 | + |
| 191 | + while ( i > n ) |
| 192 | + { |
| 193 | + if ( str[i - 1] == sep[n - 1] && str.substr( i - n, n ) == sep ) |
| 194 | + { |
| 195 | + if ( maxsplit-- <= 0 ) break; |
| 196 | + |
| 197 | + result.push_back( str.substr( i, j - i ) ); |
| 198 | + i = j = i - n; |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + i--; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + result.push_back( str.substr( 0, j ) ); |
| 207 | + reverse_strings( result ); |
| 208 | + } |
| 209 | + |
114 | 210 | ////////////////////////////////////////////////////////////////////////////////////////////// |
115 | 211 | /// |
116 | 212 | /// |
|
0 commit comments