Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/src/str.cpp @ 45

Last change on this file since 45 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 9.2 KB
Line 
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
8namespace boost { namespace python { namespace detail {
9
10detail::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
17str_base::str_base()
18  : object(detail::new_reference(::PyString_FromString("")))
19{}
20
21str_base::str_base(const char* s)
22  : object(detail::new_reference(::PyString_FromString(s)))
23{}
24
25namespace {
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
38str_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
48str_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
58str_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)                             \
66str 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
76BOOST_PYTHON_DEFINE_STR_METHOD(capitalize, 0)
77BOOST_PYTHON_DEFINE_STR_METHOD(center, 1)
78
79long str_base::count(object_cref sub) const
80{
81    return extract<long>(this->attr("count")(sub));
82}
83
84long str_base::count(object_cref sub, object_cref start) const
85{
86    return extract<long>(this->attr("count")(sub,start));
87}
88
89long 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
94object str_base::decode() const
95{
96    return this->attr("decode")();
97}
98
99object str_base::decode(object_cref encoding) const
100{
101    return this->attr("decode")(encoding);
102}
103
104object str_base::decode(object_cref encoding, object_cref errors) const
105{
106    return this->attr("decode")(encoding,errors);
107}
108
109object str_base::encode() const
110{
111    return this->attr("encode")();
112}
113
114object str_base::encode(object_cref encoding) const
115{
116    return this->attr("encode")(encoding);
117}
118
119object str_base::encode(object_cref encoding, object_cref errors) const
120{
121    return this->attr("encode")(encoding,errors);
122}
123
124bool 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
132BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 0)
133BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 1)
134
135long 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
143long 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
151long 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
159long 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
167long 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
175long 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
183bool 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
191bool 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
199bool 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
207bool 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
215bool 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
223bool 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
231bool 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
239BOOST_PYTHON_DEFINE_STR_METHOD(join, 1)
240BOOST_PYTHON_DEFINE_STR_METHOD(ljust, 1)
241BOOST_PYTHON_DEFINE_STR_METHOD(lower, 0)
242BOOST_PYTHON_DEFINE_STR_METHOD(lstrip, 0)
243BOOST_PYTHON_DEFINE_STR_METHOD(replace, 2)
244BOOST_PYTHON_DEFINE_STR_METHOD(replace, 3)
245
246long 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
254long 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
262long 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
270long 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
278long 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
286long 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
294BOOST_PYTHON_DEFINE_STR_METHOD(rjust, 1)
295BOOST_PYTHON_DEFINE_STR_METHOD(rstrip, 0)
296
297list str_base::split() const
298{
299    return list(this->attr("split")());
300}
301
302list str_base::split(object_cref sep) const
303{
304    return list(this->attr("split")(sep));
305}
306
307list str_base::split(object_cref sep, object_cref maxsplit) const
308{
309    return list(this->attr("split")(sep,maxsplit));
310}
311
312list str_base::splitlines() const
313{
314    return list(this->attr("splitlines")());
315}
316
317list str_base::splitlines(object_cref keepends) const
318{
319    return list(this->attr("splitlines")(keepends));
320}
321
322bool 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
330bool 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
338bool 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
346BOOST_PYTHON_DEFINE_STR_METHOD(strip, 0)
347BOOST_PYTHON_DEFINE_STR_METHOD(swapcase, 0)
348BOOST_PYTHON_DEFINE_STR_METHOD(title, 0)
349BOOST_PYTHON_DEFINE_STR_METHOD(translate, 1)
350BOOST_PYTHON_DEFINE_STR_METHOD(translate, 2)
351BOOST_PYTHON_DEFINE_STR_METHOD(upper, 0)
352   
353}}}  // namespace boost::python
Note: See TracBrowser for help on using the repository browser.