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
8 # include <boost/python/detail/prefix.hpp>
10 #include <boost/python/object.hpp>
11 #include <boost/python/list.hpp>
12 #include <boost/python/converter/pytype_object_mgr_traits.hpp>
14 // disable defines in <cctype> provided by some system libraries
22 namespace boost { namespace python {
28 struct BOOST_PYTHON_DECL str_base : object
30 str capitalize() const;
32 str center(object_cref width) const;
34 long count(object_cref sub) const;
36 long count(object_cref sub, object_cref start) const;
38 long count(object_cref sub, object_cref start, object_cref end) const;
40 #if PY_VERSION_HEX < 0x03000000
41 object decode() const;
42 object decode(object_cref encoding) const;
44 object decode(object_cref encoding, object_cref errors) const;
47 object encode() const;
48 object encode(object_cref encoding) const;
49 object encode(object_cref encoding, object_cref errors) const;
51 bool endswith(object_cref suffix) const;
53 bool endswith(object_cref suffix, object_cref start) const;
54 bool endswith(object_cref suffix, object_cref start, object_cref end) const;
56 str expandtabs() const;
57 str expandtabs(object_cref tabsize) const;
59 long find(object_cref sub) const;
60 long find(object_cref sub, object_cref start) const;
62 long find(object_cref sub, object_cref start, object_cref end) const;
64 long index(object_cref sub) const;
66 long index(object_cref sub, object_cref start) const;
67 long index(object_cref sub, object_cref start, object_cref end) const;
77 str join(object_cref sequence) const;
79 str ljust(object_cref width) const;
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;
87 long rfind(object_cref sub, object_cref start) const;
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;
94 long rindex(object_cref sub, object_cref start, object_cref end) const;
96 str rjust(object_cref width) const;
101 list split(object_cref sep) const;
103 list split(object_cref sep, object_cref maxsplit) const;
106 list splitlines() const;
107 list splitlines(object_cref keepends) const;
109 bool startswith(object_cref prefix) const;
112 bool startswith(object_cref prefix, object_cref start) const;
113 bool startswith(object_cref prefix, object_cref start, object_cref end) const;
116 str swapcase() const;
119 str translate(object_cref table) const;
121 str translate(object_cref table, object_cref deletechars) const;
127 str_base(); // new str
129 str_base(const char* s); // new str
131 str_base(char const* start, char const* finish);
133 str_base(char const* start, std::size_t length);
135 explicit str_base(object_cref other);
137 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str_base, object)
139 static new_reference call(object const&);
144 class str : public detail::str_base
146 typedef detail::str_base base;
150 str(const char* s) : base(s) {} // new str
152 str(char const* start, char const* finish) // new str
153 : base(start, finish)
156 str(char const* start, std::size_t length) // new str
157 : base(start, length)
161 explicit str(T const& other)
162 : base(object(other))
167 str center(T const& width) const
169 return base::center(object(width));
173 long count(T const& sub) const
175 return base::count(object(sub));
178 template<class T1, class T2>
179 long count(T1 const& sub,T2 const& start) const
181 return base::count(object(sub), object(start));
184 template<class T1, class T2, class T3>
185 long count(T1 const& sub,T2 const& start, T3 const& end) const
187 return base::count(object(sub), object(start), object(end));
190 #if PY_VERSION_HEX < 0x03000000
191 object decode() const { return base::decode(); }
194 object decode(T const& encoding) const
196 return base::decode(object(encoding));
199 template<class T1, class T2>
200 object decode(T1 const& encoding, T2 const& errors) const
202 return base::decode(object(encoding),object(errors));
206 object encode() const { return base::encode(); }
209 object encode(T const& encoding) const
211 return base::encode(object(encoding));
214 template <class T1, class T2>
215 object encode(T1 const& encoding, T2 const& errors) const
217 return base::encode(object(encoding),object(errors));
221 bool endswith(T const& suffix) const
223 return base::endswith(object(suffix));
226 template <class T1, class T2>
227 bool endswith(T1 const& suffix, T2 const& start) const
229 return base::endswith(object(suffix), object(start));
232 template <class T1, class T2, class T3>
233 bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const
235 return base::endswith(object(suffix), object(start), object(end));
238 str expandtabs() const { return base::expandtabs(); }
241 str expandtabs(T const& tabsize) const
243 return base::expandtabs(object(tabsize));
247 long find(T const& sub) const
249 return base::find(object(sub));
252 template <class T1, class T2>
253 long find(T1 const& sub, T2 const& start) const
255 return base::find(object(sub), object(start));
258 template <class T1, class T2, class T3>
259 long find(T1 const& sub, T2 const& start, T3 const& end) const
261 return base::find(object(sub), object(start), object(end));
265 long index(T const& sub) const
267 return base::index(object(sub));
270 template <class T1, class T2>
271 long index(T1 const& sub, T2 const& start) const
273 return base::index(object(sub), object(start));
276 template <class T1, class T2, class T3>
277 long index(T1 const& sub, T2 const& start, T3 const& end) const
279 return base::index(object(sub), object(start), object(end));
283 str join(T const& sequence) const
285 return base::join(object(sequence));
289 str ljust(T const& width) const
291 return base::ljust(object(width));
294 template <class T1, class T2>
295 str replace(T1 const& old, T2 const& new_) const
297 return base::replace(object(old),object(new_));
300 template <class T1, class T2, class T3>
301 str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const
303 return base::replace(object(old),object(new_), object(maxsplit));
307 long rfind(T const& sub) const
309 return base::rfind(object(sub));
312 template <class T1, class T2>
313 long rfind(T1 const& sub, T2 const& start) const
315 return base::rfind(object(sub), object(start));
318 template <class T1, class T2, class T3>
319 long rfind(T1 const& sub, T2 const& start, T3 const& end) const
321 return base::rfind(object(sub), object(start), object(end));
325 long rindex(T const& sub) const
327 return base::rindex(object(sub));
330 template <class T1, class T2>
331 long rindex(T1 const& sub, T2 const& start) const
333 return base::rindex(object(sub), object(start));
336 template <class T1, class T2, class T3>
337 long rindex(T1 const& sub, T2 const& start, T3 const& end) const
339 return base::rindex(object(sub), object(start), object(end));
343 str rjust(T const& width) const
345 return base::rjust(object(width));
348 list split() const { return base::split(); }
351 list split(T const& sep) const
353 return base::split(object(sep));
356 template <class T1, class T2>
357 list split(T1 const& sep, T2 const& maxsplit) const
359 return base::split(object(sep), object(maxsplit));
362 list splitlines() const { return base::splitlines(); }
365 list splitlines(T const& keepends) const
367 return base::splitlines(object(keepends));
371 bool startswith(T const& prefix) const
373 return base::startswith(object(prefix));
376 template <class T1, class T2>
377 bool startswith(T1 const& prefix, T2 const& start) const
379 return base::startswith(object(prefix), object(start));
382 template <class T1, class T2, class T3>
383 bool startswith(T1 const& prefix, T2 const& start, T3 const& end) const
385 return base::startswith(object(prefix), object(start), object(end));
389 str translate(T const& table) const
391 return base::translate(object(table));
394 template <class T1, class T2>
395 str translate(T1 const& table, T2 const& deletechars) const
397 return base::translate(object(table), object(deletechars));
400 public: // implementation detail -- for internal use only
401 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, base)
405 // Converter Specializations
410 struct object_manager_traits<str>
411 #if PY_VERSION_HEX >= 0x03000000
412 : pytype_object_manager_traits<&PyUnicode_Type,str>
414 : pytype_object_manager_traits<&PyString_Type,str>
420 }} // namespace boost::python
422 #endif // STR_20020703_HPP