1 /*
2 	GWEN
3 	Copyright (c) 2010 Facepunch Studios
4 	See license in Gwen.h
5 */
6 
7 #pragma once
8 #ifndef GWEN_UTILITY_H
9 #define GWEN_UTILITY_H
10 #include <sstream>
11 #include <vector>
12 #include "Gwen/Structures.h"
13 
14 namespace Gwen
15 {
16 namespace Utility
17 {
18 template <typename T>
Max(const T & x,const T & y)19 const T& Max(const T& x, const T& y)
20 {
21 	if (y < x) return x;
22 	return y;
23 }
24 
25 template <typename T>
Min(const T & x,const T & y)26 const T& Min(const T& x, const T& y)
27 {
28 	if (y > x) return x;
29 	return y;
30 }
31 
32 #ifdef _MSC_VER
33 #pragma warning(push)
34 #pragma warning(disable : 4996)
35 #endif
36 
UnicodeToString(const UnicodeString & strIn)37 inline String UnicodeToString(const UnicodeString& strIn)
38 {
39 	if (!strIn.length()) return "";
40 
41 	String temp(strIn.length(), (char)0);
42 
43 	std::use_facet<std::ctype<wchar_t> >(std::locale()).narrow(&strIn[0], &strIn[0] + strIn.length(), ' ', &temp[0]);
44 
45 	return temp;
46 }
47 
StringToUnicode(const String & strIn)48 inline UnicodeString StringToUnicode(const String& strIn)
49 {
50 	if (!strIn.length()) return L"";
51 
52 	UnicodeString temp(strIn.length(), (wchar_t)0);
53 
54 	std::use_facet<std::ctype<wchar_t> >(std::locale()).widen(&strIn[0], &strIn[0] + strIn.length(), &temp[0]);
55 
56 	return temp;
57 }
58 
59 #ifdef _MSC_VER
60 #pragma warning(pop)
61 #endif
62 
63 template <class T>
ToString(const T & object)64 String ToString(const T& object)
65 {
66 	std::ostringstream os;
67 	os << object;
68 	return os.str();
69 }
70 
71 inline Gwen::Rect ClampRectToRect(Gwen::Rect inside, Gwen::Rect outside, bool clampSize = false)
72 {
73 	if (inside.x < outside.x)
74 		inside.x = outside.x;
75 
76 	if (inside.y < outside.y)
77 		inside.y = outside.y;
78 
79 	if (inside.x + inside.w > outside.x + outside.w)
80 	{
81 		if (clampSize)
82 			inside.w = outside.w;
83 		else
84 			inside.x = outside.x + outside.w - inside.w;
85 	}
86 	if (inside.y + inside.h > outside.y + outside.h)
87 	{
88 		if (clampSize)
89 			inside.h = outside.h;
90 		else
91 			inside.y = outside.w + outside.h - inside.h;
92 	}
93 
94 	return inside;
95 }
96 
97 GWEN_EXPORT UnicodeString Format(const wchar_t* fmt, ...);
98 
99 namespace Strings
100 {
101 typedef std::vector<Gwen::String> List;
102 typedef std::vector<Gwen::UnicodeString> UnicodeList;
103 
104 GWEN_EXPORT void Split(const Gwen::String& str, const Gwen::String& seperator, Strings::List& outbits, bool bLeaveSeperators = false);
105 GWEN_EXPORT void Split(const Gwen::UnicodeString& str, const Gwen::UnicodeString& seperator, Strings::UnicodeList& outbits, bool bLeaveSeperators = false);
106 
107 template <typename T>
TrimLeft(const T & str,const T & strChars)108 T TrimLeft(const T& str, const T& strChars)
109 {
110 	T outstr = str;
111 	outstr.erase(0, outstr.find_first_not_of(strChars));
112 	return outstr;
113 }
114 
115 namespace To
116 {
117 GWEN_EXPORT bool Bool(const Gwen::String& str);
118 GWEN_EXPORT int Int(const Gwen::String& str);
119 GWEN_EXPORT float Float(const Gwen::String& str);
120 GWEN_EXPORT bool Floats(const Gwen::String& str, float* f, size_t iCount);
121 }  // namespace To
122 }  // namespace Strings
123 }  // namespace Utility
124 
125 }  // namespace Gwen
126 #endif
127