diff --git a/ChangeLog b/ChangeLog index 4aef2ed0..0de8346d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2009-06-06 Tatsuhiro Tsujikawa + + Added strjoin function template. Use it in pathJoin() + * src/Util.h + * src/a2functional.h + 2009-06-06 Tatsuhiro Tsujikawa Avoid intermediate object during string concatenation. Replaced diff --git a/src/Util.h b/src/Util.h index 6adac2df..fd5da150 100644 --- a/src/Util.h +++ b/src/Util.h @@ -299,11 +299,7 @@ public: elements.push_back(*first); } } - if(elements.empty()) { - return ""; - } - return std::accumulate(elements.begin()+1, elements.end(), elements[0], - Concat("/")); + return strjoin(elements.begin(), elements.end(), "/"); } // Parses INDEX=PATH format string. INDEX must be an unsigned diff --git a/src/a2functional.h b/src/a2functional.h index 1ed75509..9809b36a 100644 --- a/src/a2functional.h +++ b/src/a2functional.h @@ -165,18 +165,6 @@ public: } }; -class Concat { -private: - std::string _delim; -public: - Concat(const std::string& delim = A2STR::NIL):_delim(delim) {} - - std::string operator()(const std::string& s1, const std::string& s2) const - { - return s1+_delim+s2; - } -}; - class Deleter { public: template @@ -216,6 +204,23 @@ public: } }; +template +std::string strjoin(InputIterator first, InputIterator last, + const DelimiterType& delim) +{ + std::string result; + if(first == last) { + return result; + } + InputIterator beforeLast = last-1; + for(; first != beforeLast; ++first) { + result += *first; + result += delim; + } + result += *beforeLast; + return result; +} + template inline std::string strconcat(const T1& a1, const T2& a2) {