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/module.hpp> |
---|
5 | #include <boost/assert.hpp> |
---|
6 | |
---|
7 | #include <boost/python/def.hpp> |
---|
8 | #include <boost/python/class.hpp> |
---|
9 | #include <boost/python/str.hpp> |
---|
10 | #define BOOST_ENABLE_ASSERT_HANDLER |
---|
11 | #include <boost/assert.hpp> |
---|
12 | |
---|
13 | using namespace boost::python; |
---|
14 | |
---|
15 | object convert_to_string(object data) |
---|
16 | { |
---|
17 | return str(data); |
---|
18 | } |
---|
19 | |
---|
20 | void work_with_string(object print) |
---|
21 | { |
---|
22 | str data("this is a demo string"); |
---|
23 | print(data.split(" ")); |
---|
24 | print(data.split(" ",3)); |
---|
25 | print(str("<->").join(data.split(" "))); |
---|
26 | print(data.capitalize()); |
---|
27 | print('[' + data.center(30) + ']'); |
---|
28 | print(data.count("t")); |
---|
29 | print(data.encode("utf-8")); |
---|
30 | print(data.decode("utf-8")); |
---|
31 | |
---|
32 | BOOST_ASSERT(!data.endswith("xx")); |
---|
33 | BOOST_ASSERT(!data.startswith("test")); |
---|
34 | |
---|
35 | print(data.splitlines()); |
---|
36 | print(data.strip()); |
---|
37 | print(data.swapcase()); |
---|
38 | print(data.title()); |
---|
39 | |
---|
40 | print("find"); |
---|
41 | print(data.find("demo")); |
---|
42 | print(data.find("demo"),3,5); |
---|
43 | print(data.find(std::string("demo"))); |
---|
44 | print(data.find(std::string("demo"),9)); |
---|
45 | |
---|
46 | print("expandtabs"); |
---|
47 | str tabstr("\t\ttab\tdemo\t!"); |
---|
48 | print(tabstr.expandtabs()); |
---|
49 | print(tabstr.expandtabs(4)); |
---|
50 | print(tabstr.expandtabs(7L)); |
---|
51 | |
---|
52 | print("operators"); |
---|
53 | print( str("part1") + str("part2") ); |
---|
54 | // print( str("a test string").slice(3,_) ); |
---|
55 | // print( str("another test")[5] ); |
---|
56 | |
---|
57 | print(data.replace("demo",std::string("blabla"))); |
---|
58 | print(data.rfind("i",5)); |
---|
59 | print(data.rindex("i",5)); |
---|
60 | |
---|
61 | BOOST_ASSERT(!data.startswith("asdf")); |
---|
62 | BOOST_ASSERT(!data.endswith("asdf")); |
---|
63 | |
---|
64 | print(data.translate(str('a')*256)); |
---|
65 | |
---|
66 | |
---|
67 | bool tmp = data.isalnum() || data.isalpha() || data.isdigit() || data.islower() || |
---|
68 | data.isspace() || data.istitle() || data.isupper(); |
---|
69 | (void)tmp; // ignored. |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | BOOST_PYTHON_MODULE(str_ext) |
---|
74 | { |
---|
75 | def("convert_to_string",convert_to_string); |
---|
76 | def("work_with_string",work_with_string); |
---|
77 | } |
---|
78 | |
---|
79 | #include "module_tail.cpp" |
---|