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 #if PY_VERSION_HEX < 0x03000000 41 object decode() const; 42 object decode(object_cref encoding) const; 43 44 object decode(object_cref encoding, object_cref errors) const; 45 #endif 46 47 object encode() const; 48 object encode(object_cref encoding) const; 49 object encode(object_cref encoding, object_cref errors) const; 50 51 bool endswith(object_cref suffix) const; 52 53 bool endswith(object_cref suffix, object_cref start) const; 54 bool endswith(object_cref suffix, object_cref start, object_cref end) const; 55 56 str expandtabs() const; 57 str expandtabs(object_cref tabsize) const; 58 59 long find(object_cref sub) const; 60 long find(object_cref sub, object_cref start) const; 61 62 long find(object_cref sub, object_cref start, object_cref end) const; 63 64 long index(object_cref sub) const; 65 66 long index(object_cref sub, object_cref start) const; 67 long index(object_cref sub, object_cref start, object_cref end) const; 68 69 bool isalnum() const; 70 bool isalpha() const; 71 bool isdigit() const; 72 bool islower() const; 73 bool isspace() const; 74 bool istitle() const; 75 bool isupper() const; 76 77 str join(object_cref sequence) const; 78 79 str ljust(object_cref width) const; 80 str lower() const; 81 str lstrip() const; 82 83 str replace(object_cref old, object_cref new_) const; 84 str replace(object_cref old, object_cref new_, object_cref maxsplit) const; 85 long rfind(object_cref sub) const; 86 87 long rfind(object_cref sub, object_cref start) const; 88 89 long rfind(object_cref sub, object_cref start, object_cref end) const; 90 long rindex(object_cref sub) const; 91 long rindex(object_cref sub, object_cref start) const; 92 93 94 long rindex(object_cref sub, object_cref start, object_cref end) const; 95 96 str rjust(object_cref width) const; 97 98 str rstrip() const; 99 100 list split() const; 101 list split(object_cref sep) const; 102 103 list split(object_cref sep, object_cref maxsplit) const; 104 105 106 list splitlines() const; 107 list splitlines(object_cref keepends) const; 108 109 bool startswith(object_cref prefix) const; 110 111 112 bool startswith(object_cref prefix, object_cref start) const; 113 bool startswith(object_cref prefix, object_cref start, object_cref end) const; 114 115 str strip() const; 116 str swapcase() const; 117 str title() const; 118 119 str translate(object_cref table) const; 120 121 str translate(object_cref table, object_cref deletechars) const; 122 123 124 str upper() const; 125 126 protected: 127 str_base(); // new str 128 129 str_base(const char* s); // new str 130 131 str_base(char const* start, char const* finish); 132 133 str_base(char const* start, std::size_t length); 134 135 explicit str_base(object_cref other); 136 137 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str_base, object) 138 private: 139 static new_reference call(object const&); 140 }; 141 } 142 143 144 class str : public detail::str_base 145 { 146 typedef detail::str_base base; 147 public: str()148 str() {} // new str 149 str(const char * s)150 str(const char* s) : base(s) {} // new str 151 str(char const * start,char const * finish)152 str(char const* start, char const* finish) // new str 153 : base(start, finish) 154 {} 155 str(char const * start,std::size_t length)156 str(char const* start, std::size_t length) // new str 157 : base(start, length) 158 {} 159 160 template <class T> str(T const & other)161 explicit str(T const& other) 162 : base(object(other)) 163 { 164 } 165 166 template <class T> center(T const & width) const167 str center(T const& width) const 168 { 169 return base::center(object(width)); 170 } 171 172 template<class T> count(T const & sub) const173 long count(T const& sub) const 174 { 175 return base::count(object(sub)); 176 } 177 178 template<class T1, class T2> count(T1 const & sub,T2 const & start) const179 long count(T1 const& sub,T2 const& start) const 180 { 181 return base::count(object(sub), object(start)); 182 } 183 184 template<class T1, class T2, class T3> count(T1 const & sub,T2 const & start,T3 const & end) const185 long count(T1 const& sub,T2 const& start, T3 const& end) const 186 { 187 return base::count(object(sub), object(start), object(end)); 188 } 189 190 #if PY_VERSION_HEX < 0x03000000 decode() const191 object decode() const { return base::decode(); } 192 193 template<class T> decode(T const & encoding) const194 object decode(T const& encoding) const 195 { 196 return base::decode(object(encoding)); 197 } 198 199 template<class T1, class T2> decode(T1 const & encoding,T2 const & errors) const200 object decode(T1 const& encoding, T2 const& errors) const 201 { 202 return base::decode(object(encoding),object(errors)); 203 } 204 #endif 205 encode() const206 object encode() const { return base::encode(); } 207 208 template <class T> encode(T const & encoding) const209 object encode(T const& encoding) const 210 { 211 return base::encode(object(encoding)); 212 } 213 214 template <class T1, class T2> encode(T1 const & encoding,T2 const & errors) const215 object encode(T1 const& encoding, T2 const& errors) const 216 { 217 return base::encode(object(encoding),object(errors)); 218 } 219 220 template <class T> endswith(T const & suffix) const221 bool endswith(T const& suffix) const 222 { 223 return base::endswith(object(suffix)); 224 } 225 226 template <class T1, class T2> endswith(T1 const & suffix,T2 const & start) const227 bool endswith(T1 const& suffix, T2 const& start) const 228 { 229 return base::endswith(object(suffix), object(start)); 230 } 231 232 template <class T1, class T2, class T3> endswith(T1 const & suffix,T2 const & start,T3 const & end) const233 bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const 234 { 235 return base::endswith(object(suffix), object(start), object(end)); 236 } 237 expandtabs() const238 str expandtabs() const { return base::expandtabs(); } 239 240 template <class T> expandtabs(T const & tabsize) const241 str expandtabs(T const& tabsize) const 242 { 243 return base::expandtabs(object(tabsize)); 244 } 245 246 template <class T> find(T const & sub) const247 long find(T const& sub) const 248 { 249 return base::find(object(sub)); 250 } 251 252 template <class T1, class T2> find(T1 const & sub,T2 const & start) const253 long find(T1 const& sub, T2 const& start) const 254 { 255 return base::find(object(sub), object(start)); 256 } 257 258 template <class T1, class T2, class T3> find(T1 const & sub,T2 const & start,T3 const & end) const259 long find(T1 const& sub, T2 const& start, T3 const& end) const 260 { 261 return base::find(object(sub), object(start), object(end)); 262 } 263 264 template <class T> index(T const & sub) const265 long index(T const& sub) const 266 { 267 return base::index(object(sub)); 268 } 269 270 template <class T1, class T2> index(T1 const & sub,T2 const & start) const271 long index(T1 const& sub, T2 const& start) const 272 { 273 return base::index(object(sub), object(start)); 274 } 275 276 template <class T1, class T2, class T3> index(T1 const & sub,T2 const & start,T3 const & end) const277 long index(T1 const& sub, T2 const& start, T3 const& end) const 278 { 279 return base::index(object(sub), object(start), object(end)); 280 } 281 282 template <class T> join(T const & sequence) const283 str join(T const& sequence) const 284 { 285 return base::join(object(sequence)); 286 } 287 288 template <class T> ljust(T const & width) const289 str ljust(T const& width) const 290 { 291 return base::ljust(object(width)); 292 } 293 294 template <class T1, class T2> replace(T1 const & old,T2 const & new_) const295 str replace(T1 const& old, T2 const& new_) const 296 { 297 return base::replace(object(old),object(new_)); 298 } 299 300 template <class T1, class T2, class T3> replace(T1 const & old,T2 const & new_,T3 const & maxsplit) const301 str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const 302 { 303 return base::replace(object(old),object(new_), object(maxsplit)); 304 } 305 306 template <class T> rfind(T const & sub) const307 long rfind(T const& sub) const 308 { 309 return base::rfind(object(sub)); 310 } 311 312 template <class T1, class T2> rfind(T1 const & sub,T2 const & start) const313 long rfind(T1 const& sub, T2 const& start) const 314 { 315 return base::rfind(object(sub), object(start)); 316 } 317 318 template <class T1, class T2, class T3> rfind(T1 const & sub,T2 const & start,T3 const & end) const319 long rfind(T1 const& sub, T2 const& start, T3 const& end) const 320 { 321 return base::rfind(object(sub), object(start), object(end)); 322 } 323 324 template <class T> rindex(T const & sub) const325 long rindex(T const& sub) const 326 { 327 return base::rindex(object(sub)); 328 } 329 330 template <class T1, class T2> rindex(T1 const & sub,T2 const & start) const331 long rindex(T1 const& sub, T2 const& start) const 332 { 333 return base::rindex(object(sub), object(start)); 334 } 335 336 template <class T1, class T2, class T3> rindex(T1 const & sub,T2 const & start,T3 const & end) const337 long rindex(T1 const& sub, T2 const& start, T3 const& end) const 338 { 339 return base::rindex(object(sub), object(start), object(end)); 340 } 341 342 template <class T> rjust(T const & width) const343 str rjust(T const& width) const 344 { 345 return base::rjust(object(width)); 346 } 347 split() const348 list split() const { return base::split(); } 349 350 template <class T> split(T const & sep) const351 list split(T const& sep) const 352 { 353 return base::split(object(sep)); 354 } 355 356 template <class T1, class T2> split(T1 const & sep,T2 const & maxsplit) const357 list split(T1 const& sep, T2 const& maxsplit) const 358 { 359 return base::split(object(sep), object(maxsplit)); 360 } 361 splitlines() const362 list splitlines() const { return base::splitlines(); } 363 364 template <class T> splitlines(T const & keepends) const365 list splitlines(T const& keepends) const 366 { 367 return base::splitlines(object(keepends)); 368 } 369 370 template <class T> startswith(T const & prefix) const371 bool startswith(T const& prefix) const 372 { 373 return base::startswith(object(prefix)); 374 } 375 376 template <class T1, class T2> startswith(T1 const & prefix,T2 const & start) const377 bool startswith(T1 const& prefix, T2 const& start) const 378 { 379 return base::startswith(object(prefix), object(start)); 380 } 381 382 template <class T1, class T2, class T3> startswith(T1 const & prefix,T2 const & start,T3 const & end) const383 bool startswith(T1 const& prefix, T2 const& start, T3 const& end) const 384 { 385 return base::startswith(object(prefix), object(start), object(end)); 386 } 387 388 template <class T> translate(T const & table) const389 str translate(T const& table) const 390 { 391 return base::translate(object(table)); 392 } 393 394 template <class T1, class T2> translate(T1 const & table,T2 const & deletechars) const395 str translate(T1 const& table, T2 const& deletechars) const 396 { 397 return base::translate(object(table), object(deletechars)); 398 } 399 400 public: // implementation detail -- for internal use only 401 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, base) 402 }; 403 404 // 405 // Converter Specializations 406 // 407 namespace converter 408 { 409 template <> 410 struct object_manager_traits<str> 411 #if PY_VERSION_HEX >= 0x03000000 412 : pytype_object_manager_traits<&PyUnicode_Type,str> 413 #else 414 : pytype_object_manager_traits<&PyString_Type,str> 415 #endif 416 { 417 }; 418 } 419 420 }} // namespace boost::python 421 422 #endif // STR_20020703_HPP 423