source: dev/trunk/ab5.0/jenga/src/common/String.cpp

Last change on this file was 829, checked in by イグトランス (egtra), 12 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain; charset=Shift_JIS
File size: 2.0 KB
Line 
1#include "stdafx.h"
2#include <boost/numeric/conversion/cast.hpp>
3#include <boost/algorithm/string/replace.hpp>
4
5using boost::numeric_cast;
6using boost::implicit_cast;
7
8bool Jenga::Common::IsExistString(const Jenga::Common::Strings &strings, const std::string &findStr)
9{
10 return boost::find(strings, findStr) != strings.end();
11}
12
13std::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
19std::string Jenga::Common::ToString(int n)
20{
21 // VC++ 2010だとこのキャストが必要。
22 return std::to_string(implicit_cast<long long>(n));
23}
24
25std::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
58std::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
89bool Jenga::Common::IsIdentifierTopChar( char c )
90{
91 return ('A' <= c && c <= 'Z')
92 || ('a' <= c && c <= 'z')
93 || c == '_' ;
94}
95
96bool Jenga::Common::IsIdentifierChar( char c )
97{
98 return IsIdentifierTopChar(c)
99 || ('0' <= c && c <= '9');
100}
Note: See TracBrowser for help on using the repository browser.