From 5e8fefbb1214e9936253a81da4946cc42cc20b9d Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Thu, 30 Mar 2017 15:23:23 +1100 Subject: Turn psring iterator into a real forward iterator that works with standard algorithms. There are a few changes to achieve this: * Rename to const_iterator since it's immutable * Typedef iterator to const_iterator for now as there's no mutable iterator * Add default constrcutor and operator-> required by concept, const-qualify operators * Remove operator+ and operator+= since it's not a random-access iterator (use std::next and std::advance instead) * Return reference/pointer to a proxy rather than a code_t value from opertator*/operator-> The final change is required to meet the requirement that operator* for two equivalent forward iterators return an equivalent reference. The pstring doesn't actually contain a sequence of code_t, so there's no way to return a reference to code_t directly. Instead, a reference to a proxy object aliased on the string storage is returned. The proxy is implicitly convertible to code_t. The most noticeable side effect is that auto c = *s.begin() or for (auto c : s) won't work. You need to do for (auto &c : s) or for (code_t c : s) instead. --- src/lib/netlist/plib/pstring.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/lib/netlist/plib/pstring.cpp') diff --git a/src/lib/netlist/plib/pstring.cpp b/src/lib/netlist/plib/pstring.cpp index 5396f333358..90dc1f82f1d 100644 --- a/src/lib/netlist/plib/pstring.cpp +++ b/src/lib/netlist/plib/pstring.cpp @@ -104,7 +104,7 @@ void pstring_t::pcopy(const mem_t *from, std::size_t size) } template -const pstring_t pstring_t::substr(const iterator &start, const iterator &end) const +const pstring_t pstring_t::substr(const const_iterator &start, const const_iterator &end) const { pstring_t ret; //FIXME: throw ? @@ -116,7 +116,7 @@ template const pstring_t pstring_t::ucase() const { pstring_t ret = ""; - for (auto c : *this) + for (code_t c : *this) if (c >= 'a' && c <= 'z') ret += (c - 'a' + 'A'); else @@ -125,12 +125,12 @@ const pstring_t pstring_t::ucase() const } template -typename pstring_t::iterator pstring_t::find_first_not_of(const pstring_t &no) const +typename pstring_t::const_iterator pstring_t::find_first_not_of(const pstring_t &no) const { for (auto it = begin(); it != end(); ++it) { bool f = true; - for (auto const jt : no) + for (code_t const jt : no) { if (*it == jt) { @@ -145,14 +145,14 @@ typename pstring_t::iterator pstring_t::find_first_not_of(const pstring_t } template -typename pstring_t::iterator pstring_t::find_last_not_of(const pstring_t &no) const +typename pstring_t::const_iterator pstring_t::find_last_not_of(const pstring_t &no) const { - /* FIXME: reverse iterator */ - iterator last_found = end(); + /* FIXME: reverse const_iterator */ + const_iterator last_found = end(); for (auto it = begin(); it != end(); ++it) { bool f = true; - for (auto const jt : no) + for (code_t const jt : no) { if (*it == jt) { @@ -167,11 +167,11 @@ typename pstring_t::iterator pstring_t::find_last_not_of(const pstring_t & } template -typename pstring_t::iterator pstring_t::find(const pstring_t &search, iterator start) const +typename pstring_t::const_iterator pstring_t::find(const pstring_t &search, const_iterator start) const { for (; start != end(); ++start) { - iterator itc(start); + const_iterator itc(start); auto cmp = search.begin(); while (itc != end() && cmp != search.end() && *itc == *cmp) { @@ -185,7 +185,7 @@ typename pstring_t::iterator pstring_t::find(const pstring_t &search, iter } template -typename pstring_t::iterator pstring_t::find(const code_t search, iterator start) const +typename pstring_t::const_iterator pstring_t::find(const code_t search, const_iterator start) const { mem_t buf[traits::MAXCODELEN+1] = { 0 }; traits::encode(search, buf); @@ -205,7 +205,7 @@ pstring_t pstring_t::replace(const pstring_t &search, const pstring_t &rep { ret += substr(last_s, s); ret += replace; - last_s = s + slen; + last_s = std::next(s, slen); s = find(search, last_s); } ret += substr(last_s, end()); @@ -225,7 +225,7 @@ const pstring_t pstring_t::rtrim(const pstring_t &ws) const if (f==end()) return pstring_t(""); else - return substr(begin(), f + 1); + return substr(begin(), std::next(f, 1)); } template -- cgit v1.2.3-70-g09d2