str.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. # include <boost/python/detail/prefix.hpp>
  8. #include <boost/python/object.hpp>
  9. #include <boost/python/list.hpp>
  10. #include <boost/python/converter/pytype_object_mgr_traits.hpp>
  11. // disable defines in <cctype> provided by some system libraries
  12. #undef isspace
  13. #undef islower
  14. #undef isalpha
  15. #undef isdigit
  16. #undef isalnum
  17. #undef isupper
  18. namespace boost { namespace python {
  19. class str;
  20. namespace detail
  21. {
  22. struct BOOST_PYTHON_DECL str_base : object
  23. {
  24. str capitalize() const;
  25. str center(object_cref width) const;
  26. long count(object_cref sub) const;
  27. long count(object_cref sub, object_cref start) const;
  28. long count(object_cref sub, object_cref start, object_cref end) const;
  29. #if PY_VERSION_HEX < 0x03000000
  30. object decode() const;
  31. object decode(object_cref encoding) const;
  32. object decode(object_cref encoding, object_cref errors) const;
  33. #endif
  34. object encode() const;
  35. object encode(object_cref encoding) const;
  36. object encode(object_cref encoding, object_cref errors) const;
  37. bool endswith(object_cref suffix) const;
  38. bool endswith(object_cref suffix, object_cref start) const;
  39. bool endswith(object_cref suffix, object_cref start, object_cref end) const;
  40. str expandtabs() const;
  41. str expandtabs(object_cref tabsize) const;
  42. long find(object_cref sub) const;
  43. long find(object_cref sub, object_cref start) const;
  44. long find(object_cref sub, object_cref start, object_cref end) const;
  45. long index(object_cref sub) const;
  46. long index(object_cref sub, object_cref start) const;
  47. long index(object_cref sub, object_cref start, object_cref end) const;
  48. bool isalnum() const;
  49. bool isalpha() const;
  50. bool isdigit() const;
  51. bool islower() const;
  52. bool isspace() const;
  53. bool istitle() const;
  54. bool isupper() const;
  55. str join(object_cref sequence) const;
  56. str ljust(object_cref width) const;
  57. str lower() const;
  58. str lstrip() const;
  59. str replace(object_cref old, object_cref new_) const;
  60. str replace(object_cref old, object_cref new_, object_cref maxsplit) const;
  61. long rfind(object_cref sub) const;
  62. long rfind(object_cref sub, object_cref start) const;
  63. long rfind(object_cref sub, object_cref start, object_cref end) const;
  64. long rindex(object_cref sub) const;
  65. long rindex(object_cref sub, object_cref start) const;
  66. long rindex(object_cref sub, object_cref start, object_cref end) const;
  67. str rjust(object_cref width) const;
  68. str rstrip() const;
  69. list split() const;
  70. list split(object_cref sep) const;
  71. list split(object_cref sep, object_cref maxsplit) const;
  72. list splitlines() const;
  73. list splitlines(object_cref keepends) const;
  74. bool startswith(object_cref prefix) const;
  75. bool startswith(object_cref prefix, object_cref start) const;
  76. bool startswith(object_cref prefix, object_cref start, object_cref end) const;
  77. str strip() const;
  78. str swapcase() const;
  79. str title() const;
  80. str translate(object_cref table) const;
  81. str translate(object_cref table, object_cref deletechars) const;
  82. str upper() const;
  83. protected:
  84. str_base(); // new str
  85. str_base(const char* s); // new str
  86. str_base(char const* start, char const* finish);
  87. str_base(char const* start, std::size_t length);
  88. explicit str_base(object_cref other);
  89. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str_base, object)
  90. private:
  91. static new_reference call(object const&);
  92. };
  93. }
  94. class str : public detail::str_base
  95. {
  96. typedef detail::str_base base;
  97. public:
  98. str() {} // new str
  99. str(const char* s) : base(s) {} // new str
  100. str(char const* start, char const* finish) // new str
  101. : base(start, finish)
  102. {}
  103. str(char const* start, std::size_t length) // new str
  104. : base(start, length)
  105. {}
  106. template <class T>
  107. explicit str(T const& other)
  108. : base(object(other))
  109. {
  110. }
  111. template <class T>
  112. str center(T const& width) const
  113. {
  114. return base::center(object(width));
  115. }
  116. template<class T>
  117. long count(T const& sub) const
  118. {
  119. return base::count(object(sub));
  120. }
  121. template<class T1, class T2>
  122. long count(T1 const& sub,T2 const& start) const
  123. {
  124. return base::count(object(sub), object(start));
  125. }
  126. template<class T1, class T2, class T3>
  127. long count(T1 const& sub,T2 const& start, T3 const& end) const
  128. {
  129. return base::count(object(sub), object(start), object(end));
  130. }
  131. #if PY_VERSION_HEX < 0x03000000
  132. object decode() const { return base::decode(); }
  133. template<class T>
  134. object decode(T const& encoding) const
  135. {
  136. return base::decode(object(encoding));
  137. }
  138. template<class T1, class T2>
  139. object decode(T1 const& encoding, T2 const& errors) const
  140. {
  141. return base::decode(object(encoding),object(errors));
  142. }
  143. #endif
  144. object encode() const { return base::encode(); }
  145. template <class T>
  146. object encode(T const& encoding) const
  147. {
  148. return base::encode(object(encoding));
  149. }
  150. template <class T1, class T2>
  151. object encode(T1 const& encoding, T2 const& errors) const
  152. {
  153. return base::encode(object(encoding),object(errors));
  154. }
  155. template <class T>
  156. bool endswith(T const& suffix) const
  157. {
  158. return base::endswith(object(suffix));
  159. }
  160. template <class T1, class T2>
  161. bool endswith(T1 const& suffix, T2 const& start) const
  162. {
  163. return base::endswith(object(suffix), object(start));
  164. }
  165. template <class T1, class T2, class T3>
  166. bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const
  167. {
  168. return base::endswith(object(suffix), object(start), object(end));
  169. }
  170. str expandtabs() const { return base::expandtabs(); }
  171. template <class T>
  172. str expandtabs(T const& tabsize) const
  173. {
  174. return base::expandtabs(object(tabsize));
  175. }
  176. template <class T>
  177. long find(T const& sub) const
  178. {
  179. return base::find(object(sub));
  180. }
  181. template <class T1, class T2>
  182. long find(T1 const& sub, T2 const& start) const
  183. {
  184. return base::find(object(sub), object(start));
  185. }
  186. template <class T1, class T2, class T3>
  187. long find(T1 const& sub, T2 const& start, T3 const& end) const
  188. {
  189. return base::find(object(sub), object(start), object(end));
  190. }
  191. template <class T>
  192. long index(T const& sub) const
  193. {
  194. return base::index(object(sub));
  195. }
  196. template <class T1, class T2>
  197. long index(T1 const& sub, T2 const& start) const
  198. {
  199. return base::index(object(sub), object(start));
  200. }
  201. template <class T1, class T2, class T3>
  202. long index(T1 const& sub, T2 const& start, T3 const& end) const
  203. {
  204. return base::index(object(sub), object(start), object(end));
  205. }
  206. template <class T>
  207. str join(T const& sequence) const
  208. {
  209. return base::join(object(sequence));
  210. }
  211. template <class T>
  212. str ljust(T const& width) const
  213. {
  214. return base::ljust(object(width));
  215. }
  216. template <class T1, class T2>
  217. str replace(T1 const& old, T2 const& new_) const
  218. {
  219. return base::replace(object(old),object(new_));
  220. }
  221. template <class T1, class T2, class T3>
  222. str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const
  223. {
  224. return base::replace(object(old),object(new_), object(maxsplit));
  225. }
  226. template <class T>
  227. long rfind(T const& sub) const
  228. {
  229. return base::rfind(object(sub));
  230. }
  231. template <class T1, class T2>
  232. long rfind(T1 const& sub, T2 const& start) const
  233. {
  234. return base::rfind(object(sub), object(start));
  235. }
  236. template <class T1, class T2, class T3>
  237. long rfind(T1 const& sub, T2 const& start, T3 const& end) const
  238. {
  239. return base::rfind(object(sub), object(start), object(end));
  240. }
  241. template <class T>
  242. long rindex(T const& sub) const
  243. {
  244. return base::rindex(object(sub));
  245. }
  246. template <class T1, class T2>
  247. long rindex(T1 const& sub, T2 const& start) const
  248. {
  249. return base::rindex(object(sub), object(start));
  250. }
  251. template <class T1, class T2, class T3>
  252. long rindex(T1 const& sub, T2 const& start, T3 const& end) const
  253. {
  254. return base::rindex(object(sub), object(start), object(end));
  255. }
  256. template <class T>
  257. str rjust(T const& width) const
  258. {
  259. return base::rjust(object(width));
  260. }
  261. list split() const { return base::split(); }
  262. template <class T>
  263. list split(T const& sep) const
  264. {
  265. return base::split(object(sep));
  266. }
  267. template <class T1, class T2>
  268. list split(T1 const& sep, T2 const& maxsplit) const
  269. {
  270. return base::split(object(sep), object(maxsplit));
  271. }
  272. list splitlines() const { return base::splitlines(); }
  273. template <class T>
  274. list splitlines(T const& keepends) const
  275. {
  276. return base::splitlines(object(keepends));
  277. }
  278. template <class T>
  279. bool startswith(T const& prefix) const
  280. {
  281. return base::startswith(object(prefix));
  282. }
  283. template <class T1, class T2>
  284. bool startswith(T1 const& prefix, T2 const& start) const
  285. {
  286. return base::startswith(object(prefix), object(start));
  287. }
  288. template <class T1, class T2, class T3>
  289. bool startswith(T1 const& prefix, T2 const& start, T3 const& end) const
  290. {
  291. return base::startswith(object(prefix), object(start), object(end));
  292. }
  293. template <class T>
  294. str translate(T const& table) const
  295. {
  296. return base::translate(object(table));
  297. }
  298. template <class T1, class T2>
  299. str translate(T1 const& table, T2 const& deletechars) const
  300. {
  301. return base::translate(object(table), object(deletechars));
  302. }
  303. public: // implementation detail -- for internal use only
  304. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, base)
  305. };
  306. //
  307. // Converter Specializations
  308. //
  309. namespace converter
  310. {
  311. template <>
  312. struct object_manager_traits<str>
  313. #if PY_VERSION_HEX >= 0x03000000
  314. : pytype_object_manager_traits<&PyUnicode_Type,str>
  315. #else
  316. : pytype_object_manager_traits<&PyString_Type,str>
  317. #endif
  318. {
  319. };
  320. }
  321. }} // namespace boost::python
  322. #endif // STR_20020703_HPP