2
0

StringTools.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. using OStringStream = std::basic_ostringstream<char, std::char_traits<char>, STLAllocator<char>>;
  13. OStringStream oss;
  14. oss << inValue;
  15. return oss.str();
  16. }
  17. /// Calculate the FNV-1a hash of inString.
  18. /// @see https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
  19. constexpr uint64 HashString(const char *inString)
  20. {
  21. uint64 hash = 14695981039346656037UL;
  22. for (const char *c = inString; *c != 0; ++c)
  23. {
  24. hash ^= *c;
  25. hash = hash * 1099511628211UL;
  26. }
  27. return hash;
  28. }
  29. /// Replace substring with other string
  30. void StringReplace(String &ioString, const string_view &inSearch, const string_view &inReplace);
  31. /// Convert a delimited string to an array of strings
  32. void StringToVector(const string_view &inString, Array<String> &outVector, const string_view &inDelimiter = ",", bool inClearVector = true);
  33. /// Convert an array strings to a delimited string
  34. void VectorToString(const Array<String> &inVector, String &outString, const string_view &inDelimiter = ",");
  35. /// Convert a string to lower case
  36. String ToLower(const string_view &inString);
  37. /// Converts the lower 4 bits of inNibble to a string that represents the number in binary format
  38. const char *NibbleToBinary(uint32 inNibble);
  39. JPH_NAMESPACE_END