c_regex_traits.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //////////////////////////////////////////////////////////////////////////////
  2. /// \file c_regex_traits.hpp
  3. /// Contains the definition of the c_regex_traits\<\> template, which is a
  4. /// wrapper for the C locale functions that can be used to customize the
  5. /// behavior of static and dynamic regexes.
  6. //
  7. // Copyright 2008 Eric Niebler. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_XPRESSIVE_TRAITS_C_REGEX_TRAITS_HPP_EAN_10_04_2005
  11. #define BOOST_XPRESSIVE_TRAITS_C_REGEX_TRAITS_HPP_EAN_10_04_2005
  12. // MS compatible compilers support #pragma once
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <cstdlib>
  17. #include <boost/config.hpp>
  18. #include <boost/assert.hpp>
  19. #include <boost/xpressive/traits/detail/c_ctype.hpp>
  20. namespace boost { namespace xpressive
  21. {
  22. namespace detail
  23. {
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // empty_locale
  26. struct empty_locale
  27. {
  28. };
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // c_regex_traits_base
  31. template<typename Char, std::size_t SizeOfChar = sizeof(Char)>
  32. struct c_regex_traits_base
  33. {
  34. protected:
  35. template<typename Traits>
  36. void imbue(Traits const &tr)
  37. {
  38. }
  39. };
  40. template<typename Char>
  41. struct c_regex_traits_base<Char, 1>
  42. {
  43. protected:
  44. template<typename Traits>
  45. static void imbue(Traits const &)
  46. {
  47. }
  48. };
  49. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  50. template<std::size_t SizeOfChar>
  51. struct c_regex_traits_base<wchar_t, SizeOfChar>
  52. {
  53. protected:
  54. template<typename Traits>
  55. static void imbue(Traits const &)
  56. {
  57. }
  58. };
  59. #endif
  60. template<typename Char>
  61. Char c_tolower(Char);
  62. template<typename Char>
  63. Char c_toupper(Char);
  64. template<>
  65. inline char c_tolower(char ch)
  66. {
  67. using namespace std;
  68. return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
  69. }
  70. template<>
  71. inline char c_toupper(char ch)
  72. {
  73. using namespace std;
  74. return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
  75. }
  76. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  77. template<>
  78. inline wchar_t c_tolower(wchar_t ch)
  79. {
  80. using namespace std;
  81. return towlower(ch);
  82. }
  83. template<>
  84. inline wchar_t c_toupper(wchar_t ch)
  85. {
  86. using namespace std;
  87. return towupper(ch);
  88. }
  89. #endif
  90. } // namespace detail
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // regex_traits_version_1_tag
  93. //
  94. struct regex_traits_version_1_tag;
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // c_regex_traits
  97. //
  98. /// \brief Encapsaulates the standard C locale functions for use by the
  99. /// \c basic_regex\<\> class template.
  100. template<typename Char>
  101. struct c_regex_traits
  102. : detail::c_regex_traits_base<Char>
  103. {
  104. typedef Char char_type;
  105. typedef std::basic_string<char_type> string_type;
  106. typedef detail::empty_locale locale_type;
  107. typedef typename detail::char_class_impl<Char>::char_class_type char_class_type;
  108. typedef regex_traits_version_2_tag version_tag;
  109. typedef detail::c_regex_traits_base<Char> base_type;
  110. /// Initialize a c_regex_traits object to use the global C locale.
  111. ///
  112. c_regex_traits(locale_type const &loc = locale_type())
  113. : base_type()
  114. {
  115. this->imbue(loc);
  116. }
  117. /// Checks two c_regex_traits objects for equality
  118. ///
  119. /// \return true.
  120. bool operator ==(c_regex_traits<char_type> const &) const
  121. {
  122. return true;
  123. }
  124. /// Checks two c_regex_traits objects for inequality
  125. ///
  126. /// \return false.
  127. bool operator !=(c_regex_traits<char_type> const &) const
  128. {
  129. return false;
  130. }
  131. /// Convert a char to a Char
  132. ///
  133. /// \param ch The source character.
  134. /// \return ch if Char is char, std::btowc(ch) if Char is wchar_t.
  135. static char_type widen(char ch);
  136. /// Returns a hash value for a Char in the range [0, UCHAR_MAX]
  137. ///
  138. /// \param ch The source character.
  139. /// \return a value between 0 and UCHAR_MAX, inclusive.
  140. static unsigned char hash(char_type ch)
  141. {
  142. return static_cast<unsigned char>(std::char_traits<Char>::to_int_type(ch));
  143. }
  144. /// No-op
  145. ///
  146. /// \param ch The source character.
  147. /// \return ch
  148. static char_type translate(char_type ch)
  149. {
  150. return ch;
  151. }
  152. /// Converts a character to lower-case using the current global C locale.
  153. ///
  154. /// \param ch The source character.
  155. /// \return std::tolower(ch) if Char is char, std::towlower(ch) if Char is wchar_t.
  156. static char_type translate_nocase(char_type ch)
  157. {
  158. return detail::c_tolower(ch);
  159. }
  160. /// Converts a character to lower-case using the current global C locale.
  161. ///
  162. /// \param ch The source character.
  163. /// \return std::tolower(ch) if Char is char, std::towlower(ch) if Char is wchar_t.
  164. static char_type tolower(char_type ch)
  165. {
  166. return detail::c_tolower(ch);
  167. }
  168. /// Converts a character to upper-case using the current global C locale.
  169. ///
  170. /// \param ch The source character.
  171. /// \return std::toupper(ch) if Char is char, std::towupper(ch) if Char is wchar_t.
  172. static char_type toupper(char_type ch)
  173. {
  174. return detail::c_toupper(ch);
  175. }
  176. /// Returns a \c string_type containing all the characters that compare equal
  177. /// disregrarding case to the one passed in. This function can only be called
  178. /// if <tt>has_fold_case\<c_regex_traits\<Char\> \>::value</tt> is \c true.
  179. ///
  180. /// \param ch The source character.
  181. /// \return \c string_type containing all chars which are equal to \c ch when disregarding
  182. /// case
  183. //typedef array<char_type, 2> fold_case_type;
  184. string_type fold_case(char_type ch) const
  185. {
  186. BOOST_MPL_ASSERT((is_same<char_type, char>));
  187. char_type ntcs[] = {
  188. detail::c_tolower(ch)
  189. , detail::c_toupper(ch)
  190. , 0
  191. };
  192. if(ntcs[1] == ntcs[0])
  193. ntcs[1] = 0;
  194. return string_type(ntcs);
  195. }
  196. /// Checks to see if a character is within a character range.
  197. ///
  198. /// \param first The bottom of the range, inclusive.
  199. /// \param last The top of the range, inclusive.
  200. /// \param ch The source character.
  201. /// \return first <= ch && ch <= last.
  202. static bool in_range(char_type first, char_type last, char_type ch)
  203. {
  204. return first <= ch && ch <= last;
  205. }
  206. /// Checks to see if a character is within a character range, irregardless of case.
  207. ///
  208. /// \param first The bottom of the range, inclusive.
  209. /// \param last The top of the range, inclusive.
  210. /// \param ch The source character.
  211. /// \return in_range(first, last, ch) || in_range(first, last, tolower(ch)) || in_range(first,
  212. /// last, toupper(ch))
  213. /// \attention The default implementation doesn't do proper Unicode
  214. /// case folding, but this is the best we can do with the standard
  215. /// C locale functions.
  216. static bool in_range_nocase(char_type first, char_type last, char_type ch)
  217. {
  218. return c_regex_traits::in_range(first, last, ch)
  219. || c_regex_traits::in_range(first, last, detail::c_tolower(ch))
  220. || c_regex_traits::in_range(first, last, detail::c_toupper(ch));
  221. }
  222. /// Returns a sort key for the character sequence designated by the iterator range [F1, F2)
  223. /// such that if the character sequence [G1, G2) sorts before the character sequence [H1, H2)
  224. /// then v.transform(G1, G2) < v.transform(H1, H2).
  225. ///
  226. /// \attention Not currently used
  227. template<typename FwdIter>
  228. static string_type transform(FwdIter begin, FwdIter end)
  229. {
  230. BOOST_ASSERT(false); // BUGBUG implement me
  231. }
  232. /// Returns a sort key for the character sequence designated by the iterator range [F1, F2)
  233. /// such that if the character sequence [G1, G2) sorts before the character sequence [H1, H2)
  234. /// when character case is not considered then
  235. /// v.transform_primary(G1, G2) < v.transform_primary(H1, H2).
  236. ///
  237. /// \attention Not currently used
  238. template<typename FwdIter>
  239. static string_type transform_primary(FwdIter begin, FwdIter end)
  240. {
  241. BOOST_ASSERT(false); // BUGBUG implement me
  242. }
  243. /// Returns a sequence of characters that represents the collating element
  244. /// consisting of the character sequence designated by the iterator range [F1, F2).
  245. /// Returns an empty string if the character sequence is not a valid collating element.
  246. ///
  247. /// \attention Not currently used
  248. template<typename FwdIter>
  249. static string_type lookup_collatename(FwdIter begin, FwdIter end)
  250. {
  251. BOOST_ASSERT(false); // BUGBUG implement me
  252. }
  253. /// For the character class name represented by the specified character sequence,
  254. /// return the corresponding bitmask representation.
  255. ///
  256. /// \param begin A forward iterator to the start of the character sequence representing
  257. /// the name of the character class.
  258. /// \param end The end of the character sequence.
  259. /// \param icase Specifies whether the returned bitmask should represent the case-insensitive
  260. /// version of the character class.
  261. /// \return A bitmask representing the character class.
  262. template<typename FwdIter>
  263. static char_class_type lookup_classname(FwdIter begin, FwdIter end, bool icase)
  264. {
  265. return detail::char_class_impl<char_type>::lookup_classname(begin, end, icase);
  266. }
  267. /// Tests a character against a character class bitmask.
  268. ///
  269. /// \param ch The character to test.
  270. /// \param mask The character class bitmask against which to test.
  271. /// \pre mask is a bitmask returned by lookup_classname, or is several such masks bit-or'ed
  272. /// together.
  273. /// \return true if the character is a member of any of the specified character classes, false
  274. /// otherwise.
  275. static bool isctype(char_type ch, char_class_type mask)
  276. {
  277. return detail::char_class_impl<char_type>::isctype(ch, mask);
  278. }
  279. /// Convert a digit character into the integer it represents.
  280. ///
  281. /// \param ch The digit character.
  282. /// \param radix The radix to use for the conversion.
  283. /// \pre radix is one of 8, 10, or 16.
  284. /// \return -1 if ch is not a digit character, the integer value of the character otherwise. If
  285. /// char_type is char, std::strtol is used for the conversion. If char_type is wchar_t,
  286. /// std::wcstol is used.
  287. static int value(char_type ch, int radix);
  288. /// No-op
  289. ///
  290. locale_type imbue(locale_type loc)
  291. {
  292. this->base_type::imbue(*this);
  293. return loc;
  294. }
  295. /// No-op
  296. ///
  297. static locale_type getloc()
  298. {
  299. locale_type loc;
  300. return loc;
  301. }
  302. };
  303. ///////////////////////////////////////////////////////////////////////////////
  304. // c_regex_traits<>::widen specializations
  305. /// INTERNAL ONLY
  306. template<>
  307. inline char c_regex_traits<char>::widen(char ch)
  308. {
  309. return ch;
  310. }
  311. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  312. /// INTERNAL ONLY
  313. template<>
  314. inline wchar_t c_regex_traits<wchar_t>::widen(char ch)
  315. {
  316. using namespace std;
  317. return btowc(ch);
  318. }
  319. #endif
  320. ///////////////////////////////////////////////////////////////////////////////
  321. // c_regex_traits<>::hash specializations
  322. /// INTERNAL ONLY
  323. template<>
  324. inline unsigned char c_regex_traits<char>::hash(char ch)
  325. {
  326. return static_cast<unsigned char>(ch);
  327. }
  328. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  329. /// INTERNAL ONLY
  330. template<>
  331. inline unsigned char c_regex_traits<wchar_t>::hash(wchar_t ch)
  332. {
  333. return static_cast<unsigned char>(ch);
  334. }
  335. #endif
  336. ///////////////////////////////////////////////////////////////////////////////
  337. // c_regex_traits<>::value specializations
  338. /// INTERNAL ONLY
  339. template<>
  340. inline int c_regex_traits<char>::value(char ch, int radix)
  341. {
  342. using namespace std;
  343. BOOST_ASSERT(8 == radix || 10 == radix || 16 == radix);
  344. char begin[2] = { ch, '\0' }, *end = 0;
  345. int val = strtol(begin, &end, radix);
  346. return begin == end ? -1 : val;
  347. }
  348. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  349. /// INTERNAL ONLY
  350. template<>
  351. inline int c_regex_traits<wchar_t>::value(wchar_t ch, int radix)
  352. {
  353. using namespace std;
  354. BOOST_ASSERT(8 == radix || 10 == radix || 16 == radix);
  355. wchar_t begin[2] = { ch, L'\0' }, *end = 0;
  356. int val = wcstol(begin, &end, radix);
  357. return begin == end ? -1 : val;
  358. }
  359. #endif
  360. // Narrow C traits has fold_case() member function.
  361. template<>
  362. struct has_fold_case<c_regex_traits<char> >
  363. : mpl::true_
  364. {
  365. };
  366. }}
  367. #endif