From 53164e5cbe399e52df26f831e5bd626b8ff6b344 Mon Sep 17 00:00:00 2001 From: Flakebi Date: Tue, 7 Apr 2015 18:52:50 +0200 Subject: [PATCH] Simplify string code --- src/utils/string_utils.cpp | 2 +- src/utils/string_utils.hpp | 220 +++++++------------------------------ 2 files changed, 40 insertions(+), 182 deletions(-) diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 3984e874d..dd92b8c0e 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -243,7 +243,7 @@ namespace StringUtils } else { - if (keepSplitChar) + if (keepSplitChar && start != 0) result.push_back( s.subString(start - 1, s.size()-start + 1) ); else diff --git a/src/utils/string_utils.hpp b/src/utils/string_utils.hpp index ebcc28895..0c0f7d29c 100644 --- a/src/utils/string_utils.hpp +++ b/src/utils/string_utils.hpp @@ -136,210 +136,69 @@ namespace StringUtils std::string insertValues(const std::string &s, std::vector& all_vals); - /** This no-op is useful when using variadic arguments, so that we may - * support the case with 0 variadic arguments */ - template - T1 insertValues(const T1& s) { return s; } - // ------------------------------------------------------------------------ /** Same as above but for wide-strings */ irr::core::stringw insertValues(const irr::core::stringw &s, std::vector& all_vals); - // ------------------------------------------------------------------------ - // Note: the order in which the templates are specified is important, since - // otherwise some compilers will not find the right template to use. - - template - std::string insertValues(const std::string &s, const T1 &v1, - const T2 &v2, const T3 &v3, const T4 &v4, - const T5 &v5,const T6 &v6) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v2; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v3; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v4; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v5; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v6; all_vals.push_back(dummy.str()); - return insertValues(s, all_vals); - } // insertValues(s, v1, ..., v6) - - // ------------------------------------------------------------------------ - template - std::string insertValues(const std::string &s, const T1 &v1, - const T2 &v2, const T3 &v3, const T4 &v4, - const T5 &v5) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v2; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v3; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v4; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v5; all_vals.push_back(dummy.str()); - return insertValues(s, all_vals); - } // insertValues(s, v1, ..., v5) - - // ------------------------------------------------------------------------ - template - std::string insertValues(const std::string &s, const T1 &v1, - const T2 &v2, const T3 &v3, const T4 &v4) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v2; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v3; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v4; all_vals.push_back(dummy.str()); - return insertValues(s, all_vals); - } // insertValues(s, v1, ..., v4) - - // ------------------------------------------------------------------------ - /** Shortcut insert_values taking three values, see above for - * full docs. - * \param s String in which all %s or %d are replaced. - * \param v1,v2, v3 Value(s) to replace all %s or %d with. - */ - template - std::string insertValues(const std::string &s, const T1 &v1, - const T2 &v2, const T3 &v3) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v2; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v3; all_vals.push_back(dummy.str()); - return insertValues(s, all_vals); - } // insertValues(s, v1, ..., v3) - - // ------------------------------------------------------------------------ - // Note: the order in which the templates are specified is important, since - // otherwise some compilers will not find the right template to use. - /** Shortcut insert_values taking three values, see above for - * full docs. - * \param s String in which all %s or %d are replaced. - * \param v1,v2 Value(s) to replace all %s or %d with. - */ - template - std::string insertValues(const std::string &s, const T1 &v1, - const T2 &v2) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - dummy << v2; all_vals.push_back(dummy.str()); dummy.str(""); - - return insertValues(s, all_vals); - } // insertValues(s, v1, v2) - // ------------------------------------------------------------------------ - /** Shortcut insert_values taking three values, see above for - * full docs. - * \param s String in which all %s, %d are replaced. - * \param v1 Value to replace. - */ - template - std::string insertValues(const std::string &s, const T1 &v1) - { - std::vector all_vals; - std::ostringstream dummy; - dummy << v1; all_vals.push_back(dummy.str()); dummy.str(""); - - return insertValues(s, all_vals); - } // insertValues(s, v1) + /** This no-op is useful when using variadic arguments, so that we may + * support the case with 0 variadic arguments */ + template + T insertValues(T &s) { return s; } // ------------------------------------------------------------------------ /** Intermediate struct to fill a vector using variadic templates */ - struct __FillStringwVector + struct __FillStringVector { template - static void __Fill(std::vector &all_vals, T&& v, Args &&...args) + static void __FillS(std::vector &all_vals, T&& v, Args &&...args) { - all_vals.push_back(irr::core::stringw(std::forward(v))); - __Fill(all_vals, std::forward(args)...); + std::ostringstream oss; + oss << v; + all_vals.push_back(oss.str()); + __FillS(all_vals, std::forward(args)...); } - static void __Fill(std::vector&) {} + static void __FillS(std::vector&) {} + + template + static void __FillW(std::vector &all_vals, T&& v, Args &&...args) + { + all_vals.push_back(irr::core::stringw(std::forward(v))); + __FillW(all_vals, std::forward(args)...); + } + + static void __FillW(std::vector&) {} }; + template + std::string insertValues(const std::string &s, Args ...args) + { + std::vector all_vals; + __FillStringVector::__FillS(all_vals, std::forward(args)...); + return insertValues(s, all_vals); + } + + template + std::string insertValues(const char *s, Args ...args) + { + return insertValues(std::string(s), std::forward(args)...); + } + /** Like the other ones above but for wide strings */ template irr::core::stringw insertValues(const irr::core::stringw &s, Args ...args) { std::vector all_vals; - __FillStringwVector::__Fill(all_vals, std::forward(args)...); + __FillStringVector::__FillW(all_vals, std::forward(args)...); return insertValues(s, all_vals); } - // ------------------------------------------------------------------------ - /** Like the other ones above but for wide strings */ - template - irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1, - const T2 &v2, const T3 &v3, const T4 &v4, - const T5 &v5) + template + irr::core::stringw insertValues(const wchar_t *s, Args ...args) { - irr::core::stringw s(chars); - return insertValues(s, v1, v2, v3, v4, v5); - } // insertValues(s, v1, ..., v5) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for wide strings */ - template - irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1, - const T2 &v2, const T3 &v3) - { - irr::core::stringw s(chars); - return insertValues(s, v1, v2, v3); - } // insertValues(s, v1, ..., v3) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for wide strings */ - template - irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1, - const T2 &v2) - { - irr::core::stringw s(chars); - return insertValues(s, v1, v2); - } // insertValues(s, v1, v2) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for wide strings */ - template - irr::core::stringw insertValues(const wchar_t* chars, const T1 &v1) - { - irr::core::stringw s(chars); - return insertValues(s, v1); - } // insertValues(s, v1) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for C strings */ - template - std::string insertValues(const char* chars, const T1 &v1, - const T2 &v2, const T3 &v3) - { - std::string s(chars); - return insertValues(s, v1, v2, v3); - } // insertValues(s, v1, ..., v3) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for C strings */ - template - std::string insertValues(const char* chars, const T1 &v1, - const T2 &v2) - { - std::string s(chars); - return insertValues(s, v1, v2); - } // insertValues(s, v1, v2) - - // ------------------------------------------------------------------------ - /** Like the other ones above but for C strings */ - template - std::string insertValues(const char* chars, const T1 &v1) - { - std::string s(chars); - return insertValues(s, v1); - } // insertValues(s, v1) + return insertValues(irr::core::stringw(s), std::forward(args)...); + } // ------------------------------------------------------------------------ template @@ -367,4 +226,3 @@ namespace StringUtils #endif -/* EOF */