[829] | 1 | #include "stdafx.h"
|
---|
| 2 | #include <boost/numeric/conversion/cast.hpp>
|
---|
| 3 | #include <boost/algorithm/string/replace.hpp>
|
---|
| 4 |
|
---|
| 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)
|
---|
| 9 | {
|
---|
| 10 | return boost::find(strings, findStr) != strings.end();
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | std::string& Jenga::Common::StringReplace(std::string& str, const std::string &sb, const std::string &sa)
|
---|
| 14 | {
|
---|
| 15 | boost::algorithm::replace_all(str, sb, sa);
|
---|
| 16 | return str;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | std::string Jenga::Common::ToString(int n)
|
---|
| 20 | {
|
---|
| 21 | // VC++ 2010だとこのキャストが必要。
|
---|
| 22 | return std::to_string(implicit_cast<long long>(n));
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | std::string Jenga::Common::ToString( const std::wstring &wstr )
|
---|
| 26 | {
|
---|
| 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,
|
---|
| 34 | 0,
|
---|
| 35 | wstr.data(), srcSize,
|
---|
| 36 | nullptr, 0,
|
---|
| 37 | nullptr, nullptr);
|
---|
| 38 | if (needSize <= 0)
|
---|
| 39 | {
|
---|
| 40 | throw std::runtime_error("WideCharToMultiByte error");
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | std::string ret(needSize, '\0');
|
---|
| 44 | int res = WideCharToMultiByte(
|
---|
| 45 | CP_ACP,
|
---|
| 46 | 0,
|
---|
| 47 | wstr.data(), srcSize,
|
---|
| 48 | &ret[0], needSize,
|
---|
| 49 | nullptr, nullptr);
|
---|
| 50 | if (res <= 0)
|
---|
| 51 | {
|
---|
| 52 | throw std::runtime_error("WideCharToMultiByte error");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return ret;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | std::wstring Jenga::Common::ToWString(const std::string &str)
|
---|
| 59 | {
|
---|
| 60 | if (str.empty())
|
---|
| 61 | {
|
---|
| 62 | return std::wstring();
|
---|
| 63 | }
|
---|
| 64 | int srcSize = numeric_cast<int>(str.size());
|
---|
| 65 | int needSize = MultiByteToWideChar(
|
---|
| 66 | CP_ACP,
|
---|
| 67 | 0,
|
---|
| 68 | str.data(), srcSize,
|
---|
| 69 | nullptr, 0);
|
---|
| 70 | if (needSize <= 0)
|
---|
| 71 | {
|
---|
| 72 | throw std::runtime_error("WideCharToMultiByte error");
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | std::wstring ret(needSize, L'\0');
|
---|
| 76 | int res = MultiByteToWideChar(
|
---|
| 77 | CP_ACP,
|
---|
| 78 | 0,
|
---|
| 79 | str.data(), srcSize,
|
---|
| 80 | &ret[0], needSize);
|
---|
| 81 | if (res <= 0)
|
---|
| 82 | {
|
---|
| 83 | throw std::runtime_error("WideCharToMultiByte error");
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | return ret;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | bool Jenga::Common::IsIdentifierTopChar( char c )
|
---|
| 90 | {
|
---|
| 91 | return ('A' <= c && c <= 'Z')
|
---|
| 92 | || ('a' <= c && c <= 'z')
|
---|
| 93 | || c == '_' ;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | bool Jenga::Common::IsIdentifierChar( char c )
|
---|
| 97 | {
|
---|
| 98 | return IsIdentifierTopChar(c)
|
---|
| 99 | || ('0' <= c && c <= '9');
|
---|
| 100 | }
|
---|