StringTools.h 1.7 KB

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