diff options
author | 2017-03-30 15:23:23 +1100 | |
---|---|---|
committer | 2017-03-30 15:51:14 +1100 | |
commit | 5e8fefbb1214e9936253a81da4946cc42cc20b9d (patch) | |
tree | 30d297c7eb05c68326488bf1618505a8d00bd135 /src/lib/netlist/plib/pstring.cpp | |
parent | 35b673ea45da6b4759f170813f99c124c7db9d70 (diff) |
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.
Diffstat (limited to 'src/lib/netlist/plib/pstring.cpp')
-rw-r--r-- | src/lib/netlist/plib/pstring.cpp | 26 |
1 files changed, 13 insertions, 13 deletions
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<F>::pcopy(const mem_t *from, std::size_t size) } template<typename F> -const pstring_t<F> pstring_t<F>::substr(const iterator &start, const iterator &end) const +const pstring_t<F> pstring_t<F>::substr(const const_iterator &start, const const_iterator &end) const { pstring_t ret; //FIXME: throw ? @@ -116,7 +116,7 @@ template<typename F> const pstring_t<F> pstring_t<F>::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<F> pstring_t<F>::ucase() const } template<typename F> -typename pstring_t<F>::iterator pstring_t<F>::find_first_not_of(const pstring_t &no) const +typename pstring_t<F>::const_iterator pstring_t<F>::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<F>::iterator pstring_t<F>::find_first_not_of(const pstring_t } template<typename F> -typename pstring_t<F>::iterator pstring_t<F>::find_last_not_of(const pstring_t &no) const +typename pstring_t<F>::const_iterator pstring_t<F>::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<F>::iterator pstring_t<F>::find_last_not_of(const pstring_t & } template<typename F> -typename pstring_t<F>::iterator pstring_t<F>::find(const pstring_t &search, iterator start) const +typename pstring_t<F>::const_iterator pstring_t<F>::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<F>::iterator pstring_t<F>::find(const pstring_t &search, iter } template<typename F> -typename pstring_t<F>::iterator pstring_t<F>::find(const code_t search, iterator start) const +typename pstring_t<F>::const_iterator pstring_t<F>::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<F> pstring_t<F>::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<F> pstring_t<F>::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<typename F> |