StringUtils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Variant.h"
  24. namespace Atomic
  25. {
  26. /// Parse a bool from a string. Check for the first non-empty character (converted to lowercase) being either 't', 'y' or '1'.
  27. ATOMIC_API bool ToBool(const String& source);
  28. /// Parse a bool from a C string. Check for the first non-empty character (converted to lowercase) being either 't', 'y' or '1'.
  29. ATOMIC_API bool ToBool(const char* source);
  30. /// Parse a float from a string.
  31. ATOMIC_API float ToFloat(const String& source);
  32. /// Parse a float from a C string.
  33. ATOMIC_API float ToFloat(const char* source);
  34. /// Parse a double from a string.
  35. ATOMIC_API double ToDouble(const String& source);
  36. /// Parse a double from a C string.
  37. ATOMIC_API double ToDouble(const char* source);
  38. /// Parse an integer from a string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  39. ATOMIC_API int ToInt(const String& source, int base = 10);
  40. /// Parse an integer from a C string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  41. ATOMIC_API int ToInt(const char* source, int base = 10);
  42. /// Parse an unsigned integer from a string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  43. ATOMIC_API unsigned ToUInt(const String& source, int base = 10);
  44. /// Parse an unsigned integer from a C string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  45. ATOMIC_API unsigned ToUInt(const char* source, int base = 10);
  46. /// Parse an 64 bit integer from a string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  47. ATOMIC_API long long ToInt64(const String& source, int base = 10);
  48. /// Parse an 64 bit integer from a C string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  49. ATOMIC_API long long ToInt64(const char* source, int base = 10);
  50. /// Parse an unsigned 64 bit integer from a string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  51. ATOMIC_API unsigned long long ToUInt64(const String& source, int base = 10);
  52. /// Parse an unsigned 64 bit integer from a C string. Assumed to be decimal by default (base 10). Use base 0 to autodetect from string.
  53. ATOMIC_API unsigned long long ToUInt64(const char* source, int base = 10);
  54. /// Parse a Color from a string.
  55. ATOMIC_API Color ToColor(const String& source);
  56. /// Parse a Color from a C string.
  57. ATOMIC_API Color ToColor(const char* source);
  58. /// Parse an IntRect from a string.
  59. ATOMIC_API IntRect ToIntRect(const String& source);
  60. /// Parse an IntRect from a C string.
  61. ATOMIC_API IntRect ToIntRect(const char* source);
  62. /// Parse an IntVector2 from a string.
  63. ATOMIC_API IntVector2 ToIntVector2(const String& source);
  64. /// Parse an IntVector2 from a C string.
  65. ATOMIC_API IntVector2 ToIntVector2(const char* source);
  66. /// Parse an IntVector3 from a string.
  67. ATOMIC_API IntVector3 ToIntVector3(const String& source);
  68. /// Parse an IntVector3 from a C string.
  69. ATOMIC_API IntVector3 ToIntVector3(const char* source);
  70. /// Parse a Quaternion from a string. If only 3 components specified, convert Euler angles (degrees) to quaternion.
  71. ATOMIC_API Quaternion ToQuaternion(const String& source);
  72. /// Parse a Quaternion from a C string. If only 3 components specified, convert Euler angles (degrees) to quaternion.
  73. ATOMIC_API Quaternion ToQuaternion(const char* source);
  74. /// Parse a Rect from a string.
  75. ATOMIC_API Rect ToRect(const String& source);
  76. /// Parse a Rect from a C string.
  77. ATOMIC_API Rect ToRect(const char* source);
  78. /// Parse a Vector2 from a string.
  79. ATOMIC_API Vector2 ToVector2(const String& source);
  80. /// Parse a Vector2 from a C string.
  81. ATOMIC_API Vector2 ToVector2(const char* source);
  82. /// Parse a Vector3 from a string.
  83. ATOMIC_API Vector3 ToVector3(const String& source);
  84. /// Parse a Vector3 from a C string.
  85. ATOMIC_API Vector3 ToVector3(const char* source);
  86. /// Parse a Vector4 from a string.
  87. ATOMIC_API Vector4 ToVector4(const String& source, bool allowMissingCoords = false);
  88. /// Parse a Vector4 from a C string.
  89. ATOMIC_API Vector4 ToVector4(const char* source, bool allowMissingCoords = false);
  90. /// Parse a float, Vector or Matrix variant from a string. Return empty variant on illegal input.
  91. ATOMIC_API Variant ToVectorVariant(const String& source);
  92. /// Parse a float, Vector or Matrix variant from a C string. Return empty variant on illegal input.
  93. ATOMIC_API Variant ToVectorVariant(const char* source);
  94. /// Parse a Matrix3 from a string.
  95. ATOMIC_API Matrix3 ToMatrix3(const String& source);
  96. /// Parse a Matrix3 from a C string.
  97. ATOMIC_API Matrix3 ToMatrix3(const char* source);
  98. /// Parse a Matrix3x4 from a string.
  99. ATOMIC_API Matrix3x4 ToMatrix3x4(const String& source);
  100. /// Parse a Matrix3x4 from a C string.
  101. ATOMIC_API Matrix3x4 ToMatrix3x4(const char* source);
  102. /// Parse a Matrix4 from a string.
  103. ATOMIC_API Matrix4 ToMatrix4(const String& source);
  104. /// Parse a Matrix4 from a C string.
  105. ATOMIC_API Matrix4 ToMatrix4(const char* source);
  106. /// Convert a pointer to string (returns hexadecimal.)
  107. ATOMIC_API String ToString(void* value);
  108. /// Convert an unsigned integer to string as hexadecimal.
  109. ATOMIC_API String ToStringHex(unsigned value);
  110. /// Convert a byte buffer to a string.
  111. ATOMIC_API void BufferToString(String& dest, const void* data, unsigned size);
  112. /// Convert a string to a byte buffer.
  113. ATOMIC_API void StringToBuffer(PODVector<unsigned char>& dest, const String& source);
  114. /// Convert a C string to a byte buffer.
  115. ATOMIC_API void StringToBuffer(PODVector<unsigned char>& dest, const char* source);
  116. /// Return an index to a string list corresponding to the given string, or a default value if not found. The string list must be empty-terminated.
  117. ATOMIC_API unsigned GetStringListIndex(const String& value, const String* strings, unsigned defaultIndex, bool caseSensitive = false);
  118. /// Return an index to a string list corresponding to the given C string, or a default value if not found. The string list must be empty-terminated.
  119. ATOMIC_API unsigned GetStringListIndex(const char* value, const String* strings, unsigned defaultIndex, bool caseSensitive = false);
  120. /// Return an index to a C string list corresponding to the given C string, or a default value if not found. The string list must be empty-terminated.
  121. ATOMIC_API unsigned GetStringListIndex(const char* value, const char** strings, unsigned defaultIndex, bool caseSensitive = false);
  122. /// Return a formatted string.
  123. ATOMIC_API String ToString(const char* formatString, ...);
  124. /// Return whether a char is an alphabet letter.
  125. ATOMIC_API bool IsAlpha(unsigned ch);
  126. /// Return whether a char is a digit.
  127. ATOMIC_API bool IsDigit(unsigned ch);
  128. /// Return char in uppercase.
  129. ATOMIC_API unsigned ToUpper(unsigned ch);
  130. /// Return char in lowercase.
  131. ATOMIC_API unsigned ToLower(unsigned ch);
  132. /// Convert a memory size into a formatted size string, of the style "1.5 Mb".
  133. ATOMIC_API String GetFileSizeString(unsigned long long memorySize);
  134. /// Decode a base64-encoded string into buffer.
  135. ATOMIC_API PODVector<unsigned char> DecodeBase64(String encoded_string);
  136. /// Parse type from a C string.
  137. template <class T> T FromString(const char* source);
  138. template <> inline const char* FromString<const char*>(const char* source) { return source; }
  139. template <> inline String FromString<String>(const char* source) { return source; }
  140. template <> inline bool FromString<bool>(const char* source) { return ToBool(source); }
  141. template <> inline float FromString<float>(const char* source) { return ToFloat(source); }
  142. template <> inline double FromString<double>(const char* source) { return ToDouble(source); }
  143. template <> inline int FromString<int>(const char* source) { return ToInt(source); }
  144. template <> inline unsigned FromString<unsigned>(const char* source) { return ToUInt(source); }
  145. template <> inline Color FromString<Color>(const char* source) { return ToColor(source); }
  146. template <> inline IntRect FromString<IntRect>(const char* source) { return ToIntRect(source); }
  147. template <> inline IntVector2 FromString<IntVector2>(const char* source) { return ToIntVector2(source); }
  148. template <> inline IntVector3 FromString<IntVector3>(const char* source) { return ToIntVector3(source); }
  149. template <> inline Quaternion FromString<Quaternion>(const char* source) { return ToQuaternion(source); }
  150. template <> inline Rect FromString<Rect>(const char* source) { return ToRect(source); }
  151. template <> inline Vector2 FromString<Vector2>(const char* source) { return ToVector2(source); }
  152. template <> inline Vector3 FromString<Vector3>(const char* source) { return ToVector3(source); }
  153. template <> inline Vector4 FromString<Vector4>(const char* source) { return ToVector4(source); }
  154. template <> inline Variant FromString<Variant>(const char* source) { return ToVectorVariant(source); }
  155. template <> inline Matrix3 FromString<Matrix3>(const char* source) { return ToMatrix3(source); }
  156. template <> inline Matrix3x4 FromString<Matrix3x4>(const char* source) { return ToMatrix3x4(source); }
  157. template <> inline Matrix4 FromString<Matrix4>(const char* source) { return ToMatrix4(source); }
  158. /// Parse type from a string.
  159. template <class T> T FromString(const String& source) { return FromString<T>(source.CString()); }
  160. // ATOMIC BEGIN
  161. ATOMIC_API String ToStringVariadic(const char* formatString, va_list args);
  162. // ATOMIC END
  163. }