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