collate_info.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. *
  3. * Copyright (c) 2005
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. // most of the workarounds and headers we need are already in here:
  12. #include <boost/regex.hpp>
  13. #include <boost/regex/v4/primary_transform.hpp>
  14. #include <assert.h>
  15. #include <boost/detail/lightweight_main.hpp>
  16. #include <iostream>
  17. #include <iomanip>
  18. #ifdef BOOST_INTEL
  19. #pragma warning(disable:1418 981 983 2259)
  20. #endif
  21. #ifdef BOOST_NO_STDC_NAMESPACE
  22. namespace std{
  23. using ::strxfrm;
  24. #ifndef BOOST_NO_WREGEX
  25. using ::wcsxfrm;
  26. #endif
  27. }
  28. #endif
  29. #include <iostream>
  30. template <class charT>
  31. int make_int(charT c)
  32. {
  33. return c;
  34. }
  35. int make_int(char c)
  36. {
  37. return static_cast<unsigned char>(c);
  38. }
  39. template <class charT>
  40. void print_string(const std::basic_string<charT>& s)
  41. {
  42. typedef typename std::basic_string<charT>::size_type size_type;
  43. std::cout.put(static_cast<unsigned char>('"'));
  44. for(size_type i = 0; i < s.size(); ++i)
  45. {
  46. if((s[i] > ' ') && (s[i] <= 'z'))
  47. {
  48. std::cout.put(static_cast<unsigned char>(s[i]));
  49. }
  50. else
  51. {
  52. std::cout << "\\x" << std::hex << make_int(s[i]);
  53. }
  54. }
  55. std::cout.put(static_cast<unsigned char>('"'));
  56. }
  57. void print_c_char(char c)
  58. {
  59. char buf[50];
  60. const char cbuf[2] = { c, 0, };
  61. std::size_t len = std::strxfrm(buf, cbuf, 50);
  62. std:: cout << len << " ";
  63. std::string s(buf);
  64. print_string(s);
  65. }
  66. #ifndef BOOST_NO_WREGEX
  67. void print_c_char(wchar_t c)
  68. {
  69. wchar_t buf[50];
  70. const wchar_t cbuf[2] = { c, 0, };
  71. std::size_t len = std::wcsxfrm(buf, cbuf, 50);
  72. std:: cout << len << " ";
  73. std::wstring s(buf);
  74. print_string(s);
  75. }
  76. #endif
  77. template <class charT>
  78. void print_c_info(charT, const char* name)
  79. {
  80. std::cout << "Info for " << name << " C API's:" << std::endl;
  81. std::cout << " \"a\" : ";
  82. print_c_char(charT('a'));
  83. std::cout << std::endl;
  84. std::cout << " \"A\" : ";
  85. print_c_char(charT('A'));
  86. std::cout << std::endl;
  87. std::cout << " \"z\" : ";
  88. print_c_char(charT('z'));
  89. std::cout << std::endl;
  90. std::cout << " \"Z\" : ";
  91. print_c_char(charT('Z'));
  92. std::cout << std::endl;
  93. std::cout << " \";\" : ";
  94. print_c_char(charT(';'));
  95. std::cout << std::endl;
  96. std::cout << " \"{\" : ";
  97. print_c_char(charT('{'));
  98. std::cout << std::endl;
  99. }
  100. template <class charT>
  101. void print_cpp_char(charT c)
  102. {
  103. #ifndef BOOST_NO_STD_LOCALE
  104. std::locale l;
  105. const std::collate<charT>& col = BOOST_USE_FACET(std::collate<charT>, l);
  106. std::basic_string<charT> result = col.transform(&c, &c+1);
  107. std::cout << result.size() << " ";
  108. print_string(result);
  109. std::size_t n = result.find(charT(0));
  110. if(n != std::basic_string<charT>::npos)
  111. {
  112. std::cerr << "(Error in location of null, found: " << n << ")";
  113. }
  114. #endif
  115. }
  116. template <class charT>
  117. void print_cpp_info(charT, const char* name)
  118. {
  119. std::cout << "Info for " << name << " C++ locale API's:" << std::endl;
  120. std::cout << " \"a\" : ";
  121. print_cpp_char(charT('a'));
  122. std::cout << std::endl;
  123. std::cout << " \"A\" : ";
  124. print_cpp_char(charT('A'));
  125. std::cout << std::endl;
  126. std::cout << " \"z\" : ";
  127. print_cpp_char(charT('z'));
  128. std::cout << std::endl;
  129. std::cout << " \"Z\" : ";
  130. print_cpp_char(charT('Z'));
  131. std::cout << std::endl;
  132. std::cout << " \";\" : ";
  133. print_cpp_char(charT(';'));
  134. std::cout << std::endl;
  135. std::cout << " \"{\" : ";
  136. print_cpp_char(charT('{'));
  137. std::cout << std::endl;
  138. }
  139. template <class traits>
  140. void print_sort_syntax(const traits& pt, const char* name)
  141. {
  142. std::cout << "Sort Key Syntax for type " << name << ":\n";
  143. typedef typename traits::char_type char_type;
  144. char_type delim;
  145. unsigned result = ::boost::BOOST_REGEX_DETAIL_NS::find_sort_syntax(&pt, &delim);
  146. std::cout << " ";
  147. switch(result)
  148. {
  149. case boost::BOOST_REGEX_DETAIL_NS::sort_C:
  150. std::cout << "sort_C";
  151. break;
  152. case boost::BOOST_REGEX_DETAIL_NS::sort_fixed:
  153. std::cout << "sort_fixed" << " " << static_cast<int>(delim);
  154. break;
  155. case boost::BOOST_REGEX_DETAIL_NS::sort_delim:
  156. {
  157. std::cout << "sort_delim" << " ";
  158. std::basic_string<char_type> s(1, delim);
  159. print_string(s);
  160. }
  161. break;
  162. case boost::BOOST_REGEX_DETAIL_NS::sort_unknown:
  163. std::cout << "sort_unknown";
  164. break;
  165. default:
  166. std::cout << "bad_value";
  167. break;
  168. }
  169. std::cout << std::endl;
  170. typedef typename traits::string_type string_type;
  171. typedef typename traits::char_type char_type;
  172. char_type c[5] = { 'a', 'A', ';', '{', '}', };
  173. for(int i = 0; i < 5; ++i)
  174. {
  175. string_type s(1, c[i]);
  176. string_type sk = pt.transform(s.c_str(), s.c_str() + s.size());
  177. string_type skp = pt.transform_primary(s.c_str(), s.c_str() + s.size());
  178. print_string(s);
  179. std::cout << " ";
  180. print_string(sk);
  181. std::cout << " ";
  182. print_string(skp);
  183. std::cout << std::endl;
  184. }
  185. }
  186. #ifndef BOOST_NO_STD_LOCALE
  187. template <class charT>
  188. void print_ctype_info(charT, const char* name)
  189. {
  190. std::locale l;
  191. const std::ctype<charT>& ct = BOOST_USE_FACET(std::ctype<charT>, l);
  192. typedef typename std::ctype<charT>::mask mask_type;
  193. mask_type m = static_cast<mask_type>(std::ctype<charT>::lower | std::ctype<charT>::upper);
  194. bool result = ct.is(m, static_cast<charT>('a')) && ct.is(m , static_cast<charT>('A'));
  195. std::cout << "Checking std::ctype<" << name << ">::is(mask, c):" << std::endl;
  196. #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
  197. std::cout << " Boost.Regex believes this facet to be buggy..." << std::endl;
  198. #else
  199. std::cout << " Boost.Regex believes this facet to be correct..." << std::endl;
  200. #endif
  201. std::cout << " Actual behavior, appears to be " << (result ? "correct." : "buggy.") << std::endl;
  202. assert(ct.is(std::ctype<charT>::alnum, 'a'));
  203. assert(ct.is(std::ctype<charT>::alnum, 'A'));
  204. assert(ct.is(std::ctype<charT>::alnum, '0'));
  205. }
  206. #endif
  207. int cpp_main(int /*argc*/, char * /*argv*/[])
  208. {
  209. print_c_info(char(0), "char");
  210. #ifndef BOOST_NO_WREGEX
  211. print_c_info(wchar_t(0), "wchar_t");
  212. #endif
  213. print_cpp_info(char(0), "char");
  214. #ifndef BOOST_NO_WREGEX
  215. print_cpp_info(wchar_t(0), "wchar_t");
  216. #endif
  217. #if !BOOST_WORKAROUND(__BORLANDC__, < 0x560)
  218. boost::c_regex_traits<char> a;
  219. print_sort_syntax(a, "boost::c_regex_traits<char>");
  220. #ifndef BOOST_NO_WREGEX
  221. boost::c_regex_traits<wchar_t> b;
  222. print_sort_syntax(b, "boost::c_regex_traits<wchar_t>");
  223. #endif
  224. #endif
  225. #ifndef BOOST_NO_STD_LOCALE
  226. boost::cpp_regex_traits<char> c;
  227. print_sort_syntax(c, "boost::cpp_regex_traits<char>");
  228. #ifndef BOOST_NO_WREGEX
  229. boost::cpp_regex_traits<wchar_t> d;
  230. print_sort_syntax(d, "boost::cpp_regex_traits<wchar_t>");
  231. #endif
  232. print_ctype_info(char(0), "char");
  233. #ifndef BOOST_NO_WREGEX
  234. print_ctype_info(wchar_t(0), "wchar_t");
  235. #endif
  236. #endif
  237. return 0;
  238. }