diff --git a/include/Netra.hpp b/include/Netra.hpp index 021c71e..997ccbd 100644 --- a/include/Netra.hpp +++ b/include/Netra.hpp @@ -292,8 +292,9 @@ namespace QCL // 屏蔽所有信号 void blockAllSignals(); - - // 去除字符串的左空格 + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // 字符串操作 + // 去除字符串的左空格 std::string Ltrim(const std::string &s); // 去除字符串右侧的空格 @@ -302,4 +303,68 @@ namespace QCL // 去除字符串左右两侧的空格 std::string LRtrim(const std::string &s); + // 通用类型转字符串 + template + std::string to_string_any(const T &value) + { + std::ostringstream oss; + oss << value; + return oss.str(); + } + + // 递归获取 tuple 中 index 对应参数 + template + std::string get_tuple_arg(const Tuple &tup, std::size_t index) + { + if constexpr (I < std::tuple_size_v) + { + if (I == index) + return to_string_any(std::get(tup)); + else + return get_tuple_arg(tup, index); + } + else + { + throw std::runtime_error("Too few arguments for format string"); + } + } + + // format 函数 + template + std::string format(const std::string &fmt, const Args &...args) + { + std::ostringstream oss; + std::tuple tup(args...); + size_t pos = 0; + size_t arg_idx = 0; + + while (pos < fmt.size()) + { + if (fmt[pos] == '{' && pos + 1 < fmt.size() && fmt[pos + 1] == '{') + { + oss << '{'; + pos += 2; + } + else if (fmt[pos] == '}' && pos + 1 < fmt.size() && fmt[pos + 1] == '}') + { + oss << '}'; + pos += 2; + } + else if (fmt[pos] == '{' && pos + 1 < fmt.size() && fmt[pos + 1] == '}') + { + oss << get_tuple_arg(tup, arg_idx++); + pos += 2; + } + else + { + oss << fmt[pos++]; + } + } + + if (arg_idx < sizeof...(Args)) + throw std::runtime_error("Too many arguments for format string"); + + return oss.str(); + } + } diff --git a/src/Netra.cpp b/src/Netra.cpp index 0f04a70..38236e8 100644 --- a/src/Netra.cpp +++ b/src/Netra.cpp @@ -546,5 +546,8 @@ namespace QCL { return Ltrim(Rtrim(s)); } + + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } \ No newline at end of file