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
convert_to_string(object data)15 object convert_to_string(object data)
16 {
17 return str(data);
18 }
19
work_with_string(object print)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 #if PY_VERSION_HEX < 0x03000000
30 print(data.encode("utf-8"));
31 print(data.decode("utf-8"));
32 #else
33 print(data.encode("utf-8").attr("decode")("utf-8"));
34 print(data.encode("utf-8").attr("decode")("utf-8"));
35 #endif
36
37 BOOST_ASSERT(!data.endswith("xx"));
38 BOOST_ASSERT(!data.startswith("test"));
39
40 print(data.splitlines());
41 print(data.strip());
42 print(data.swapcase());
43 print(data.title());
44
45 print("find");
46 print(data.find("demo"));
47 print(data.find("demo"),3,5);
48 print(data.find(std::string("demo")));
49 print(data.find(std::string("demo"),9));
50
51 print("expandtabs");
52 str tabstr("\t\ttab\tdemo\t!");
53 print(tabstr.expandtabs());
54 print(tabstr.expandtabs(4));
55 print(tabstr.expandtabs(7L));
56
57 print("operators");
58 print( str("part1") + str("part2") );
59 // print( str("a test string").slice(3,_) );
60 // print( str("another test")[5] );
61
62 print(data.replace("demo",std::string("blabla")));
63 print(data.rfind("i",5));
64 print(data.rindex("i",5));
65
66 BOOST_ASSERT(!data.startswith("asdf"));
67 BOOST_ASSERT(!data.endswith("asdf"));
68
69 print(data.translate(str('a')*256));
70
71
72 bool tmp = data.isalnum() || data.isalpha() || data.isdigit() || data.islower() ||
73 data.isspace() || data.istitle() || data.isupper();
74 (void)tmp; // ignored.
75 }
76
77
BOOST_PYTHON_MODULE(str_ext)78 BOOST_PYTHON_MODULE(str_ext)
79 {
80 def("convert_to_string",convert_to_string);
81 def("work_with_string",work_with_string);
82 }
83
84 #include "module_tail.cpp"
85