From cb93a5ce1552c1fe6c5454e8228a07facbc6e9d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Wed, 5 Sep 2018 15:19:51 +0900 Subject: [PATCH] [enco] Rewrite constant methods using concat (#1337) Currently, constant methods in Global internally implements string concat operation. This commit revises these methods to reuse existing concat method. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/CppGen/Global.cpp | 63 +++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/contrib/enco/core/src/CppGen/Global.cpp b/contrib/enco/core/src/CppGen/Global.cpp index 5385899..889eb4f 100644 --- a/contrib/enco/core/src/CppGen/Global.cpp +++ b/contrib/enco/core/src/CppGen/Global.cpp @@ -1,7 +1,49 @@ #include "Global.h" +#include "String.h" + #include +namespace +{ + +template std::string concat(const std::string &sep, Container &&c) +{ + std::stringstream ss; + enco::concat(ss, sep, c.begin(), c.end()); + return ss.str(); +} + +template +auto transform(Callable &&cb, InputIt beg, InputIt end) -> std::vector +{ + using U = decltype(cb(*beg)); + std::vector res; + + for (auto it = beg; it != end; ++it) + { + res.emplace_back(cb(*it)); + } + + return res; +} + +std::string u32_to_dec(uint32_t value) +{ + std::stringstream ss; + ss << value; + return ss.str(); +} + +std::string u08_to_hex(uint8_t value) +{ + std::stringstream ss; + ss << "0x" << std::hex << value + 0; + return ss.str(); +} + +} // namespace + namespace enco { @@ -21,14 +63,7 @@ template <> std::string Global::constant(const std::vector &values) std::stringstream ss; ss << "const uint32_t " << name << "[" << values.size() << "] = { "; - if (values.size() > 0) - { - ss << values.at(0); - for (uint32_t n = 1; n < values.size(); ++n) - { - ss << ", " << values.at(n); - } - } + ss << ::concat(", ", transform(u32_to_dec, values.begin(), values.end())); ss << " };"; _content.append(ss.str()); @@ -43,17 +78,7 @@ std::string Global::constant(const uint8_t *base, uint32_t size) std::stringstream ss; ss << "const uint8_t " << name << "[" << size << "] = { "; - - if (size > 0) - { - ss << "0x" << std::hex << (base[0] + 0); - - for (uint32_t n = 1; n < size; ++n) - { - ss << ", 0x" << std::hex << (base[n] + 0); - } - } - + ss << ::concat(", ", transform(u08_to_hex, base, base + size)); ss << " };"; _content.append(ss.str()); -- 2.7.4