string_utils.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <string_view>
  5. #include <algorithm>
  6. #include <sstream>
  7. #include <regex>
  8. #include <unordered_map>
  9. #include <cstdint>
  10. #include <iomanip>
  11. namespace smartbotic::common {
  12. class StringUtils {
  13. public:
  14. // Trim whitespace
  15. static std::string trim(std::string_view str);
  16. static std::string trimLeft(std::string_view str);
  17. static std::string trimRight(std::string_view str);
  18. // Case conversion
  19. static std::string toLower(std::string_view str);
  20. static std::string toUpper(std::string_view str);
  21. // Split and join
  22. static std::vector<std::string> split(std::string_view str, char delimiter);
  23. static std::vector<std::string> split(std::string_view str, std::string_view delimiter);
  24. static std::string join(const std::vector<std::string>& parts, std::string_view delimiter);
  25. // String operations
  26. static bool startsWith(std::string_view str, std::string_view prefix);
  27. static bool endsWith(std::string_view str, std::string_view suffix);
  28. static bool contains(std::string_view str, std::string_view substr);
  29. // Replace
  30. static std::string replace(std::string_view str, std::string_view from, std::string_view to);
  31. static std::string replaceAll(std::string_view str, std::string_view from, std::string_view to);
  32. // Template interpolation: replaces ${VAR} with values from map
  33. static std::string interpolate(std::string_view tmpl,
  34. const std::unordered_map<std::string, std::string>& vars);
  35. // Environment variable expansion: replaces ${VAR:default} with env value or default
  36. static std::string expandEnvVars(std::string_view str);
  37. // URL encoding/decoding
  38. static std::string urlEncode(std::string_view str);
  39. static std::string urlDecode(std::string_view str);
  40. // Base64 encoding/decoding
  41. static std::string base64Encode(std::string_view data);
  42. static std::string base64Decode(std::string_view encoded);
  43. // Hex encoding/decoding
  44. static std::string hexEncode(const std::vector<uint8_t>& data);
  45. static std::vector<uint8_t> hexDecode(std::string_view hex);
  46. /// Hex SHA-256 of the given bytes.
  47. ///
  48. /// A binary object's `checksum` is the hash of its base64 text, not of the
  49. /// raw bytes - `http-request.js:457` does `sha256(base64Data)`. An upload
  50. /// and a download of the same image must produce the same checksum or
  51. /// deduplication downstream silently stops working, so hash the same input.
  52. static std::string sha256Hex(std::string_view data);
  53. };
  54. } // namespace smartbotic::common