Changeset 826 in dev for branches/egtra/ab5.0/jenga/src
- Timestamp:
- Mar 18, 2012, 11:48:48 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/egtra/ab5.0/jenga/src/common/String.cpp
r763 r826 1 1 #include "stdafx.h" 2 #include <algorithm> 2 #include <boost/numeric/conversion/cast.hpp> 3 #include <boost/algorithm/string/replace.hpp> 3 4 4 bool Jenga::Common::IsExistString( const Jenga::Common::Strings &strings, const std::string &findStr ) 5 using boost::numeric_cast; 6 using boost::implicit_cast; 7 8 bool Jenga::Common::IsExistString(const Jenga::Common::Strings &strings, const std::string &findStr) 5 9 { 6 return std::find( strings.begin(), strings.end(), findStr) != strings.end();10 return boost::find(strings, findStr) != strings.end(); 7 11 } 8 12 9 std::string& Jenga::Common::StringReplace( std::string& str, const std::string &sb, const std::string &sa)13 std::string& Jenga::Common::StringReplace(std::string& str, const std::string &sb, const std::string &sa) 10 14 { 11 std::string::size_type n, nb = 0; 12 13 while ((n = str.find(sb,nb)) != std::string::npos) 14 { 15 str.replace(n,sb.size(),sa); 16 nb = n + sa.size(); 17 } 18 15 boost::algorithm::replace_all(str, sb, sa); 19 16 return str; 20 17 } 21 18 22 std::string Jenga::Common::ToString( int n)19 std::string Jenga::Common::ToString(int n) 23 20 { 24 char temp[255]; 25 wsprintf( temp, "%d", n ); 26 return temp; 21 // VC++ 2010だとこのキャストが必要。 22 return std::to_string(implicit_cast<long long>(n)); 27 23 } 28 24 29 25 std::string Jenga::Common::ToString( const std::wstring &wstr ) 30 26 { 31 int needSize = WideCharToMultiByte( 32 CP_THREAD_ACP, 27 if (wstr.empty()) 28 { 29 return std::string(); 30 } 31 int srcSize = numeric_cast<int>(wstr.size()); 32 int needSize = WideCharToMultiByte( 33 CP_ACP, 33 34 0, 34 wstr.data(), static_cast<int>(wstr.size()), 35 NULL, NULL, 36 NULL, NULL ); 35 wstr.data(), srcSize, 36 nullptr, 0, 37 nullptr, nullptr); 38 if (needSize <= 0) 39 { 40 throw std::runtime_error("WideCharToMultiByte error"); 41 } 37 42 38 char *pstr = (char *)calloc( needSize, 1);39 WideCharToMultiByte(40 CP_ THREAD_ACP,43 std::string ret(needSize, '\0'); 44 int res = WideCharToMultiByte( 45 CP_ACP, 41 46 0, 42 wstr.data(), static_cast<int>(wstr.size()), 43 pstr, needSize, 44 NULL, NULL ); 47 wstr.data(), srcSize, 48 &ret[0], needSize, 49 nullptr, nullptr); 50 if (res <= 0) 51 { 52 throw std::runtime_error("WideCharToMultiByte error"); 53 } 45 54 46 std::string result(pstr, needSize); 47 48 free( pstr ); 49 50 return result; 55 return ret; 51 56 } 52 57 53 std::wstring Jenga::Common::ToWString( const std::string &str)58 std::wstring Jenga::Common::ToWString(const std::string &str) 54 59 { 55 int size = MultiByteToWideChar( 60 if (str.empty()) 61 { 62 return std::wstring(); 63 } 64 int srcSize = numeric_cast<int>(str.size()); 65 int needSize = MultiByteToWideChar( 56 66 CP_ACP, 57 67 0, 58 str.data(), static_cast<int>(str.size()), 59 NULL, 0 ); 68 str.data(), srcSize, 69 nullptr, 0); 70 if (needSize <= 0) 71 { 72 throw std::runtime_error("WideCharToMultiByte error"); 73 } 60 74 61 LPWSTR pwstr = (LPWSTR)calloc( size, sizeof (wchar_t) ); 62 63 MultiByteToWideChar( 75 std::wstring ret(needSize, L'\0'); 76 int res = MultiByteToWideChar( 64 77 CP_ACP, 65 78 0, 66 str.data(), static_cast<int>(str.size()), 67 pwstr, size ); 79 str.data(), srcSize, 80 &ret[0], needSize); 81 if (res <= 0) 82 { 83 throw std::runtime_error("WideCharToMultiByte error"); 84 } 68 85 69 std::wstring wstr( pwstr, size ); 70 71 free( pwstr ); 72 73 return wstr; 86 return ret; 74 87 } 75 88 76 89 bool Jenga::Common::IsIdentifierTopChar( char c ) 77 90 { 78 return ( isalpha( c ) || c == '_' ); 91 return ('A' <= c && c <= 'Z') 92 || ('a' <= c && c <= 'z') 93 || c == '_' ; 79 94 } 80 95 81 96 bool Jenga::Common::IsIdentifierChar( char c ) 82 97 { 83 return ( IsIdentifierTopChar( c ) || isdigit( c ) ); 98 return IsIdentifierTopChar(c) 99 || ('0' <= c && c <= '9'); 84 100 }
Note:
See TracChangeset
for help on using the changeset viewer.