StringTools.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Create a formatted text string for debugging purposes.
  6. /// Note that this function has an internal buffer of 1024 characters, so long strings will be trimmed.
  7. string StringFormat(const char *inFMT, ...);
  8. /// Convert type to string
  9. template<typename T>
  10. string ConvertToString(const T &inValue)
  11. {
  12. ostringstream oss;
  13. oss << inValue;
  14. return oss.str();
  15. }
  16. /// Calculate the FNV-1a hash of inString.
  17. /// @see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
  18. constexpr uint64 HashString(const char *inString)
  19. {
  20. uint64 hash = 14695981039346656037UL;
  21. for (const char *c = inString; *c != 0; ++c)
  22. {
  23. hash ^= *c;
  24. hash = hash * 1099511628211UL;
  25. }
  26. return hash;
  27. }
  28. /// Replace substring with other string
  29. void StringReplace(string &ioString, const string_view &inSearch, const string_view &inReplace);
  30. /// Convert a delimited string to an array of strings
  31. void StringToVector(const string_view &inString, vector<string> &outVector, const string_view &inDelimiter = ",", bool inClearVector = true);
  32. /// Convert an array strings to a delimited string
  33. void VectorToString(const vector<string> &inVector, string &outString, const string_view &inDelimiter = ",");
  34. /// Convert a string to lower case
  35. string ToLower(const string_view &inString);
  36. /// Converts the lower 4 bits of inNibble to a string that represents the number in binary format
  37. const char *NibbleToBinary(uint32 inNibble);
  38. JPH_NAMESPACE_END