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)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE: wc_regex_traits.cpp
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Implements out of line members for c_regex_traits<wchar_t>
20 #define BOOST_REGEX_SOURCE
22 #include <boost/detail/workaround.hpp>
26 #if defined(_DLL_CPPLIB) && !defined(_M_CEE_PURE) && defined(_NATIVE_WCHAR_T_DEFINED) \
27 && !(defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) || defined(__STD_RWCOMPILER_H__) || defined(_RWSTD_VER))\
28 && BOOST_WORKAROUND(BOOST_MSVC, <1600)
30 // This is a horrible workaround, but without declaring these symbols extern we get
31 // duplicate symbol errors when linking if the application is built without
35 # define BOOST_REGEX_STDLIB_DECL _CRTIMP2_PURE
37 # define BOOST_REGEX_STDLIB_DECL _CRTIMP2
42 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
43 template class BOOST_REGEX_STDLIB_DECL allocator<unsigned short>;
44 template class BOOST_REGEX_STDLIB_DECL _String_val<unsigned short, allocator<unsigned short> >;
45 template class BOOST_REGEX_STDLIB_DECL basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >;
48 #if BOOST_WORKAROUND(BOOST_MSVC, > 1300) && BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400))
49 template<> BOOST_REGEX_STDLIB_DECL std::size_t __cdecl char_traits<unsigned short>::length(unsigned short const*);
52 template BOOST_REGEX_STDLIB_DECL bool __cdecl operator==(
53 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&,
54 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&);
55 template BOOST_REGEX_STDLIB_DECL bool __cdecl operator==(
56 const unsigned short *,
57 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&);
58 template BOOST_REGEX_STDLIB_DECL bool __cdecl operator==(
59 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&,
60 const unsigned short *);
61 template BOOST_REGEX_STDLIB_DECL bool __cdecl operator<(
62 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&,
63 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&);
64 template BOOST_REGEX_STDLIB_DECL bool __cdecl operator>(
65 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&,
66 const basic_string<unsigned short, char_traits<unsigned short>, allocator<unsigned short> >&);
70 #include <boost/regex/config.hpp>
71 #include <boost/detail/workaround.hpp>
73 #if !BOOST_WORKAROUND(__BORLANDC__, < 0x560)
75 #include <boost/regex/v4/c_regex_traits.hpp>
76 #ifndef BOOST_NO_WREGEX
77 #include <boost/regex/v4/primary_transform.hpp>
78 #include <boost/regex/v4/regex_traits_defaults.hpp>
80 #if defined(BOOST_NO_STDC_NAMESPACE)
88 c_regex_traits<wchar_t>::string_type BOOST_REGEX_CALL c_regex_traits<wchar_t>::transform(const wchar_t* p1, const wchar_t* p2)
92 std::wstring src(p1, p2);
93 std::wstring result(s, L' ');
94 while(s < (r = std::wcsxfrm(&*result.begin(), src.c_str(), s)))
96 result.append(r - s + 3, L' ');
103 c_regex_traits<wchar_t>::string_type BOOST_REGEX_CALL c_regex_traits<wchar_t>::transform_primary(const wchar_t* p1, const wchar_t* p2)
105 static wchar_t s_delim;
106 static const int s_collate_type = ::boost::re_detail::find_sort_syntax(static_cast<const c_regex_traits<wchar_t>*>(0), &s_delim);
109 // What we do here depends upon the format of the sort key returned by
110 // sort key returned by this->transform:
112 switch(s_collate_type)
114 case ::boost::re_detail::sort_C:
115 case ::boost::re_detail::sort_unknown:
116 // the best we can do is translate to lower case, then get a regular sort key:
118 result.assign(p1, p2);
119 for(std::wstring::size_type i = 0; i < result.size(); ++i)
120 result[i] = (std::towlower)(result[i]);
121 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
124 case ::boost::re_detail::sort_fixed:
126 // get a regular sort key, and then truncate it:
127 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
128 result.erase(s_delim);
131 case ::boost::re_detail::sort_delim:
132 // get a regular sort key, and then truncate everything after the delim:
133 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
134 if(result.size() && (result[0] == s_delim))
137 for(i = 0; i < result.size(); ++i)
139 if(result[i] == s_delim)
146 result = std::wstring(1, char(0));
152 char_class_space=1<<0,
153 char_class_print=1<<1,
154 char_class_cntrl=1<<2,
155 char_class_upper=1<<3,
156 char_class_lower=1<<4,
157 char_class_alpha=1<<5,
158 char_class_digit=1<<6,
159 char_class_punct=1<<7,
160 char_class_xdigit=1<<8,
161 char_class_alnum=char_class_alpha|char_class_digit,
162 char_class_graph=char_class_alnum|char_class_punct,
163 char_class_blank=1<<9,
164 char_class_word=1<<10,
165 char_class_unicode=1<<11,
166 char_class_horizontal=1<<12,
167 char_class_vertical=1<<13
170 c_regex_traits<wchar_t>::char_class_type BOOST_REGEX_CALL c_regex_traits<wchar_t>::lookup_classname(const wchar_t* p1, const wchar_t* p2)
172 static const char_class_type masks[] =
182 char_class_horizontal,
193 char_class_alnum | char_class_word,
194 char_class_alnum | char_class_word,
198 int id = ::boost::re_detail::get_default_class_id(p1, p2);
201 std::wstring s(p1, p2);
202 for(std::wstring::size_type i = 0; i < s.size(); ++i)
203 s[i] = (std::towlower)(s[i]);
204 id = ::boost::re_detail::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
206 BOOST_ASSERT(id+1 < static_cast<int>(sizeof(masks) / sizeof(masks[0])));
210 bool BOOST_REGEX_CALL c_regex_traits<wchar_t>::isctype(wchar_t c, char_class_type mask)
213 ((mask & char_class_space) && (std::iswspace)(c))
214 || ((mask & char_class_print) && (std::iswprint)(c))
215 || ((mask & char_class_cntrl) && (std::iswcntrl)(c))
216 || ((mask & char_class_upper) && (std::iswupper)(c))
217 || ((mask & char_class_lower) && (std::iswlower)(c))
218 || ((mask & char_class_alpha) && (std::iswalpha)(c))
219 || ((mask & char_class_digit) && (std::iswdigit)(c))
220 || ((mask & char_class_punct) && (std::iswpunct)(c))
221 || ((mask & char_class_xdigit) && (std::iswxdigit)(c))
222 || ((mask & char_class_blank) && (std::iswspace)(c) && !::boost::re_detail::is_separator(c))
223 || ((mask & char_class_word) && (c == '_'))
224 || ((mask & char_class_unicode) && (c & ~static_cast<wchar_t>(0xff)))
225 || ((mask & char_class_vertical) && (::boost::re_detail::is_separator(c) || (c == L'\v')))
226 || ((mask & char_class_horizontal) && (std::iswspace)(c) && !::boost::re_detail::is_separator(c) && (c != L'\v'));
229 c_regex_traits<wchar_t>::string_type BOOST_REGEX_CALL c_regex_traits<wchar_t>::lookup_collatename(const wchar_t* p1, const wchar_t* p2)
231 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
232 && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
233 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
234 std::string name(p1, p2);
237 const wchar_t* p0 = p1;
239 name.append(1, char(*p0++));
241 name = ::boost::re_detail::lookup_default_collate_name(name);
242 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
243 && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
244 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
246 return string_type(name.begin(), name.end());
251 typedef std::string::const_iterator iter;
252 iter b = name.begin();
255 result.append(1, wchar_t(*b++));
260 return string_type(1, *p1);
261 return string_type();
264 int BOOST_REGEX_CALL c_regex_traits<wchar_t>::value(wchar_t c, int radix)
267 // workaround for broken wcstol:
268 if((std::iswxdigit)(c) == 0)
271 wchar_t b[2] = { c, '\0', };
273 int result = std::wcstol(b, &ep, radix);
279 #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
280 c_regex_traits<unsigned short>::string_type BOOST_REGEX_CALL c_regex_traits<unsigned short>::transform(const unsigned short* p1, const unsigned short* p2)
282 std::wstring result = c_regex_traits<wchar_t>::transform((const wchar_t*)p1, (const wchar_t*)p2);
283 return string_type(result.begin(), result.end());
286 c_regex_traits<unsigned short>::string_type BOOST_REGEX_CALL c_regex_traits<unsigned short>::transform_primary(const unsigned short* p1, const unsigned short* p2)
288 std::wstring result = c_regex_traits<wchar_t>::transform_primary((const wchar_t*)p1, (const wchar_t*)p2);
289 return string_type(result.begin(), result.end());
292 c_regex_traits<unsigned short>::char_class_type BOOST_REGEX_CALL c_regex_traits<unsigned short>::lookup_classname(const unsigned short* p1, const unsigned short* p2)
294 return c_regex_traits<wchar_t>::lookup_classname((const wchar_t*)p1, (const wchar_t*)p2);
297 c_regex_traits<unsigned short>::string_type BOOST_REGEX_CALL c_regex_traits<unsigned short>::lookup_collatename(const unsigned short* p1, const unsigned short* p2)
299 std::wstring result = c_regex_traits<wchar_t>::lookup_collatename((const wchar_t*)p1, (const wchar_t*)p2);
300 return string_type(result.begin(), result.end());
303 bool BOOST_REGEX_CALL c_regex_traits<unsigned short>::isctype(unsigned short c, char_class_type m)
305 return c_regex_traits<wchar_t>::isctype(c, m);
308 int BOOST_REGEX_CALL c_regex_traits<unsigned short>::value(unsigned short c, int radix)
310 return c_regex_traits<wchar_t>::value(c, radix);
317 #endif // BOOST_NO_WREGEX
319 #endif // __BORLANDC__