Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / boost / python / str.hpp
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
7
8 # include <boost/python/detail/prefix.hpp>
9
10 #include <boost/python/object.hpp>
11 #include <boost/python/list.hpp>
12 #include <boost/python/converter/pytype_object_mgr_traits.hpp>
13
14 // disable defines in <cctype> provided by some system libraries
15 #undef isspace
16 #undef islower
17 #undef isalpha
18 #undef isdigit
19 #undef isalnum
20 #undef isupper
21
22 namespace boost { namespace python {
23
24 class str;
25
26 namespace detail
27 {
28   struct BOOST_PYTHON_DECL str_base : object
29   {
30       str capitalize() const;
31
32       str center(object_cref width) const;
33
34       long count(object_cref sub) const;
35
36       long count(object_cref sub, object_cref start) const;
37     
38       long count(object_cref sub, object_cref start, object_cref end) const;
39
40 #if PY_VERSION_HEX < 0x03000000
41       object decode() const;
42       object decode(object_cref encoding) const;
43
44       object decode(object_cref encoding, object_cref errors) const;
45 #endif
46
47       object encode() const;
48       object encode(object_cref encoding) const;
49       object encode(object_cref encoding, object_cref errors) const;
50
51       bool endswith(object_cref suffix) const;
52     
53       bool endswith(object_cref suffix, object_cref start) const;
54       bool endswith(object_cref suffix, object_cref start, object_cref end) const;
55     
56       str expandtabs() const;
57       str expandtabs(object_cref tabsize) const;
58
59       long find(object_cref sub) const;
60       long find(object_cref sub, object_cref start) const;
61
62       long find(object_cref sub, object_cref start, object_cref end) const;
63
64       long index(object_cref sub) const;
65
66       long index(object_cref sub, object_cref start) const;
67       long index(object_cref sub, object_cref start, object_cref end) const;
68
69       bool isalnum() const;
70       bool isalpha() const;
71       bool isdigit() const;
72       bool islower() const;
73       bool isspace() const;
74       bool istitle() const;
75       bool isupper() const;
76     
77       str join(object_cref sequence) const;
78
79       str ljust(object_cref width) const;
80       str lower() const;
81       str lstrip() const;
82
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;
86
87       long rfind(object_cref sub, object_cref start) const;
88
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;
92
93
94       long rindex(object_cref sub, object_cref start, object_cref end) const;
95
96       str rjust(object_cref width) const;
97     
98       str rstrip() const;
99     
100       list split() const; 
101       list split(object_cref sep) const;
102    
103       list split(object_cref sep, object_cref maxsplit) const; 
104     
105
106       list splitlines() const;
107       list splitlines(object_cref keepends) const;
108
109       bool startswith(object_cref prefix) const;
110
111
112       bool startswith(object_cref prefix, object_cref start) const;
113       bool startswith(object_cref prefix, object_cref start, object_cref end) const;
114
115       str strip() const;
116       str swapcase() const;
117       str title() const;
118     
119       str translate(object_cref table) const;
120
121       str translate(object_cref table, object_cref deletechars) const;
122
123     
124       str upper() const;
125
126    protected:
127       str_base(); // new str
128     
129       str_base(const char* s); // new str
130
131       str_base(char const* start, char const* finish);
132       
133       str_base(char const* start, std::size_t length);
134       
135       explicit str_base(object_cref other);
136
137       BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str_base, object)
138    private:
139       static new_reference call(object const&);
140   };
141 }
142
143
144 class str : public detail::str_base
145 {
146     typedef detail::str_base base;
147  public:
148     str() {} // new str
149     
150     str(const char* s) : base(s) {} // new str
151     
152     str(char const* start, char const* finish) // new str
153       : base(start, finish)
154     {}
155     
156     str(char const* start, std::size_t length) // new str
157       : base(start, length)
158     {}
159     
160     template <class T>
161     explicit str(T const& other)
162         : base(object(other))
163     {
164     }
165
166     template <class T>
167     str center(T const& width) const
168     {
169         return base::center(object(width));
170     }
171
172     template<class T>
173     long count(T const& sub) const
174     {
175         return base::count(object(sub));
176     }
177
178     template<class T1, class T2>
179     long count(T1 const& sub,T2 const& start) const
180     {
181         return base::count(object(sub), object(start));
182     }
183
184     template<class T1, class T2, class T3>
185     long count(T1 const& sub,T2 const& start, T3 const& end) const
186     {
187         return base::count(object(sub), object(start));
188     }
189
190 #if PY_VERSION_HEX < 0x03000000
191     object decode() const { return base::decode(); }
192     
193     template<class T>
194     object decode(T const& encoding) const
195     {
196         return base::decode(object(encoding));
197     }
198
199     template<class T1, class T2>
200     object decode(T1 const& encoding, T2 const& errors) const
201     {
202         return base::decode(object(encoding),object(errors));
203     }
204 #endif
205
206     object encode() const { return base::encode(); }
207
208     template <class T>
209     object encode(T const& encoding) const
210     {
211         return base::encode(object(encoding));
212     }
213
214     template <class T1, class T2>
215     object encode(T1 const& encoding, T2 const& errors) const
216     {
217         return base::encode(object(encoding),object(errors));
218     }
219
220     template <class T>
221     bool endswith(T const& suffix) const
222     {
223         return base::endswith(object(suffix));
224     }
225
226     template <class T1, class T2>
227     bool endswith(T1 const& suffix, T2 const& start) const
228     {
229         return base::endswith(object(suffix), object(start));
230     }
231
232     template <class T1, class T2, class T3>
233     bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const
234     {
235         return base::endswith(object(suffix), object(start), object(end));
236     }
237     
238     str expandtabs() const { return base::expandtabs(); }
239
240     template <class T>
241     str expandtabs(T const& tabsize) const
242     {
243         return base::expandtabs(object(tabsize));
244     }
245     
246     template <class T>
247     long find(T const& sub) const
248     {
249         return base::find(object(sub));
250     }
251
252     template <class T1, class T2>
253     long find(T1 const& sub, T2 const& start) const
254     {
255         return base::find(object(sub), object(start));
256     }
257
258     template <class T1, class T2, class T3>
259     long find(T1 const& sub, T2 const& start, T3 const& end) const
260     {
261         return base::find(object(sub), object(start), object(end));
262     }
263     
264     template <class T>
265     long index(T const& sub) const
266     {
267         return base::index(object(sub));
268     }
269     
270     template <class T1, class T2>
271     long index(T1 const& sub, T2 const& start) const
272     {
273         return base::index(object(sub), object(start));
274     }
275
276     template <class T1, class T2, class T3>
277     long index(T1 const& sub, T2 const& start, T3 const& end) const
278     {
279         return base::index(object(sub), object(start), object(end));
280     }
281
282     template <class T>
283     str join(T const& sequence) const
284     {
285         return base::join(object(sequence));
286     }
287     
288     template <class T>
289     str ljust(T const& width) const
290     {
291         return base::ljust(object(width));
292     }
293
294     template <class T1, class T2>
295     str replace(T1 const& old, T2 const& new_) const 
296     {
297         return base::replace(object(old),object(new_));
298     }
299
300     template <class T1, class T2, class T3>
301     str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const 
302     {
303         return base::replace(object(old),object(new_), object(maxsplit));
304     }
305     
306     template <class T>
307     long rfind(T const& sub) const
308     {
309         return base::rfind(object(sub));
310     }
311
312     template <class T1, class T2>
313     long rfind(T1 const& sub, T2 const& start) const
314     {
315         return base::rfind(object(sub), object(start));
316     }
317     
318     template <class T1, class T2, class T3>
319     long rfind(T1 const& sub, T2 const& start, T3 const& end) const
320     {
321         return base::rfind(object(sub), object(start), object(end));
322     }
323     
324     template <class T>
325     long rindex(T const& sub) const
326     {
327         return base::rindex(object(sub));
328     }
329
330     template <class T1, class T2>
331     long rindex(T1 const& sub, T2 const& start) const
332     {
333         return base::rindex(object(sub), object(start));
334     }
335
336     template <class T1, class T2, class T3>
337     long rindex(T1 const& sub, T2 const& start, T3 const& end) const
338     {
339         return base::rindex(object(sub), object(start), object(end));
340     }
341
342     template <class T>
343     str rjust(T const& width) const
344     {
345         return base::rjust(object(width));
346     }
347     
348     list split() const { return base::split(); }
349    
350     template <class T>
351     list split(T const& sep) const
352     {
353         return base::split(object(sep));
354     }
355
356     template <class T1, class T2>
357     list split(T1 const& sep, T2 const& maxsplit) const
358     {
359         return base::split(object(sep), object(maxsplit));
360     }
361
362     list splitlines() const { return base::splitlines(); }
363
364     template <class T>
365     list splitlines(T const& keepends) const
366     {
367         return base::splitlines(object(keepends));
368     }
369
370     template <class T>
371     bool startswith(T const& prefix) const
372     {
373         return base::startswith(object(prefix));
374     }
375
376     template <class T1, class T2>
377     bool startswith(T1 const& prefix, T2 const& start) const
378     {
379         return base::startswith(object(prefix), object(start));
380     }
381      
382     template <class T1, class T2, class T3>
383     bool startswith(T1 const& prefix, T2 const& start, T3 const& end) const
384     {
385         return base::startswith(object(prefix), object(start), object(end));
386     }
387
388     template <class T>
389     str translate(T const& table) const
390     {
391         return base::translate(object(table));
392     }
393
394     template <class T1, class T2>
395     str translate(T1 const& table, T2 const& deletechars) const
396     {
397         return base::translate(object(table), object(deletechars));
398     }
399     
400  public: // implementation detail -- for internal use only
401     BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, base)
402 };
403
404 //
405 // Converter Specializations
406 //
407 namespace converter
408 {
409   template <>
410   struct object_manager_traits<str>
411 #if PY_VERSION_HEX >= 0x03000000
412       : pytype_object_manager_traits<&PyUnicode_Type,str>
413 #else
414       : pytype_object_manager_traits<&PyString_Type,str>
415 #endif
416   {
417   };
418 }
419
420 }}  // namespace boost::python
421
422 #endif // STR_20020703_HPP