Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/src/str.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 18 years ago

added boost

File size: 8.8 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
7namespace boost { namespace python { namespace detail {
8
9detail::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
16str_base::str_base()
17  : object(detail::new_reference(::PyString_FromString("")))
18{}
19
20str_base::str_base(const char* s)
21  : object(detail::new_reference(::PyString_FromString(s)))
22{}
23
24str_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
32str_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
40str_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)                             \
48str 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
58BOOST_PYTHON_DEFINE_STR_METHOD(capitalize, 0)
59BOOST_PYTHON_DEFINE_STR_METHOD(center, 1)
60
61long str_base::count(object_cref sub) const
62{
63    return extract<long>(this->attr("count")(sub));
64}
65
66long str_base::count(object_cref sub, object_cref start) const
67{
68    return extract<long>(this->attr("count")(sub,start));
69}
70
71long 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
76object str_base::decode() const
77{
78    return this->attr("decode")();
79}
80
81object str_base::decode(object_cref encoding) const
82{
83    return this->attr("decode")(encoding);
84}
85
86object str_base::decode(object_cref encoding, object_cref errors) const
87{
88    return this->attr("decode")(encoding,errors);
89}
90
91object str_base::encode() const
92{
93    return this->attr("encode")();
94}
95
96object str_base::encode(object_cref encoding) const
97{
98    return this->attr("encode")(encoding);
99}
100
101object str_base::encode(object_cref encoding, object_cref errors) const
102{
103    return this->attr("encode")(encoding,errors);
104}
105
106bool 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
114BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 0)
115BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 1)
116
117long 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
125long 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
133long 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
141long 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
149long 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
157long 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
165bool 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
173bool 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
181bool 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
189bool 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
197bool 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
205bool 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
213bool 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
221BOOST_PYTHON_DEFINE_STR_METHOD(join, 1)
222BOOST_PYTHON_DEFINE_STR_METHOD(ljust, 1)
223BOOST_PYTHON_DEFINE_STR_METHOD(lower, 0)
224BOOST_PYTHON_DEFINE_STR_METHOD(lstrip, 0)
225BOOST_PYTHON_DEFINE_STR_METHOD(replace, 2)
226BOOST_PYTHON_DEFINE_STR_METHOD(replace, 3)
227
228long 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
236long 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
244long 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
252long 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
260long 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
268long 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
276BOOST_PYTHON_DEFINE_STR_METHOD(rjust, 1)
277BOOST_PYTHON_DEFINE_STR_METHOD(rstrip, 0)
278
279list str_base::split() const
280{
281    return list(this->attr("split")());
282}
283
284list str_base::split(object_cref sep) const
285{
286    return list(this->attr("split")(sep));
287}
288
289list str_base::split(object_cref sep, object_cref maxsplit) const
290{
291    return list(this->attr("split")(sep,maxsplit));
292}
293
294list str_base::splitlines() const
295{
296    return list(this->attr("splitlines")());
297}
298
299list str_base::splitlines(object_cref keepends) const
300{
301    return list(this->attr("splitlines")(keepends));
302}
303
304bool 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
312bool 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
320bool 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
328BOOST_PYTHON_DEFINE_STR_METHOD(strip, 0)
329BOOST_PYTHON_DEFINE_STR_METHOD(swapcase, 0)
330BOOST_PYTHON_DEFINE_STR_METHOD(title, 0)
331BOOST_PYTHON_DEFINE_STR_METHOD(translate, 1)
332BOOST_PYTHON_DEFINE_STR_METHOD(translate, 2)
333BOOST_PYTHON_DEFINE_STR_METHOD(upper, 0)
334   
335}}}  // namespace boost::python
Note: See TracBrowser for help on using the repository browser.