new feature: String::format
This commit is contained in:
		
							parent
							
								
									198ffefdd8
								
							
						
					
					
						commit
						af73a5df92
					
				|  | @ -46,6 +46,50 @@ e2d::String & e2d::String::operator=(const char *cstr) | ||||||
| 	return (*this); | 	return (*this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | e2d::String & e2d::String::format(const char * format, ...) | ||||||
|  | { | ||||||
|  | 	std::string tmp; | ||||||
|  | 
 | ||||||
|  | 	va_list marker = NULL; | ||||||
|  | 	va_start(marker, format); | ||||||
|  | 
 | ||||||
|  | 	size_t num_of_chars = _vscprintf(format, marker); | ||||||
|  | 
 | ||||||
|  | 	if (num_of_chars > tmp.capacity())  | ||||||
|  | 	{ | ||||||
|  | 		tmp.resize(num_of_chars + 1); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	vsprintf_s(const_cast<LPSTR>(tmp.data()), tmp.capacity(), format, marker); | ||||||
|  | 
 | ||||||
|  | 	va_end(marker); | ||||||
|  | 
 | ||||||
|  | 	m_str = static_cast<wchar_t*>(_bstr_t(tmp.c_str())); | ||||||
|  | 	return (*this); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | e2d::String & e2d::String::format(const wchar_t * format, ...) | ||||||
|  | { | ||||||
|  | 	std::wstring tmp; | ||||||
|  | 
 | ||||||
|  | 	va_list marker = NULL; | ||||||
|  | 	va_start(marker, format); | ||||||
|  | 
 | ||||||
|  | 	size_t num_of_chars = _vscwprintf(format, marker); | ||||||
|  | 
 | ||||||
|  | 	if (num_of_chars > tmp.capacity()) | ||||||
|  | 	{ | ||||||
|  | 		tmp.resize(num_of_chars + 1); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	vswprintf_s(const_cast<LPWSTR>(tmp.data()), tmp.capacity(), format, marker); | ||||||
|  | 
 | ||||||
|  | 	va_end(marker); | ||||||
|  | 
 | ||||||
|  | 	m_str = tmp.c_str(); | ||||||
|  | 	return (*this); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| e2d::String & e2d::String::operator=(const String &str) | e2d::String & e2d::String::operator=(const String &str) | ||||||
| { | { | ||||||
| 	m_str = str.m_str; | 	m_str = str.m_str; | ||||||
|  |  | ||||||
|  | @ -148,9 +148,12 @@ public: | ||||||
| 	// 将字符串转化为 bool 型
 | 	// 将字符串转化为 bool 型
 | ||||||
| 	bool toBool() const; | 	bool toBool() const; | ||||||
| 
 | 
 | ||||||
| 	// 将任意类型转化为字符串
 | 	// 将数字转化为字符串
 | ||||||
| 	template<typename T> | 	template<typename T> | ||||||
| 	static String toString(const T value); | 	static String toString(T value); | ||||||
|  | 
 | ||||||
|  | 	String& format(const char * format, ...); | ||||||
|  | 	String& format(const wchar_t * format, ...); | ||||||
| 
 | 
 | ||||||
| 	// 赋值运算符
 | 	// 赋值运算符
 | ||||||
| 	String& operator= (const String &); | 	String& operator= (const String &); | ||||||
|  | @ -712,12 +715,11 @@ protected: | ||||||
| // String 类模板函数定义
 | // String 类模板函数定义
 | ||||||
| 
 | 
 | ||||||
| template<typename T> | template<typename T> | ||||||
| inline e2d::String e2d::String::toString(const T value) | inline e2d::String e2d::String::toString(T value) | ||||||
| { | { | ||||||
| 	std::wostringstream ss; | 	String tmp; | ||||||
| 	ss << value; | 	tmp.m_str = std::to_wstring(value); | ||||||
| 	String str = ss.str().c_str(); | 	return std::move(tmp); | ||||||
| 	return std::move(str); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | @ -240,10 +240,10 @@ | ||||||
|     <ClCompile Include="..\..\core\Tool\Music.cpp" /> |     <ClCompile Include="..\..\core\Tool\Music.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Tool\Random.cpp" /> |     <ClCompile Include="..\..\core\Tool\Random.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Tool\Timer.cpp" /> |     <ClCompile Include="..\..\core\Tool\Timer.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransition.cpp" /> |     <ClCompile Include="..\..\core\Transition\Transition.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionEmerge.cpp" /> |     <ClCompile Include="..\..\core\Transition\TransitionEmerge.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionFade.cpp" /> |     <ClCompile Include="..\..\core\Transition\TransitionFade.cpp" /> | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionMove.cpp" /> |     <ClCompile Include="..\..\core\Transition\TransitionMove.cpp" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClInclude Include="..\..\core\eactions.h" /> |     <ClInclude Include="..\..\core\eactions.h" /> | ||||||
|  |  | ||||||
|  | @ -27,18 +27,6 @@ | ||||||
|     </Filter> |     </Filter> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionFade.cpp"> |  | ||||||
|       <Filter>Transition</Filter> |  | ||||||
|     </ClCompile> |  | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionEmerge.cpp"> |  | ||||||
|       <Filter>Transition</Filter> |  | ||||||
|     </ClCompile> |  | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransitionMove.cpp"> |  | ||||||
|       <Filter>Transition</Filter> |  | ||||||
|     </ClCompile> |  | ||||||
|     <ClCompile Include="..\..\core\Transition\ETransition.cpp"> |  | ||||||
|       <Filter>Transition</Filter> |  | ||||||
|     </ClCompile> |  | ||||||
|     <ClCompile Include="..\..\core\Base\Input.cpp"> |     <ClCompile Include="..\..\core\Base\Input.cpp"> | ||||||
|       <Filter>Base</Filter> |       <Filter>Base</Filter> | ||||||
|     </ClCompile> |     </ClCompile> | ||||||
|  | @ -189,6 +177,18 @@ | ||||||
|     <ClCompile Include="..\..\core\Tool\Path.cpp"> |     <ClCompile Include="..\..\core\Tool\Path.cpp"> | ||||||
|       <Filter>Tool</Filter> |       <Filter>Tool</Filter> | ||||||
|     </ClCompile> |     </ClCompile> | ||||||
|  |     <ClCompile Include="..\..\core\Transition\Transition.cpp"> | ||||||
|  |       <Filter>Transition</Filter> | ||||||
|  |     </ClCompile> | ||||||
|  |     <ClCompile Include="..\..\core\Transition\TransitionEmerge.cpp"> | ||||||
|  |       <Filter>Transition</Filter> | ||||||
|  |     </ClCompile> | ||||||
|  |     <ClCompile Include="..\..\core\Transition\TransitionFade.cpp"> | ||||||
|  |       <Filter>Transition</Filter> | ||||||
|  |     </ClCompile> | ||||||
|  |     <ClCompile Include="..\..\core\Transition\TransitionMove.cpp"> | ||||||
|  |       <Filter>Transition</Filter> | ||||||
|  |     </ClCompile> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClInclude Include="..\..\core\etools.h" /> |     <ClInclude Include="..\..\core\etools.h" /> | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue