StringUtils.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Color.h"
  25. #include "Quaternion.h"
  26. #include "Rect.h"
  27. #include "StringHash.h"
  28. #include "Vector4.h"
  29. #include <vector>
  30. /// Split string with a separator char
  31. std::vector<std::string> Split(const std::string& source, char separator);
  32. /// Replace characters in a string
  33. std::string Replace(const std::string& source, char replaceThis, char replaceWith);
  34. /// Replace substrings in a string
  35. std::string Replace(const std::string& source, const std::string& replaceThis, const std::string& replaceWith);
  36. /// Convert a string to uppercase
  37. std::string ToUpper(const std::string& source);
  38. /// Convert a string to lowercase
  39. std::string ToLower(const std::string& source);
  40. /// Replace characters in a string by modifying the original string
  41. void ReplaceInPlace(std::string& str, char replaceThis, char replaceWith);
  42. /// Replace substrings in a string by modifying the original string
  43. void ReplaceInPlace(std::string& str, const std::string& replaceThis, const std::string& replaceWith);
  44. /// Convert a string to uppercase by modifying the original string
  45. void ToUpperInPlace(std::string& str);
  46. /// Convert a string to lowercase by modifying the original string
  47. void ToLowerInPlace(std::string& str);
  48. /// Parse a bool from a string. Check for the substring "true" case-insensitively
  49. bool ToBool(const std::string& source);
  50. /// Parse a float from a string
  51. float ToFloat(const std::string& source);
  52. /// Parse an integer from a string
  53. int ToInt(const std::string& source);
  54. /// Parse an unsigned integer from a string
  55. unsigned ToUInt(const std::string& source);
  56. /// Parse a Color from a string
  57. Color ToColor(const std::string& source);
  58. /// Parse an IntRect from a string
  59. IntRect ToIntRect(const std::string& source);
  60. /// Parse an IntVector2 from a string
  61. IntVector2 ToIntVector2(const std::string& source);
  62. /// Parse a Quaternion from a string. If only 3 components specified, convert Euler angles (degrees) to quaternion
  63. Quaternion ToQuaternion(const std::string& source);
  64. /// Parse a Rect from a string
  65. Rect ToRect(const std::string& source);
  66. /// Parse a Vector2 from a string
  67. Vector2 ToVector2(const std::string& source);
  68. /// Parse a Vector3 from a string
  69. Vector3 ToVector3(const std::string& source);
  70. /// Parse a Vector4 from a string
  71. Vector4 ToVector4(const std::string& source, bool allowMissingCoords = false);
  72. /// Convert a bool to string
  73. std::string ToString(bool value);
  74. /// Convert a float to string
  75. std::string ToString(float value);
  76. /// Convert an integer to string
  77. std::string ToString(int value);
  78. /// Convert an unsigned integer to string
  79. std::string ToString(unsigned value);
  80. /// Convert a Color to string
  81. std::string ToString(const Color& value);
  82. /// Convert an IntRect to string
  83. std::string ToString(const IntRect& value);
  84. /// Convert an IntVector2 to string
  85. std::string ToString(const IntVector2& value);
  86. /// Convert a Quaternion to string
  87. std::string ToString(const Quaternion& value);
  88. /// Convert a Rect to string
  89. std::string ToString(const Rect& value);
  90. /// Convert a StringHash to string. Return the reverse mapping if found
  91. std::string ToString(const StringHash& value);
  92. /// Convert a ShortStringHash to string. Return the reverse mapping if found
  93. std::string ToString(const ShortStringHash& value);
  94. /// Convert a Vector2 to string
  95. std::string ToString(const Vector2& value);
  96. /// Convert a Vector3 to string
  97. std::string ToString(const Vector3& value);
  98. /// Convert a Vector4 to string
  99. std::string ToString(const Vector4& value);
  100. /// Convert a pointer to string (returns hexadecimal)
  101. std::string ToString(void* value);
  102. /// Convert an unsigned integer to string as hexadecimal
  103. std::string ToStringHex(unsigned value);
  104. /// Return an index to a string list corresponding to the given string, or a default value if not found
  105. unsigned GetStringListIndex(const std::string& value, const std::string* strings, unsigned count, unsigned defaultIndex,
  106. bool caseSensitive = false);