| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #pragma once
- #include <string>
- #include <vector>
- #include <string_view>
- #include <algorithm>
- #include <sstream>
- #include <regex>
- #include <unordered_map>
- #include <cstdint>
- #include <iomanip>
- namespace smartbotic::common {
- class StringUtils {
- public:
- // Trim whitespace
- static std::string trim(std::string_view str);
- static std::string trimLeft(std::string_view str);
- static std::string trimRight(std::string_view str);
- // Case conversion
- static std::string toLower(std::string_view str);
- static std::string toUpper(std::string_view str);
- // Split and join
- static std::vector<std::string> split(std::string_view str, char delimiter);
- static std::vector<std::string> split(std::string_view str, std::string_view delimiter);
- static std::string join(const std::vector<std::string>& parts, std::string_view delimiter);
- // String operations
- static bool startsWith(std::string_view str, std::string_view prefix);
- static bool endsWith(std::string_view str, std::string_view suffix);
- static bool contains(std::string_view str, std::string_view substr);
- // Replace
- static std::string replace(std::string_view str, std::string_view from, std::string_view to);
- static std::string replaceAll(std::string_view str, std::string_view from, std::string_view to);
- // Template interpolation: replaces ${VAR} with values from map
- static std::string interpolate(std::string_view tmpl,
- const std::unordered_map<std::string, std::string>& vars);
- // Environment variable expansion: replaces ${VAR:default} with env value or default
- static std::string expandEnvVars(std::string_view str);
- // URL encoding/decoding
- static std::string urlEncode(std::string_view str);
- static std::string urlDecode(std::string_view str);
- // Base64 encoding/decoding
- static std::string base64Encode(std::string_view data);
- static std::string base64Decode(std::string_view encoded);
- // Hex encoding/decoding
- static std::string hexEncode(const std::vector<uint8_t>& data);
- static std::vector<uint8_t> hexDecode(std::string_view hex);
- /// Hex SHA-256 of the given bytes.
- ///
- /// A binary object's `checksum` is the hash of its base64 text, not of the
- /// raw bytes - `http-request.js:457` does `sha256(base64Data)`. An upload
- /// and a download of the same image must produce the same checksum or
- /// deduplication downstream silently stops working, so hash the same input.
- static std::string sha256Hex(std::string_view data);
- };
- } // namespace smartbotic::common
|