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