[29] | 1 | // Copyright David Abrahams 2004. Distributed under the Boost |
---|
| 2 | // Software License, Version 1.0. (See accompanying |
---|
| 3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
| 4 | #include <boost/python/str.hpp> |
---|
| 5 | #include <boost/python/extract.hpp> |
---|
| 6 | #include <boost/python/ssize_t.hpp> |
---|
| 7 | |
---|
| 8 | namespace boost { namespace python { namespace detail { |
---|
| 9 | |
---|
| 10 | detail::new_reference str_base::call(object const& arg_) |
---|
| 11 | { |
---|
| 12 | return (detail::new_reference)PyObject_CallFunction( |
---|
| 13 | (PyObject*)&PyString_Type, "(O)", |
---|
| 14 | arg_.ptr()); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | str_base::str_base() |
---|
| 18 | : object(detail::new_reference(::PyString_FromString(""))) |
---|
| 19 | {} |
---|
| 20 | |
---|
| 21 | str_base::str_base(const char* s) |
---|
| 22 | : object(detail::new_reference(::PyString_FromString(s))) |
---|
| 23 | {} |
---|
| 24 | |
---|
| 25 | namespace { |
---|
| 26 | |
---|
| 27 | ssize_t str_size_as_py_ssize_t(std::size_t n) |
---|
| 28 | { |
---|
| 29 | if (n > ssize_t_max) |
---|
| 30 | { |
---|
| 31 | throw std::range_error("str size > ssize_t_max"); |
---|
| 32 | } |
---|
| 33 | return static_cast<ssize_t>(n); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | } // namespace <anonymous> |
---|
| 37 | |
---|
| 38 | str_base::str_base(char const* start, char const* finish) |
---|
| 39 | : object( |
---|
| 40 | detail::new_reference( |
---|
| 41 | ::PyString_FromStringAndSize( |
---|
| 42 | start, str_size_as_py_ssize_t(finish - start) |
---|
| 43 | ) |
---|
| 44 | ) |
---|
| 45 | ) |
---|
| 46 | {} |
---|
| 47 | |
---|
| 48 | str_base::str_base(char const* start, std::size_t length) // new str |
---|
| 49 | : object( |
---|
| 50 | detail::new_reference( |
---|
| 51 | ::PyString_FromStringAndSize( |
---|
| 52 | start, str_size_as_py_ssize_t(length) |
---|
| 53 | ) |
---|
| 54 | ) |
---|
| 55 | ) |
---|
| 56 | {} |
---|
| 57 | |
---|
| 58 | str_base::str_base(object_cref other) |
---|
| 59 | : object(str_base::call(other)) |
---|
| 60 | {} |
---|
| 61 | |
---|
| 62 | #define BOOST_PYTHON_FORMAT_OBJECT(z, n, data) "O" |
---|
| 63 | #define BOOST_PYTHON_OBJECT_PTR(z, n, data) , x##n .ptr() |
---|
| 64 | |
---|
| 65 | #define BOOST_PYTHON_DEFINE_STR_METHOD(name, arity) \ |
---|
| 66 | str str_base:: name ( BOOST_PP_ENUM_PARAMS(arity, object_cref x) ) const \ |
---|
| 67 | { \ |
---|
| 68 | return str(new_reference( \ |
---|
| 69 | expect_non_null( \ |
---|
| 70 | PyObject_CallMethod( \ |
---|
| 71 | this->ptr(), #name, \ |
---|
| 72 | "(" BOOST_PP_REPEAT(arity, BOOST_PYTHON_FORMAT_OBJECT, _) ")" \ |
---|
| 73 | BOOST_PP_REPEAT_1(arity, BOOST_PYTHON_OBJECT_PTR, _))))); \ |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | BOOST_PYTHON_DEFINE_STR_METHOD(capitalize, 0) |
---|
| 77 | BOOST_PYTHON_DEFINE_STR_METHOD(center, 1) |
---|
| 78 | |
---|
| 79 | long str_base::count(object_cref sub) const |
---|
| 80 | { |
---|
| 81 | return extract<long>(this->attr("count")(sub)); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | long str_base::count(object_cref sub, object_cref start) const |
---|
| 85 | { |
---|
| 86 | return extract<long>(this->attr("count")(sub,start)); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | long str_base::count(object_cref sub, object_cref start, object_cref end) const |
---|
| 90 | { |
---|
| 91 | return extract<long>(this->attr("count")(sub,start,end)); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | object str_base::decode() const |
---|
| 95 | { |
---|
| 96 | return this->attr("decode")(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | object str_base::decode(object_cref encoding) const |
---|
| 100 | { |
---|
| 101 | return this->attr("decode")(encoding); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | object str_base::decode(object_cref encoding, object_cref errors) const |
---|
| 105 | { |
---|
| 106 | return this->attr("decode")(encoding,errors); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | object str_base::encode() const |
---|
| 110 | { |
---|
| 111 | return this->attr("encode")(); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | object str_base::encode(object_cref encoding) const |
---|
| 115 | { |
---|
| 116 | return this->attr("encode")(encoding); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | object str_base::encode(object_cref encoding, object_cref errors) const |
---|
| 120 | { |
---|
| 121 | return this->attr("encode")(encoding,errors); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | bool str_base::endswith(object_cref suffix) const |
---|
| 125 | { |
---|
| 126 | bool result = PyInt_AsLong(this->attr("endswith")(suffix).ptr()); |
---|
| 127 | if (PyErr_Occurred()) |
---|
| 128 | throw_error_already_set(); |
---|
| 129 | return result; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 0) |
---|
| 133 | BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 1) |
---|
| 134 | |
---|
| 135 | long str_base::find(object_cref sub) const |
---|
| 136 | { |
---|
| 137 | long result = PyInt_AsLong(this->attr("find")(sub).ptr()); |
---|
| 138 | if (PyErr_Occurred()) |
---|
| 139 | throw_error_already_set(); |
---|
| 140 | return result; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | long str_base::find(object_cref sub, object_cref start) const |
---|
| 144 | { |
---|
| 145 | long result = PyInt_AsLong(this->attr("find")(sub,start).ptr()); |
---|
| 146 | if (PyErr_Occurred()) |
---|
| 147 | throw_error_already_set(); |
---|
| 148 | return result; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | long str_base::find(object_cref sub, object_cref start, object_cref end) const |
---|
| 152 | { |
---|
| 153 | long result = PyInt_AsLong(this->attr("find")(sub,start,end).ptr()); |
---|
| 154 | if (PyErr_Occurred()) |
---|
| 155 | throw_error_already_set(); |
---|
| 156 | return result; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | long str_base::index(object_cref sub) const |
---|
| 160 | { |
---|
| 161 | long result = PyInt_AsLong(this->attr("index")(sub).ptr()); |
---|
| 162 | if (PyErr_Occurred()) |
---|
| 163 | throw_error_already_set(); |
---|
| 164 | return result; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | long str_base::index(object_cref sub, object_cref start) const |
---|
| 168 | { |
---|
| 169 | long result = PyInt_AsLong(this->attr("index")(sub,start).ptr()); |
---|
| 170 | if (PyErr_Occurred()) |
---|
| 171 | throw_error_already_set(); |
---|
| 172 | return result; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | long str_base::index(object_cref sub, object_cref start, object_cref end) const |
---|
| 176 | { |
---|
| 177 | long result = PyInt_AsLong(this->attr("index")(sub,start,end).ptr()); |
---|
| 178 | if (PyErr_Occurred()) |
---|
| 179 | throw_error_already_set(); |
---|
| 180 | return result; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | bool str_base::isalnum() const |
---|
| 184 | { |
---|
| 185 | bool result = PyInt_AsLong(this->attr("isalnum")().ptr()); |
---|
| 186 | if (PyErr_Occurred()) |
---|
| 187 | throw_error_already_set(); |
---|
| 188 | return result; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | bool str_base::isalpha() const |
---|
| 192 | { |
---|
| 193 | bool result = PyInt_AsLong(this->attr("isalpha")().ptr()); |
---|
| 194 | if (PyErr_Occurred()) |
---|
| 195 | throw_error_already_set(); |
---|
| 196 | return result; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | bool str_base::isdigit() const |
---|
| 200 | { |
---|
| 201 | bool result = PyInt_AsLong(this->attr("isdigit")().ptr()); |
---|
| 202 | if (PyErr_Occurred()) |
---|
| 203 | throw_error_already_set(); |
---|
| 204 | return result; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | bool str_base::islower() const |
---|
| 208 | { |
---|
| 209 | bool result = PyInt_AsLong(this->attr("islower")().ptr()); |
---|
| 210 | if (PyErr_Occurred()) |
---|
| 211 | throw_error_already_set(); |
---|
| 212 | return result; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | bool str_base::isspace() const |
---|
| 216 | { |
---|
| 217 | bool result = PyInt_AsLong(this->attr("isspace")().ptr()); |
---|
| 218 | if (PyErr_Occurred()) |
---|
| 219 | throw_error_already_set(); |
---|
| 220 | return result; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | bool str_base::istitle() const |
---|
| 224 | { |
---|
| 225 | bool result = PyInt_AsLong(this->attr("istitle")().ptr()); |
---|
| 226 | if (PyErr_Occurred()) |
---|
| 227 | throw_error_already_set(); |
---|
| 228 | return result; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | bool str_base::isupper() const |
---|
| 232 | { |
---|
| 233 | bool result = PyInt_AsLong(this->attr("isupper")().ptr()); |
---|
| 234 | if (PyErr_Occurred()) |
---|
| 235 | throw_error_already_set(); |
---|
| 236 | return result; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | BOOST_PYTHON_DEFINE_STR_METHOD(join, 1) |
---|
| 240 | BOOST_PYTHON_DEFINE_STR_METHOD(ljust, 1) |
---|
| 241 | BOOST_PYTHON_DEFINE_STR_METHOD(lower, 0) |
---|
| 242 | BOOST_PYTHON_DEFINE_STR_METHOD(lstrip, 0) |
---|
| 243 | BOOST_PYTHON_DEFINE_STR_METHOD(replace, 2) |
---|
| 244 | BOOST_PYTHON_DEFINE_STR_METHOD(replace, 3) |
---|
| 245 | |
---|
| 246 | long str_base::rfind(object_cref sub) const |
---|
| 247 | { |
---|
| 248 | long result = PyInt_AsLong(this->attr("rfind")(sub).ptr()); |
---|
| 249 | if (PyErr_Occurred()) |
---|
| 250 | throw_error_already_set(); |
---|
| 251 | return result; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | long str_base::rfind(object_cref sub, object_cref start) const |
---|
| 255 | { |
---|
| 256 | long result = PyInt_AsLong(this->attr("rfind")(sub,start).ptr()); |
---|
| 257 | if (PyErr_Occurred()) |
---|
| 258 | throw_error_already_set(); |
---|
| 259 | return result; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | long str_base::rfind(object_cref sub, object_cref start, object_cref end) const |
---|
| 263 | { |
---|
| 264 | long result = PyInt_AsLong(this->attr("rfind")(sub,start,end).ptr()); |
---|
| 265 | if (PyErr_Occurred()) |
---|
| 266 | throw_error_already_set(); |
---|
| 267 | return result; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | long str_base::rindex(object_cref sub) const |
---|
| 271 | { |
---|
| 272 | long result = PyInt_AsLong(this->attr("rindex")(sub).ptr()); |
---|
| 273 | if (PyErr_Occurred()) |
---|
| 274 | throw_error_already_set(); |
---|
| 275 | return result; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | long str_base::rindex(object_cref sub, object_cref start) const |
---|
| 279 | { |
---|
| 280 | long result = PyInt_AsLong(this->attr("rindex")(sub,start).ptr()); |
---|
| 281 | if (PyErr_Occurred()) |
---|
| 282 | throw_error_already_set(); |
---|
| 283 | return result; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | long str_base::rindex(object_cref sub, object_cref start, object_cref end) const |
---|
| 287 | { |
---|
| 288 | long result = PyInt_AsLong(this->attr("rindex")(sub,start,end).ptr()); |
---|
| 289 | if (PyErr_Occurred()) |
---|
| 290 | throw_error_already_set(); |
---|
| 291 | return result; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | BOOST_PYTHON_DEFINE_STR_METHOD(rjust, 1) |
---|
| 295 | BOOST_PYTHON_DEFINE_STR_METHOD(rstrip, 0) |
---|
| 296 | |
---|
| 297 | list str_base::split() const |
---|
| 298 | { |
---|
| 299 | return list(this->attr("split")()); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | list str_base::split(object_cref sep) const |
---|
| 303 | { |
---|
| 304 | return list(this->attr("split")(sep)); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | list str_base::split(object_cref sep, object_cref maxsplit) const |
---|
| 308 | { |
---|
| 309 | return list(this->attr("split")(sep,maxsplit)); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | list str_base::splitlines() const |
---|
| 313 | { |
---|
| 314 | return list(this->attr("splitlines")()); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | list str_base::splitlines(object_cref keepends) const |
---|
| 318 | { |
---|
| 319 | return list(this->attr("splitlines")(keepends)); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | bool str_base::startswith(object_cref prefix) const |
---|
| 323 | { |
---|
| 324 | bool result = PyInt_AsLong(this->attr("startswith")(prefix).ptr()); |
---|
| 325 | if (PyErr_Occurred()) |
---|
| 326 | throw_error_already_set(); |
---|
| 327 | return result; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | bool str_base::startswith(object_cref prefix, object_cref start) const |
---|
| 331 | { |
---|
| 332 | bool result = PyInt_AsLong(this->attr("startswith")(prefix,start).ptr()); |
---|
| 333 | if (PyErr_Occurred()) |
---|
| 334 | throw_error_already_set(); |
---|
| 335 | return result; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | bool str_base::startswith(object_cref prefix, object_cref start, object_cref end) const |
---|
| 339 | { |
---|
| 340 | bool result = PyInt_AsLong(this->attr("startswith")(prefix,start,end).ptr()); |
---|
| 341 | if (PyErr_Occurred()) |
---|
| 342 | throw_error_already_set(); |
---|
| 343 | return result; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | BOOST_PYTHON_DEFINE_STR_METHOD(strip, 0) |
---|
| 347 | BOOST_PYTHON_DEFINE_STR_METHOD(swapcase, 0) |
---|
| 348 | BOOST_PYTHON_DEFINE_STR_METHOD(title, 0) |
---|
| 349 | BOOST_PYTHON_DEFINE_STR_METHOD(translate, 1) |
---|
| 350 | BOOST_PYTHON_DEFINE_STR_METHOD(translate, 2) |
---|
| 351 | BOOST_PYTHON_DEFINE_STR_METHOD(upper, 0) |
---|
| 352 | |
---|
| 353 | }}} // namespace boost::python |
---|