w32_regex_traits.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 651 行 · 第 1/2 页

CPP
651
字号
#ifndef BOOST_NO_ANSI_APIS   int bytes = ::LCMapStringA(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      0,  // destination buffer      0        // size of destination buffer      );   if(!bytes)      return std::string(p1, p2);   std::string result(++bytes, '\0');   bytes = ::LCMapStringA(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      &*result.begin(),  // destination buffer      bytes        // size of destination buffer      );#else   UINT code_page = get_code_page_for_locale_id(id);   if(code_page == 0)      return std::string(p1, p2);   int src_len = static_cast<int>(p2 - p1);   LPWSTR wide_p1 = (LPWSTR)_alloca( (src_len + 1) * 2 );   if(::MultiByteToWideChar(code_page, 0,  p1, src_len,  wide_p1, src_len + 1) == 0)      return std::string(p1, p2);   int bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      wide_p1,  // source string      src_len,        // number of characters in source string      0,  // destination buffer      0        // size of destination buffer      );   if(!bytes)      return std::string(p1, p2);   std::string result(++bytes, '\0');   bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      wide_p1,  // source string      src_len,        // number of characters in source string      (LPWSTR)&*result.begin(),  // destination buffer      bytes        // size of destination buffer      );#endif   if(bytes > static_cast<int>(result.size()))      return std::string(p1, p2);   while(result.size() && result[result.size()-1] == '\0')   {      result.erase(result.size()-1);   }   return result;}#ifndef BOOST_NO_WREGEXBOOST_REGEX_DECL std::wstring BOOST_REGEX_CALL w32_transform(lcid_type id, const wchar_t* p1, const wchar_t* p2){   int bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      0,  // destination buffer      0        // size of destination buffer      );   if(!bytes)      return std::wstring(p1, p2);   std::string result(++bytes, '\0');   bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      reinterpret_cast<wchar_t*>(&*result.begin()),  // destination buffer *of bytes*      bytes        // size of destination buffer      );   if(bytes > static_cast<int>(result.size()))      return std::wstring(p1, p2);   while(result.size() && result[result.size()-1] == L'\0')   {      result.erase(result.size()-1);   }   std::wstring r2;   for(std::string::size_type i = 0; i < result.size(); ++i)      r2.append(1, static_cast<wchar_t>(static_cast<unsigned char>(result[i])));   return r2;}#ifdef BOOST_REGEX_HAS_OTHER_WCHAR_TBOOST_REGEX_DECL std::basic_string<unsigned short> BOOST_REGEX_CALL w32_transform(lcid_type id, const unsigned short* p1, const unsigned short* p2){   int bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      (LPCWSTR)p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      0,  // destination buffer      0        // size of destination buffer      );   if(!bytes)      return std::basic_string<unsigned short>(p1, p2);   std::string result(++bytes, '\0');   bytes = ::LCMapStringW(      id,       // locale identifier      LCMAP_SORTKEY,  // mapping transformation type      (LPCWSTR)p1,  // source string      static_cast<int>(p2 - p1),        // number of characters in source string      reinterpret_cast<wchar_t*>(&*result.begin()),  // destination buffer *of bytes*      bytes        // size of destination buffer      );   if(bytes > static_cast<int>(result.size()))      return std::basic_string<unsigned short>(p1, p2);   while(result.size() && result[result.size()-1] == L'\0')   {      result.erase(result.size()-1);   }   std::basic_string<unsigned short> r2;   for(std::string::size_type i = 0; i < result.size(); ++i)      r2.append(1, static_cast<unsigned short>(static_cast<unsigned char>(result[i])));   return r2;}#endif#endifBOOST_REGEX_DECL char BOOST_REGEX_CALL w32_tolower(char c, lcid_type id){   char result[2];#ifndef BOOST_NO_ANSI_APIS   int b = ::LCMapStringA(      id,       // locale identifier      LCMAP_LOWERCASE,  // mapping transformation type      &c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;#else   UINT code_page = get_code_page_for_locale_id(id);   if (code_page == 0)      return c;   WCHAR wide_c;   if (::MultiByteToWideChar(code_page, 0,  &c, 1,  &wide_c, 1) == 0)      return c;   WCHAR wide_result;   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_LOWERCASE,  // mapping transformation type      &wide_c,  // source string      1,        // number of characters in source string      &wide_result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   if (::WideCharToMultiByte(code_page, 0,  &wide_result, 1,  result, 2,  NULL, NULL) == 0)       return c;#endif   return result[0];}#ifndef BOOST_NO_WREGEXBOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_tolower(wchar_t c, lcid_type id){   wchar_t result[2];   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_LOWERCASE,  // mapping transformation type      &c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   return result[0];}#ifdef BOOST_REGEX_HAS_OTHER_WCHAR_TBOOST_REGEX_DECL unsigned short BOOST_REGEX_CALL w32_tolower(unsigned short c, lcid_type id){   wchar_t result[2];   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_LOWERCASE,  // mapping transformation type      (wchar_t const*)&c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   return result[0];}#endif#endifBOOST_REGEX_DECL char BOOST_REGEX_CALL w32_toupper(char c, lcid_type id){   char result[2];#ifndef BOOST_NO_ANSI_APIS   int b = ::LCMapStringA(      id,       // locale identifier      LCMAP_UPPERCASE,  // mapping transformation type      &c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;#else   UINT code_page = get_code_page_for_locale_id(id);   if(code_page == 0)       return c;   WCHAR wide_c;   if (::MultiByteToWideChar(code_page, 0,  &c, 1,  &wide_c, 1) == 0)       return c;   WCHAR wide_result;   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_UPPERCASE,  // mapping transformation type      &wide_c,  // source string      1,        // number of characters in source string      &wide_result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   if (::WideCharToMultiByte(code_page, 0,  &wide_result, 1,  result, 2,  NULL, NULL) == 0)       return c;#endif   return result[0];}#ifndef BOOST_NO_WREGEXBOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_toupper(wchar_t c, lcid_type id){   wchar_t result[2];   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_UPPERCASE,  // mapping transformation type      &c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   return result[0];}#ifdef BOOST_REGEX_HAS_OTHER_WCHAR_TBOOST_REGEX_DECL unsigned short BOOST_REGEX_CALL w32_toupper(unsigned short c, lcid_type id){   wchar_t result[2];   int b = ::LCMapStringW(      id,       // locale identifier      LCMAP_UPPERCASE,  // mapping transformation type      (wchar_t const*)&c,  // source string      1,        // number of characters in source string      result,  // destination buffer      1);        // size of destination buffer   if(b == 0)      return c;   return result[0];}#endif#endifBOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type id, boost::uint32_t m, char c){   WORD mask;#ifndef BOOST_NO_ANSI_APIS   if(::GetStringTypeExA(id, CT_CTYPE1, &c, 1, &mask) && (mask & m & w32_regex_traits_implementation<char>::mask_base))      return true;#else   UINT code_page = get_code_page_for_locale_id(id);   if(code_page == 0)       return false;   WCHAR wide_c;   if (::MultiByteToWideChar(code_page, 0,  &c, 1,  &wide_c, 1) == 0)       return false;   if(::GetStringTypeExW(id, CT_CTYPE1, &wide_c, 1, &mask) && (mask & m & w32_regex_traits_implementation<char>::mask_base))      return true;#endif   if((m & w32_regex_traits_implementation<char>::mask_word) && (c == '_'))      return true;   return false;}#ifndef BOOST_NO_WREGEXBOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type id, boost::uint32_t m, wchar_t c){   WORD mask;   if(::GetStringTypeExW(id, CT_CTYPE1, &c, 1, &mask) && (mask & m & w32_regex_traits_implementation<wchar_t>::mask_base))      return true;   if((m & w32_regex_traits_implementation<wchar_t>::mask_word) && (c == '_'))      return true;   if((m & w32_regex_traits_implementation<wchar_t>::mask_unicode) && (c > 0xff))      return true;   return false;}#ifdef BOOST_REGEX_HAS_OTHER_WCHAR_TBOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type id, boost::uint32_t m, unsigned short c){   WORD mask;   if(::GetStringTypeExW(id, CT_CTYPE1, (wchar_t const*)&c, 1, &mask) && (mask & m & w32_regex_traits_implementation<wchar_t>::mask_base))      return true;   if((m & w32_regex_traits_implementation<wchar_t>::mask_word) && (c == '_'))      return true;   if((m & w32_regex_traits_implementation<wchar_t>::mask_unicode) && (c > 0xff))      return true;   return false;}#endif#endif} // re_detail} // boost#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?