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