w32_regex_traits.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. *
  3. * Copyright (c) 2004
  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. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE w32_regex_traits.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares regular expression traits class w32_regex_traits.
  16. */
  17. #ifndef BOOST_W32_REGEX_TRAITS_HPP_INCLUDED
  18. #define BOOST_W32_REGEX_TRAITS_HPP_INCLUDED
  19. #ifndef BOOST_REGEX_NO_WIN32_LOCALE
  20. #ifndef BOOST_RE_PAT_EXCEPT_HPP
  21. #include <boost/regex/pattern_except.hpp>
  22. #endif
  23. #ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED
  24. #include <boost/regex/v4/regex_traits_defaults.hpp>
  25. #endif
  26. #ifdef BOOST_HAS_THREADS
  27. #include <boost/regex/pending/static_mutex.hpp>
  28. #endif
  29. #ifndef BOOST_REGEX_PRIMARY_TRANSFORM
  30. #include <boost/regex/v4/primary_transform.hpp>
  31. #endif
  32. #ifndef BOOST_REGEX_OBJECT_CACHE_HPP
  33. #include <boost/regex/pending/object_cache.hpp>
  34. #endif
  35. #ifdef BOOST_MSVC
  36. #pragma warning(push)
  37. #pragma warning(disable: 4103)
  38. #endif
  39. #ifdef BOOST_HAS_ABI_HEADERS
  40. # include BOOST_ABI_PREFIX
  41. #endif
  42. #ifdef BOOST_MSVC
  43. #pragma warning(pop)
  44. #endif
  45. #ifdef BOOST_MSVC
  46. #pragma warning(push)
  47. #pragma warning(disable:4786)
  48. #if BOOST_MSVC < 1910
  49. #pragma warning(disable:4800)
  50. #endif
  51. #endif
  52. namespace boost{
  53. //
  54. // forward declaration is needed by some compilers:
  55. //
  56. template <class charT>
  57. class w32_regex_traits;
  58. namespace BOOST_REGEX_DETAIL_NS{
  59. //
  60. // start by typedeffing the types we'll need:
  61. //
  62. typedef ::boost::uint32_t lcid_type; // placeholder for LCID.
  63. typedef ::boost::shared_ptr<void> cat_type; // placeholder for dll HANDLE.
  64. //
  65. // then add wrappers around the actual Win32 API's (ie implementation hiding):
  66. //
  67. BOOST_REGEX_DECL lcid_type BOOST_REGEX_CALL w32_get_default_locale();
  68. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(char, lcid_type);
  69. #ifndef BOOST_NO_WREGEX
  70. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(wchar_t, lcid_type);
  71. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  72. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(unsigned short ca, lcid_type state_id);
  73. #endif
  74. #endif
  75. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(char, lcid_type);
  76. #ifndef BOOST_NO_WREGEX
  77. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(wchar_t, lcid_type);
  78. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  79. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(unsigned short ca, lcid_type state_id);
  80. #endif
  81. #endif
  82. BOOST_REGEX_DECL cat_type BOOST_REGEX_CALL w32_cat_open(const std::string& name);
  83. BOOST_REGEX_DECL std::string BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type state_id, int i, const std::string& def);
  84. #ifndef BOOST_NO_WREGEX
  85. BOOST_REGEX_DECL std::wstring BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type state_id, int i, const std::wstring& def);
  86. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  87. BOOST_REGEX_DECL std::basic_string<unsigned short> BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type, int i, const std::basic_string<unsigned short>& def);
  88. #endif
  89. #endif
  90. BOOST_REGEX_DECL std::string BOOST_REGEX_CALL w32_transform(lcid_type state_id, const char* p1, const char* p2);
  91. #ifndef BOOST_NO_WREGEX
  92. BOOST_REGEX_DECL std::wstring BOOST_REGEX_CALL w32_transform(lcid_type state_id, const wchar_t* p1, const wchar_t* p2);
  93. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  94. BOOST_REGEX_DECL std::basic_string<unsigned short> BOOST_REGEX_CALL w32_transform(lcid_type state_id, const unsigned short* p1, const unsigned short* p2);
  95. #endif
  96. #endif
  97. BOOST_REGEX_DECL char BOOST_REGEX_CALL w32_tolower(char c, lcid_type);
  98. #ifndef BOOST_NO_WREGEX
  99. BOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_tolower(wchar_t c, lcid_type);
  100. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  101. BOOST_REGEX_DECL unsigned short BOOST_REGEX_CALL w32_tolower(unsigned short c, lcid_type state_id);
  102. #endif
  103. #endif
  104. BOOST_REGEX_DECL char BOOST_REGEX_CALL w32_toupper(char c, lcid_type);
  105. #ifndef BOOST_NO_WREGEX
  106. BOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_toupper(wchar_t c, lcid_type);
  107. #endif
  108. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type, boost::uint32_t mask, char c);
  109. #ifndef BOOST_NO_WREGEX
  110. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type, boost::uint32_t mask, wchar_t c);
  111. #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T
  112. BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type state_id, boost::uint32_t m, unsigned short c);
  113. #endif
  114. #endif
  115. //
  116. // class w32_regex_traits_base:
  117. // acts as a container for locale and the facets we are using.
  118. //
  119. template <class charT>
  120. struct w32_regex_traits_base
  121. {
  122. w32_regex_traits_base(lcid_type l)
  123. { imbue(l); }
  124. lcid_type imbue(lcid_type l);
  125. lcid_type m_locale;
  126. };
  127. template <class charT>
  128. inline lcid_type w32_regex_traits_base<charT>::imbue(lcid_type l)
  129. {
  130. lcid_type result(m_locale);
  131. m_locale = l;
  132. return result;
  133. }
  134. //
  135. // class w32_regex_traits_char_layer:
  136. // implements methods that require specialisation for narrow characters:
  137. //
  138. template <class charT>
  139. class w32_regex_traits_char_layer : public w32_regex_traits_base<charT>
  140. {
  141. typedef std::basic_string<charT> string_type;
  142. typedef std::map<charT, regex_constants::syntax_type> map_type;
  143. typedef typename map_type::const_iterator map_iterator_type;
  144. public:
  145. w32_regex_traits_char_layer(const lcid_type l);
  146. regex_constants::syntax_type syntax_type(charT c)const
  147. {
  148. map_iterator_type i = m_char_map.find(c);
  149. return ((i == m_char_map.end()) ? 0 : i->second);
  150. }
  151. regex_constants::escape_syntax_type escape_syntax_type(charT c) const
  152. {
  153. map_iterator_type i = m_char_map.find(c);
  154. if(i == m_char_map.end())
  155. {
  156. if(::boost::BOOST_REGEX_DETAIL_NS::w32_is_lower(c, this->m_locale)) return regex_constants::escape_type_class;
  157. if(::boost::BOOST_REGEX_DETAIL_NS::w32_is_upper(c, this->m_locale)) return regex_constants::escape_type_not_class;
  158. return 0;
  159. }
  160. return i->second;
  161. }
  162. charT tolower(charT c)const
  163. {
  164. return ::boost::BOOST_REGEX_DETAIL_NS::w32_tolower(c, this->m_locale);
  165. }
  166. bool isctype(boost::uint32_t mask, charT c)const
  167. {
  168. return ::boost::BOOST_REGEX_DETAIL_NS::w32_is(this->m_locale, mask, c);
  169. }
  170. private:
  171. string_type get_default_message(regex_constants::syntax_type);
  172. // TODO: use a hash table when available!
  173. map_type m_char_map;
  174. };
  175. template <class charT>
  176. w32_regex_traits_char_layer<charT>::w32_regex_traits_char_layer(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l)
  177. : w32_regex_traits_base<charT>(l)
  178. {
  179. // we need to start by initialising our syntax map so we know which
  180. // character is used for which purpose:
  181. cat_type cat;
  182. std::string cat_name(w32_regex_traits<charT>::get_catalog_name());
  183. if(cat_name.size())
  184. {
  185. cat = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_open(cat_name);
  186. if(!cat)
  187. {
  188. std::string m("Unable to open message catalog: ");
  189. std::runtime_error err(m + cat_name);
  190. boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err);
  191. }
  192. }
  193. //
  194. // if we have a valid catalog then load our messages:
  195. //
  196. if(cat)
  197. {
  198. for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
  199. {
  200. string_type mss = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, i, get_default_message(i));
  201. for(typename string_type::size_type j = 0; j < mss.size(); ++j)
  202. {
  203. this->m_char_map[mss[j]] = i;
  204. }
  205. }
  206. }
  207. else
  208. {
  209. for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
  210. {
  211. const char* ptr = get_default_syntax(i);
  212. while(ptr && *ptr)
  213. {
  214. this->m_char_map[static_cast<charT>(*ptr)] = i;
  215. ++ptr;
  216. }
  217. }
  218. }
  219. }
  220. template <class charT>
  221. typename w32_regex_traits_char_layer<charT>::string_type
  222. w32_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
  223. {
  224. const char* ptr = get_default_syntax(i);
  225. string_type result;
  226. while(ptr && *ptr)
  227. {
  228. result.append(1, static_cast<charT>(*ptr));
  229. ++ptr;
  230. }
  231. return result;
  232. }
  233. //
  234. // specialised version for narrow characters:
  235. //
  236. template <>
  237. class BOOST_REGEX_DECL w32_regex_traits_char_layer<char> : public w32_regex_traits_base<char>
  238. {
  239. typedef std::string string_type;
  240. public:
  241. w32_regex_traits_char_layer(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l)
  242. : w32_regex_traits_base<char>(l)
  243. {
  244. init();
  245. }
  246. regex_constants::syntax_type syntax_type(char c)const
  247. {
  248. return m_char_map[static_cast<unsigned char>(c)];
  249. }
  250. regex_constants::escape_syntax_type escape_syntax_type(char c) const
  251. {
  252. return m_char_map[static_cast<unsigned char>(c)];
  253. }
  254. char tolower(char c)const
  255. {
  256. return m_lower_map[static_cast<unsigned char>(c)];
  257. }
  258. bool isctype(boost::uint32_t mask, char c)const
  259. {
  260. return m_type_map[static_cast<unsigned char>(c)] & mask;
  261. }
  262. private:
  263. regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
  264. char m_lower_map[1u << CHAR_BIT];
  265. boost::uint16_t m_type_map[1u << CHAR_BIT];
  266. void init();
  267. };
  268. //
  269. // class w32_regex_traits_implementation:
  270. // provides pimpl implementation for w32_regex_traits.
  271. //
  272. template <class charT>
  273. class w32_regex_traits_implementation : public w32_regex_traits_char_layer<charT>
  274. {
  275. public:
  276. typedef typename w32_regex_traits<charT>::char_class_type char_class_type;
  277. BOOST_STATIC_CONSTANT(char_class_type, mask_word = 0x0400); // must be C1_DEFINED << 1
  278. BOOST_STATIC_CONSTANT(char_class_type, mask_unicode = 0x0800); // must be C1_DEFINED << 2
  279. BOOST_STATIC_CONSTANT(char_class_type, mask_horizontal = 0x1000); // must be C1_DEFINED << 3
  280. BOOST_STATIC_CONSTANT(char_class_type, mask_vertical = 0x2000); // must be C1_DEFINED << 4
  281. BOOST_STATIC_CONSTANT(char_class_type, mask_base = 0x3ff); // all the masks used by the CT_CTYPE1 group
  282. typedef std::basic_string<charT> string_type;
  283. typedef charT char_type;
  284. w32_regex_traits_implementation(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l);
  285. std::string error_string(regex_constants::error_type n) const
  286. {
  287. if(!m_error_strings.empty())
  288. {
  289. std::map<int, std::string>::const_iterator p = m_error_strings.find(n);
  290. return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second;
  291. }
  292. return get_default_error_string(n);
  293. }
  294. char_class_type lookup_classname(const charT* p1, const charT* p2) const
  295. {
  296. char_class_type result = lookup_classname_imp(p1, p2);
  297. if(result == 0)
  298. {
  299. typedef typename string_type::size_type size_type;
  300. string_type temp(p1, p2);
  301. for(size_type i = 0; i < temp.size(); ++i)
  302. temp[i] = this->tolower(temp[i]);
  303. result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size());
  304. }
  305. return result;
  306. }
  307. string_type lookup_collatename(const charT* p1, const charT* p2) const;
  308. string_type transform_primary(const charT* p1, const charT* p2) const;
  309. string_type transform(const charT* p1, const charT* p2) const
  310. {
  311. return ::boost::BOOST_REGEX_DETAIL_NS::w32_transform(this->m_locale, p1, p2);
  312. }
  313. private:
  314. std::map<int, std::string> m_error_strings; // error messages indexed by numberic ID
  315. std::map<string_type, char_class_type> m_custom_class_names; // character class names
  316. std::map<string_type, string_type> m_custom_collate_names; // collating element names
  317. unsigned m_collate_type; // the form of the collation string
  318. charT m_collate_delim; // the collation group delimiter
  319. //
  320. // helpers:
  321. //
  322. char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
  323. };
  324. template <class charT>
  325. typename w32_regex_traits_implementation<charT>::string_type
  326. w32_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const
  327. {
  328. string_type result;
  329. //
  330. // What we do here depends upon the format of the sort key returned by
  331. // sort key returned by this->transform:
  332. //
  333. switch(m_collate_type)
  334. {
  335. case sort_C:
  336. case sort_unknown:
  337. // the best we can do is translate to lower case, then get a regular sort key:
  338. {
  339. result.assign(p1, p2);
  340. typedef typename string_type::size_type size_type;
  341. for(size_type i = 0; i < result.size(); ++i)
  342. result[i] = this->tolower(result[i]);
  343. result = this->transform(&*result.begin(), &*result.begin() + result.size());
  344. break;
  345. }
  346. case sort_fixed:
  347. {
  348. // get a regular sort key, and then truncate it:
  349. result.assign(this->transform(p1, p2));
  350. result.erase(this->m_collate_delim);
  351. break;
  352. }
  353. case sort_delim:
  354. // get a regular sort key, and then truncate everything after the delim:
  355. result.assign(this->transform(p1, p2));
  356. std::size_t i;
  357. for(i = 0; i < result.size(); ++i)
  358. {
  359. if(result[i] == m_collate_delim)
  360. break;
  361. }
  362. result.erase(i);
  363. break;
  364. }
  365. if(result.empty())
  366. result = string_type(1, charT(0));
  367. return result;
  368. }
  369. template <class charT>
  370. typename w32_regex_traits_implementation<charT>::string_type
  371. w32_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const
  372. {
  373. typedef typename std::map<string_type, string_type>::const_iterator iter_type;
  374. if(m_custom_collate_names.size())
  375. {
  376. iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
  377. if(pos != m_custom_collate_names.end())
  378. return pos->second;
  379. }
  380. #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
  381. && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
  382. std::string name(p1, p2);
  383. #else
  384. std::string name;
  385. const charT* p0 = p1;
  386. while(p0 != p2)
  387. name.append(1, char(*p0++));
  388. #endif
  389. name = lookup_default_collate_name(name);
  390. #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
  391. && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
  392. if(name.size())
  393. return string_type(name.begin(), name.end());
  394. #else
  395. if(name.size())
  396. {
  397. string_type result;
  398. typedef std::string::const_iterator iter;
  399. iter b = name.begin();
  400. iter e = name.end();
  401. while(b != e)
  402. result.append(1, charT(*b++));
  403. return result;
  404. }
  405. #endif
  406. if(p2 - p1 == 1)
  407. return string_type(1, *p1);
  408. return string_type();
  409. }
  410. template <class charT>
  411. w32_regex_traits_implementation<charT>::w32_regex_traits_implementation(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l)
  412. : w32_regex_traits_char_layer<charT>(l)
  413. {
  414. cat_type cat;
  415. std::string cat_name(w32_regex_traits<charT>::get_catalog_name());
  416. if(cat_name.size())
  417. {
  418. cat = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_open(cat_name);
  419. if(!cat)
  420. {
  421. std::string m("Unable to open message catalog: ");
  422. std::runtime_error err(m + cat_name);
  423. boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err);
  424. }
  425. }
  426. //
  427. // if we have a valid catalog then load our messages:
  428. //
  429. if(cat)
  430. {
  431. //
  432. // Error messages:
  433. //
  434. for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0);
  435. i <= boost::regex_constants::error_unknown;
  436. i = static_cast<boost::regex_constants::error_type>(i + 1))
  437. {
  438. const char* p = get_default_error_string(i);
  439. string_type default_message;
  440. while(*p)
  441. {
  442. default_message.append(1, static_cast<charT>(*p));
  443. ++p;
  444. }
  445. string_type s = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, i+200, default_message);
  446. std::string result;
  447. for(std::string::size_type j = 0; j < s.size(); ++j)
  448. {
  449. result.append(1, static_cast<char>(s[j]));
  450. }
  451. m_error_strings[i] = result;
  452. }
  453. //
  454. // Custom class names:
  455. //
  456. static const char_class_type masks[14] =
  457. {
  458. 0x0104u, // C1_ALPHA | C1_DIGIT
  459. 0x0100u, // C1_ALPHA
  460. 0x0020u, // C1_CNTRL
  461. 0x0004u, // C1_DIGIT
  462. (~(0x0020u|0x0008u) & 0x01ffu) | 0x0400u, // not C1_CNTRL or C1_SPACE
  463. 0x0002u, // C1_LOWER
  464. (~0x0020u & 0x01ffu) | 0x0400, // not C1_CNTRL
  465. 0x0010u, // C1_PUNCT
  466. 0x0008u, // C1_SPACE
  467. 0x0001u, // C1_UPPER
  468. 0x0080u, // C1_XDIGIT
  469. 0x0040u, // C1_BLANK
  470. w32_regex_traits_implementation<charT>::mask_word,
  471. w32_regex_traits_implementation<charT>::mask_unicode,
  472. };
  473. static const string_type null_string;
  474. for(unsigned int j = 0; j <= 13; ++j)
  475. {
  476. string_type s(::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, j+300, null_string));
  477. if(s.size())
  478. this->m_custom_class_names[s] = masks[j];
  479. }
  480. }
  481. //
  482. // get the collation format used by m_pcollate:
  483. //
  484. m_collate_type = BOOST_REGEX_DETAIL_NS::find_sort_syntax(this, &m_collate_delim);
  485. }
  486. template <class charT>
  487. typename w32_regex_traits_implementation<charT>::char_class_type
  488. w32_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const
  489. {
  490. static const char_class_type masks[22] =
  491. {
  492. 0,
  493. 0x0104u, // C1_ALPHA | C1_DIGIT
  494. 0x0100u, // C1_ALPHA
  495. 0x0040u, // C1_BLANK
  496. 0x0020u, // C1_CNTRL
  497. 0x0004u, // C1_DIGIT
  498. 0x0004u, // C1_DIGIT
  499. (~(0x0020u|0x0008u|0x0040) & 0x01ffu) | 0x0400u, // not C1_CNTRL or C1_SPACE or C1_BLANK
  500. w32_regex_traits_implementation<charT>::mask_horizontal,
  501. 0x0002u, // C1_LOWER
  502. 0x0002u, // C1_LOWER
  503. (~0x0020u & 0x01ffu) | 0x0400, // not C1_CNTRL
  504. 0x0010u, // C1_PUNCT
  505. 0x0008u, // C1_SPACE
  506. 0x0008u, // C1_SPACE
  507. 0x0001u, // C1_UPPER
  508. w32_regex_traits_implementation<charT>::mask_unicode,
  509. 0x0001u, // C1_UPPER
  510. w32_regex_traits_implementation<charT>::mask_vertical,
  511. 0x0104u | w32_regex_traits_implementation<charT>::mask_word,
  512. 0x0104u | w32_regex_traits_implementation<charT>::mask_word,
  513. 0x0080u, // C1_XDIGIT
  514. };
  515. if(m_custom_class_names.size())
  516. {
  517. typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter;
  518. map_iter pos = m_custom_class_names.find(string_type(p1, p2));
  519. if(pos != m_custom_class_names.end())
  520. return pos->second;
  521. }
  522. std::size_t state_id = 1 + BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
  523. if(state_id < sizeof(masks) / sizeof(masks[0]))
  524. return masks[state_id];
  525. return masks[0];
  526. }
  527. template <class charT>
  528. boost::shared_ptr<const w32_regex_traits_implementation<charT> > create_w32_regex_traits(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l)
  529. {
  530. // TODO: create a cache for previously constructed objects.
  531. return boost::object_cache< ::boost::BOOST_REGEX_DETAIL_NS::lcid_type, w32_regex_traits_implementation<charT> >::get(l, 5);
  532. }
  533. } // BOOST_REGEX_DETAIL_NS
  534. template <class charT>
  535. class w32_regex_traits
  536. {
  537. public:
  538. typedef charT char_type;
  539. typedef std::size_t size_type;
  540. typedef std::basic_string<char_type> string_type;
  541. typedef ::boost::BOOST_REGEX_DETAIL_NS::lcid_type locale_type;
  542. typedef boost::uint_least32_t char_class_type;
  543. struct boost_extensions_tag{};
  544. w32_regex_traits()
  545. : m_pimpl(BOOST_REGEX_DETAIL_NS::create_w32_regex_traits<charT>(::boost::BOOST_REGEX_DETAIL_NS::w32_get_default_locale()))
  546. { }
  547. static size_type length(const char_type* p)
  548. {
  549. return std::char_traits<charT>::length(p);
  550. }
  551. regex_constants::syntax_type syntax_type(charT c)const
  552. {
  553. return m_pimpl->syntax_type(c);
  554. }
  555. regex_constants::escape_syntax_type escape_syntax_type(charT c) const
  556. {
  557. return m_pimpl->escape_syntax_type(c);
  558. }
  559. charT translate(charT c) const
  560. {
  561. return c;
  562. }
  563. charT translate_nocase(charT c) const
  564. {
  565. return this->m_pimpl->tolower(c);
  566. }
  567. charT translate(charT c, bool icase) const
  568. {
  569. return icase ? this->m_pimpl->tolower(c) : c;
  570. }
  571. charT tolower(charT c) const
  572. {
  573. return this->m_pimpl->tolower(c);
  574. }
  575. charT toupper(charT c) const
  576. {
  577. return ::boost::BOOST_REGEX_DETAIL_NS::w32_toupper(c, this->m_pimpl->m_locale);
  578. }
  579. string_type transform(const charT* p1, const charT* p2) const
  580. {
  581. return ::boost::BOOST_REGEX_DETAIL_NS::w32_transform(this->m_pimpl->m_locale, p1, p2);
  582. }
  583. string_type transform_primary(const charT* p1, const charT* p2) const
  584. {
  585. return m_pimpl->transform_primary(p1, p2);
  586. }
  587. char_class_type lookup_classname(const charT* p1, const charT* p2) const
  588. {
  589. return m_pimpl->lookup_classname(p1, p2);
  590. }
  591. string_type lookup_collatename(const charT* p1, const charT* p2) const
  592. {
  593. return m_pimpl->lookup_collatename(p1, p2);
  594. }
  595. bool isctype(charT c, char_class_type f) const
  596. {
  597. if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_base)
  598. && (this->m_pimpl->isctype(f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_base, c)))
  599. return true;
  600. else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_unicode) && BOOST_REGEX_DETAIL_NS::is_extended(c))
  601. return true;
  602. else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_word) && (c == '_'))
  603. return true;
  604. else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_vertical)
  605. && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == '\v')))
  606. return true;
  607. else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_horizontal)
  608. && this->isctype(c, 0x0008u) && !this->isctype(c, BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_vertical))
  609. return true;
  610. return false;
  611. }
  612. boost::intmax_t toi(const charT*& p1, const charT* p2, int radix)const
  613. {
  614. return ::boost::BOOST_REGEX_DETAIL_NS::global_toi(p1, p2, radix, *this);
  615. }
  616. int value(charT c, int radix)const
  617. {
  618. int result = (int)::boost::BOOST_REGEX_DETAIL_NS::global_value(c);
  619. return result < radix ? result : -1;
  620. }
  621. locale_type imbue(locale_type l)
  622. {
  623. ::boost::BOOST_REGEX_DETAIL_NS::lcid_type result(getloc());
  624. m_pimpl = BOOST_REGEX_DETAIL_NS::create_w32_regex_traits<charT>(l);
  625. return result;
  626. }
  627. locale_type getloc()const
  628. {
  629. return m_pimpl->m_locale;
  630. }
  631. std::string error_string(regex_constants::error_type n) const
  632. {
  633. return m_pimpl->error_string(n);
  634. }
  635. //
  636. // extension:
  637. // set the name of the message catalog in use (defaults to "boost_regex").
  638. //
  639. static std::string catalog_name(const std::string& name);
  640. static std::string get_catalog_name();
  641. private:
  642. boost::shared_ptr<const BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT> > m_pimpl;
  643. //
  644. // catalog name handler:
  645. //
  646. static std::string& get_catalog_name_inst();
  647. #ifdef BOOST_HAS_THREADS
  648. static static_mutex& get_mutex_inst();
  649. #endif
  650. };
  651. template <class charT>
  652. std::string w32_regex_traits<charT>::catalog_name(const std::string& name)
  653. {
  654. #ifdef BOOST_HAS_THREADS
  655. static_mutex::scoped_lock lk(get_mutex_inst());
  656. #endif
  657. std::string result(get_catalog_name_inst());
  658. get_catalog_name_inst() = name;
  659. return result;
  660. }
  661. template <class charT>
  662. std::string& w32_regex_traits<charT>::get_catalog_name_inst()
  663. {
  664. static std::string s_name;
  665. return s_name;
  666. }
  667. template <class charT>
  668. std::string w32_regex_traits<charT>::get_catalog_name()
  669. {
  670. #ifdef BOOST_HAS_THREADS
  671. static_mutex::scoped_lock lk(get_mutex_inst());
  672. #endif
  673. std::string result(get_catalog_name_inst());
  674. return result;
  675. }
  676. #ifdef BOOST_HAS_THREADS
  677. template <class charT>
  678. static_mutex& w32_regex_traits<charT>::get_mutex_inst()
  679. {
  680. static static_mutex s_mutex = BOOST_STATIC_MUTEX_INIT;
  681. return s_mutex;
  682. }
  683. #endif
  684. } // boost
  685. #ifdef BOOST_MSVC
  686. #pragma warning(pop)
  687. #endif
  688. #ifdef BOOST_MSVC
  689. #pragma warning(push)
  690. #pragma warning(disable: 4103)
  691. #endif
  692. #ifdef BOOST_HAS_ABI_HEADERS
  693. # include BOOST_ABI_SUFFIX
  694. #endif
  695. #ifdef BOOST_MSVC
  696. #pragma warning(pop)
  697. #endif
  698. #endif // BOOST_REGEX_NO_WIN32_LOCALE
  699. #endif