| 1 | // Copyright David Abrahams 2002. |
|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 5 | #ifndef STR_20020703_HPP |
|---|
| 6 | #define STR_20020703_HPP |
|---|
| 7 | |
|---|
| 8 | # include <boost/python/detail/prefix.hpp> |
|---|
| 9 | |
|---|
| 10 | #include <boost/python/object.hpp> |
|---|
| 11 | #include <boost/python/list.hpp> |
|---|
| 12 | #include <boost/python/converter/pytype_object_mgr_traits.hpp> |
|---|
| 13 | |
|---|
| 14 | // disable defines in <cctype> provided by some system libraries |
|---|
| 15 | #undef isspace |
|---|
| 16 | #undef islower |
|---|
| 17 | #undef isalpha |
|---|
| 18 | #undef isdigit |
|---|
| 19 | #undef isalnum |
|---|
| 20 | #undef isupper |
|---|
| 21 | |
|---|
| 22 | namespace boost { namespace python { |
|---|
| 23 | |
|---|
| 24 | class str; |
|---|
| 25 | |
|---|
| 26 | namespace detail |
|---|
| 27 | { |
|---|
| 28 | struct BOOST_PYTHON_DECL str_base : object |
|---|
| 29 | { |
|---|
| 30 | str capitalize() const; |
|---|
| 31 | |
|---|
| 32 | str center(object_cref width) const; |
|---|
| 33 | |
|---|
| 34 | long count(object_cref sub) const; |
|---|
| 35 | |
|---|
| 36 | long count(object_cref sub, object_cref start) const; |
|---|
| 37 | |
|---|
| 38 | long count(object_cref sub, object_cref start, object_cref end) const; |
|---|
| 39 | |
|---|
| 40 | object decode() const; |
|---|
| 41 | object decode(object_cref encoding) const; |
|---|
| 42 | |
|---|
| 43 | object decode(object_cref encoding, object_cref errors) const; |
|---|
| 44 | |
|---|
| 45 | object encode() const; |
|---|
| 46 | object encode(object_cref encoding) const; |
|---|
| 47 | object encode(object_cref encoding, object_cref errors) const; |
|---|
| 48 | |
|---|
| 49 | bool endswith(object_cref suffix) const; |
|---|
| 50 | |
|---|
| 51 | bool endswith(object_cref suffix, object_cref start) const; |
|---|
| 52 | bool endswith(object_cref suffix, object_cref start, object_cref end) const; |
|---|
| 53 | |
|---|
| 54 | str expandtabs() const; |
|---|
| 55 | str expandtabs(object_cref tabsize) const; |
|---|
| 56 | |
|---|
| 57 | long find(object_cref sub) const; |
|---|
| 58 | long find(object_cref sub, object_cref start) const; |
|---|
| 59 | |
|---|
| 60 | long find(object_cref sub, object_cref start, object_cref end) const; |
|---|
| 61 | |
|---|
| 62 | long index(object_cref sub) const; |
|---|
| 63 | |
|---|
| 64 | long index(object_cref sub, object_cref start) const; |
|---|
| 65 | long index(object_cref sub, object_cref start, object_cref end) const; |
|---|
| 66 | |
|---|
| 67 | bool isalnum() const; |
|---|
| 68 | bool isalpha() const; |
|---|
| 69 | bool isdigit() const; |
|---|
| 70 | bool islower() const; |
|---|
| 71 | bool isspace() const; |
|---|
| 72 | bool istitle() const; |
|---|
| 73 | bool isupper() const; |
|---|
| 74 | |
|---|
| 75 | str join(object_cref sequence) const; |
|---|
| 76 | |
|---|
| 77 | str ljust(object_cref width) const; |
|---|
| 78 | str lower() const; |
|---|
| 79 | str lstrip() const; |
|---|
| 80 | |
|---|
| 81 | str replace(object_cref old, object_cref new_) const; |
|---|
| 82 | str replace(object_cref old, object_cref new_, object_cref maxsplit) const; |
|---|
| 83 | long rfind(object_cref sub) const; |
|---|
| 84 | |
|---|
| 85 | long rfind(object_cref sub, object_cref start) const; |
|---|
| 86 | |
|---|
| 87 | long rfind(object_cref sub, object_cref start, object_cref end) const; |
|---|
| 88 | long rindex(object_cref sub) const; |
|---|
| 89 | long rindex(object_cref sub, object_cref start) const; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | long rindex(object_cref sub, object_cref start, object_cref end) const; |
|---|
| 93 | |
|---|
| 94 | str rjust(object_cref width) const; |
|---|
| 95 | |
|---|
| 96 | str rstrip() const; |
|---|
| 97 | |
|---|
| 98 | list split() const; |
|---|
| 99 | list split(object_cref sep) const; |
|---|
| 100 | |
|---|
| 101 | list split(object_cref sep, object_cref maxsplit) const; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | list splitlines() const; |
|---|
| 105 | list splitlines(object_cref keepends) const; |
|---|
| 106 | |
|---|
| 107 | bool startswith(object_cref prefix) const; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | bool startswith(object_cref prefix, object_cref start) const; |
|---|
| 111 | bool startswith(object_cref prefix, object_cref start, object_cref end) const; |
|---|
| 112 | |
|---|
| 113 | str strip() const; |
|---|
| 114 | str swapcase() const; |
|---|
| 115 | str title() const; |
|---|
| 116 | |
|---|
| 117 | str translate(object_cref table) const; |
|---|
| 118 | |
|---|
| 119 | str translate(object_cref table, object_cref deletechars) const; |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | str upper() const; |
|---|
| 123 | |
|---|
| 124 | protected: |
|---|
| 125 | str_base(); // new str |
|---|
| 126 | |
|---|
| 127 | str_base(const char* s); // new str |
|---|
| 128 | |
|---|
| 129 | str_base(char const* start, char const* finish); |
|---|
| 130 | |
|---|
| 131 | str_base(char const* start, std::size_t length); |
|---|
| 132 | |
|---|
| 133 | explicit str_base(object_cref other); |
|---|
| 134 | |
|---|
| 135 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str_base, object) |
|---|
| 136 | private: |
|---|
| 137 | static new_reference call(object const&); |
|---|
| 138 | }; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | class str : public detail::str_base |
|---|
| 143 | { |
|---|
| 144 | typedef detail::str_base base; |
|---|
| 145 | public: |
|---|
| 146 | str() {} // new str |
|---|
| 147 | |
|---|
| 148 | str(const char* s) : base(s) {} // new str |
|---|
| 149 | |
|---|
| 150 | str(char const* start, char const* finish) // new str |
|---|
| 151 | : base(start, finish) |
|---|
| 152 | {} |
|---|
| 153 | |
|---|
| 154 | str(char const* start, std::size_t length) // new str |
|---|
| 155 | : base(start, length) |
|---|
| 156 | {} |
|---|
| 157 | |
|---|
| 158 | template <class T> |
|---|
| 159 | explicit str(T const& other) |
|---|
| 160 | : base(object(other)) |
|---|
| 161 | { |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | template <class T> |
|---|
| 165 | str center(T const& width) const |
|---|
| 166 | { |
|---|
| 167 | return base::center(object(width)); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | template<class T> |
|---|
| 171 | long count(T const& sub) const |
|---|
| 172 | { |
|---|
| 173 | return base::count(object(sub)); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | template<class T1, class T2> |
|---|
| 177 | long count(T1 const& sub,T2 const& start) const |
|---|
| 178 | { |
|---|
| 179 | return base::count(object(sub), object(start)); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | template<class T1, class T2, class T3> |
|---|
| 183 | long count(T1 const& sub,T2 const& start, T3 const& end) const |
|---|
| 184 | { |
|---|
| 185 | return base::count(object(sub), object(start)); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | object decode() const { return base::decode(); } |
|---|
| 189 | |
|---|
| 190 | template<class T> |
|---|
| 191 | object decode(T const& encoding) const |
|---|
| 192 | { |
|---|
| 193 | return base::decode(object(encoding)); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | template<class T1, class T2> |
|---|
| 197 | object decode(T1 const& encoding, T2 const& errors) const |
|---|
| 198 | { |
|---|
| 199 | return base::decode(object(encoding),object(errors)); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | object encode() const { return base::encode(); } |
|---|
| 203 | |
|---|
| 204 | template <class T> |
|---|
| 205 | object encode(T const& encoding) const |
|---|
| 206 | { |
|---|
| 207 | return base::encode(object(encoding)); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | template <class T1, class T2> |
|---|
| 211 | object encode(T1 const& encoding, T2 const& errors) const |
|---|
| 212 | { |
|---|
| 213 | return base::encode(object(encoding),object(errors)); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | template <class T> |
|---|
| 217 | bool endswith(T const& suffix) const |
|---|
| 218 | { |
|---|
| 219 | return base::endswith(object(suffix)); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | template <class T1, class T2> |
|---|
| 223 | bool endswith(T1 const& suffix, T2 const& start) const |
|---|
| 224 | { |
|---|
| 225 | return base::endswith(object(suffix), object(start)); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | template <class T1, class T2, class T3> |
|---|
| 229 | bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const |
|---|
| 230 | { |
|---|
| 231 | return base::endswith(object(suffix), object(start), object(end)); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | str expandtabs() const { return base::expandtabs(); } |
|---|
| 235 | |
|---|
| 236 | template <class T> |
|---|
| 237 | str expandtabs(T const& tabsize) const |
|---|
| 238 | { |
|---|
| 239 | return base::expandtabs(object(tabsize)); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | template <class T> |
|---|
| 243 | long find(T const& sub) const |
|---|
| 244 | { |
|---|
| 245 | return base::find(object(sub)); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | template <class T1, class T2> |
|---|
| 249 | long find(T1 const& sub, T2 const& start) const |
|---|
| 250 | { |
|---|
| 251 | return base::find(object(sub), object(start)); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | template <class T1, class T2, class T3> |
|---|
| 255 | long find(T1 const& sub, T2 const& start, T3 const& end) const |
|---|
| 256 | { |
|---|
| 257 | return base::find(object(sub), object(start), object(end)); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | template <class T> |
|---|
| 261 | long index(T const& sub) const |
|---|
| 262 | { |
|---|
| 263 | return base::index(object(sub)); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | template <class T1, class T2> |
|---|
| 267 | long index(T1 const& sub, T2 const& start) const |
|---|
| 268 | { |
|---|
| 269 | return base::index(object(sub), object(start)); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | template <class T1, class T2, class T3> |
|---|
| 273 | long index(T1 const& sub, T2 const& start, T3 const& end) const |
|---|
| 274 | { |
|---|
| 275 | return base::index(object(sub), object(start), object(end)); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | template <class T> |
|---|
| 279 | str join(T const& sequence) const |
|---|
| 280 | { |
|---|
| 281 | return base::join(object(sequence)); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | template <class T> |
|---|
| 285 | str ljust(T const& width) const |
|---|
| 286 | { |
|---|
| 287 | return base::ljust(object(width)); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | template <class T1, class T2> |
|---|
| 291 | str replace(T1 const& old, T2 const& new_) const |
|---|
| 292 | { |
|---|
| 293 | return base::replace(object(old),object(new_)); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | template <class T1, class T2, class T3> |
|---|
| 297 | str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const |
|---|
| 298 | { |
|---|
| 299 | return base::replace(object(old),object(new_), object(maxsplit)); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | template <class T> |
|---|
| 303 | long rfind(T const& sub) const |
|---|
| 304 | { |
|---|
| 305 | return base::rfind(object(sub)); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | template <class T1, class T2> |
|---|
| 309 | long rfind(T1 const& sub, T2 const& start) const |
|---|
| 310 | { |
|---|
| 311 | return base::rfind(object(sub), object(start)); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | template <class T1, class T2, class T3> |
|---|
| 315 | long rfind(T1 const& sub, T2 const& start, T3 const& end) const |
|---|
| 316 | { |
|---|
| 317 | return base::rfind(object(sub), object(start), object(end)); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | template <class T> |
|---|
| 321 | long rindex(T const& sub) const |
|---|
| 322 | { |
|---|
| 323 | return base::rindex(object(sub)); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | template <class T1, class T2> |
|---|
| 327 | long rindex(T1 const& sub, T2 const& start) const |
|---|
| 328 | { |
|---|
| 329 | return base::rindex(object(sub), object(start)); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | template <class T1, class T2, class T3> |
|---|
| 333 | long rindex(T1 const& sub, T2 const& start, T3 const& end) const |
|---|
| 334 | { |
|---|
| 335 | return base::rindex(object(sub), object(start), object(end)); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | template <class T> |
|---|
| 339 | str rjust(T const& width) const |
|---|
| 340 | { |
|---|
| 341 | return base::rjust(object(width)); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | list split() const { return base::split(); } |
|---|
| 345 | |
|---|
| 346 | template <class T> |
|---|
| 347 | list split(T const& sep) const |
|---|
| 348 | { |
|---|
| 349 | return base::split(object(sep)); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | template <class T1, class T2> |
|---|
| 353 | list split(T1 const& sep, T2 const& maxsplit) const |
|---|
| 354 | { |
|---|
| 355 | return base::split(object(sep), object(maxsplit)); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | list splitlines() const { return base::splitlines(); } |
|---|
| 359 | |
|---|
| 360 | template <class T> |
|---|
| 361 | list splitlines(T const& keepends) const |
|---|
| 362 | { |
|---|
| 363 | return base::splitlines(object(keepends)); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | template <class T> |
|---|
| 367 | bool startswith(T const& prefix) const |
|---|
| 368 | { |
|---|
| 369 | return base::startswith(object(prefix)); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | template <class T1, class T2> |
|---|
| 373 | bool startswith(T1 const& prefix, T2 const& start) const |
|---|
| 374 | { |
|---|
| 375 | return base::startswith(object(prefix), object(start)); |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | template <class T1, class T2, class T3> |
|---|
| 379 | bool startswith(T1 const& prefix, T2 const& start, T3 const& end) const |
|---|
| 380 | { |
|---|
| 381 | return base::startswith(object(prefix), object(start), object(end)); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | template <class T> |
|---|
| 385 | str translate(T const& table) const |
|---|
| 386 | { |
|---|
| 387 | return base::translate(object(table)); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | template <class T1, class T2> |
|---|
| 391 | str translate(T1 const& table, T2 const& deletechars) const |
|---|
| 392 | { |
|---|
| 393 | return base::translate(object(table), object(deletechars)); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | public: // implementation detail -- for internal use only |
|---|
| 397 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, base) |
|---|
| 398 | }; |
|---|
| 399 | |
|---|
| 400 | // |
|---|
| 401 | // Converter Specializations |
|---|
| 402 | // |
|---|
| 403 | namespace converter |
|---|
| 404 | { |
|---|
| 405 | template <> |
|---|
| 406 | struct object_manager_traits<str> |
|---|
| 407 | : pytype_object_manager_traits<&PyString_Type,str> |
|---|
| 408 | { |
|---|
| 409 | }; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | }} // namespace boost::python |
|---|
| 413 | |
|---|
| 414 | #endif // STR_20020703_HPP |
|---|