StringUtils.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef COMMON_STRINGUTILS_H
  24. #define COMMON_STRINGUTILS_H
  25. #include "Color.h"
  26. #include "Quaternion.h"
  27. #include "Rect.h"
  28. #include "StringHash.h"
  29. #include "Vector4.h"
  30. #include <string>
  31. #include <vector>
  32. //! Split string with a separator char
  33. std::vector<std::string> split(const std::string& source, char separator);
  34. //! Replace characters in a string
  35. std::string replace(const std::string& source, char replaceThis, char replaceWith);
  36. //! Replace substrings in a string
  37. std::string replace(const std::string& source, const std::string& replaceThis, const std::string& replaceWith);
  38. //! Convert a string to uppercase
  39. std::string toUpper(const std::string& source);
  40. //! Convert a string to lowercase
  41. std::string toLower(const std::string& source);
  42. //! Replace characters in a string by modifying the original string
  43. void replaceInPlace(std::string& str, char replaceThis, char replaceWith);
  44. //! Replace substrings in a string by modifying the original string
  45. void replaceInPlace(std::string& str, const std::string& replaceThis, const std::string& replaceWith);
  46. //! Convert a string to uppercase by modifying the original string
  47. void toUpperInPlace(std::string& str);
  48. //! Convert a string to lowercase by modifying the original string
  49. void toLowerInPlace(std::string& str);
  50. //! Parse a bool from a string. Check for the substring "true" case-insensitively
  51. bool toBool(const std::string& source);
  52. //! Parse a float from a string
  53. float toFloat(const std::string& source);
  54. //! Parse an integer from a string
  55. int toInt(const std::string& source);
  56. //! Parse an unsigned integer from a string
  57. unsigned toUInt(const std::string& source);
  58. //! Parse a Color from a string
  59. Color toColor(const std::string& source);
  60. //! Parse an IntRect from a string
  61. IntRect toIntRect(const std::string& source);
  62. //! Parse an IntVector2 from a string
  63. IntVector2 toIntVector2(const std::string& source);
  64. //! Parse a Quaternion from a string. If only 3 components specified, convert Euler angles (degrees) to quaternion
  65. Quaternion toQuaternion(const std::string& source);
  66. //! Parse a Rect from a string
  67. Rect toRect(const std::string& source);
  68. //! Parse a Vector2 from a string
  69. Vector2 toVector2(const std::string& source);
  70. //! Parse a Vector3 from a string
  71. Vector3 toVector3(const std::string& source);
  72. //! Parse a Vector4 from a string
  73. Vector4 toVector4(const std::string& source, bool allowMissingCoords = false);
  74. //! Convert a bool to string
  75. std::string toString(bool value);
  76. //! Convert a float to string
  77. std::string toString(float value);
  78. //! Convert an integer to string
  79. std::string toString(int value);
  80. //! Convert an unsigned integer to string
  81. std::string toString(unsigned value);
  82. //! Convert a Color to string
  83. std::string toString(const Color& value);
  84. //! Convert an IntRect to string
  85. std::string toString(const IntRect& value);
  86. //! Convert an IntVector2 to string
  87. std::string toString(const IntVector2& value);
  88. //! Convert a Quaternion to string
  89. std::string toString(const Quaternion& value);
  90. //! Convert a Rect to string
  91. std::string toString(const Rect& value);
  92. //! Convert a StringHash to string. Return the reverse mapping if found
  93. std::string toString(const StringHash& value);
  94. //! Convert a ShortStringHash to string. Return the reverse mapping if found
  95. std::string toString(const ShortStringHash& value);
  96. //! Convert a Vector2 to string
  97. std::string toString(const Vector2& value);
  98. //! Convert a Vector3 to string
  99. std::string toString(const Vector3& value);
  100. //! Convert a Vector4 to string
  101. std::string toString(const Vector4& value);
  102. //! Convert an integer to string as hexadecimal
  103. std::string toStringHex(int value);
  104. //! Return an index to a string list corresponding to the given string, or a default value if not found
  105. unsigned getIndexFromStringList(const std::string& value, const std::string* strings, unsigned count, unsigned defaultIndex,
  106. bool caseSensitive = false);
  107. #endif // COMMON_STRINGUTILS_H