StringTools.h 1.3 KB

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