89 lines
3.2 KiB
C++
89 lines
3.2 KiB
C++
#ifndef UTILS_H
|
||
#define UTILS_H
|
||
|
||
#include <string>
|
||
#include <chrono>
|
||
#include "nlohmann/json.hpp"
|
||
|
||
// // ǰ<><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> httplib <20><> Response <20><>
|
||
// namespace httplib {
|
||
// class Response;
|
||
// }
|
||
|
||
using json = nlohmann::json;
|
||
|
||
namespace utils
|
||
{
|
||
// ʱ<><CAB1><EFBFBD><EFBFBD>غ<EFBFBD><D8BA><EFBFBD>
|
||
std::string get_current_time();
|
||
std::string get_current_timestamp();
|
||
|
||
// JSON <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
||
json create_error_response(int code, const std::string &message, const std::string &details = "");
|
||
json create_success_response(const json &data = {}, const std::string &message = "Success");
|
||
|
||
// UTF-8 <20><>֤
|
||
bool is_valid_utf8(const std::string &str);
|
||
std::string sanitize_utf8(const std::string &str);
|
||
|
||
// API <20><>Ӧ<EFBFBD><D3A6>ʽ
|
||
json create_api_response(bool success, int code, const std::string &msg,
|
||
const std::string &req_code = "", const std::string &req_from = "",
|
||
const std::string &req_cmd = "", const json &res_data = json::object());
|
||
|
||
// Base64 <20><><EFBFBD><EFBFBD><EFBFBD>URDF<44><46><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
std::string base64_decode(const std::string &encoded_string);
|
||
std::string base64_to_urdf(const std::string &base64_urdf);
|
||
bool save_base64_urdf_to_file(const std::string &base64_urdf, const std::string &filename);
|
||
bool validate_urdf_base64(const std::string &base64_urdf);
|
||
bool save_urdf_string_to_file(const std::string &urdf_content, const std::string &filename);
|
||
|
||
} // namespace utils
|
||
class StringUtils
|
||
{
|
||
public:
|
||
/**
|
||
* @brief 将包含一个分隔符的字符串拆分成两部分
|
||
* @param str 要拆分的字符串
|
||
* @param delimiter 分隔符,默认为'_'
|
||
* @return std::pair<std::string, std::string> 包含两个部分的pair
|
||
* 第一个元素是分隔符前的部分,第二个是分隔符后的部分
|
||
* 如果未找到分隔符,第二个元素为空字符串
|
||
*/
|
||
static std::pair<std::string, std::string> splitByDelimiter(
|
||
const std::string &str,
|
||
char delimiter = '_');
|
||
|
||
/**
|
||
* @brief 专门拆分"A_B"格式的字符串
|
||
* @param str 要拆分的字符串
|
||
* @return std::pair<std::string, std::string> 包含两个部分的pair
|
||
*/
|
||
static std::pair<std::string, std::string> splitA_B(const std::string &str);
|
||
|
||
/**
|
||
* @brief 将字符串按分隔符拆分成两个部分,支持引用参数返回
|
||
* @param str 要拆分的字符串
|
||
* @param part1 返回第一部分
|
||
* @param part2 返回第二部分
|
||
* @param delimiter 分隔符,默认为'_'
|
||
* @return bool 如果找到分隔符返回true,否则返回false
|
||
*/
|
||
static bool splitToTwoParts(
|
||
const std::string &str,
|
||
std::string &part1,
|
||
std::string &part2,
|
||
char delimiter = '_');
|
||
|
||
/**
|
||
* @brief 严格拆分,必须包含且只包含一个分隔符
|
||
* @param str 要拆分的字符串
|
||
* @param delimiter 分隔符,默认为'_'
|
||
* @return std::pair<std::string, std::string> 包含两个部分的pair
|
||
* @throws std::invalid_argument 如果没有找到分隔符或找到多个分隔符
|
||
*/
|
||
static std::pair<std::string, std::string> splitStrict(
|
||
const std::string &str,
|
||
char delimiter = '_');
|
||
};
|
||
#endif // UTILS_H
|