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