]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Implement iterator for wordlist (#1729)
authorFrancesco Chemolli <5175948+kinkie@users.noreply.github.com>
Tue, 12 Mar 2024 15:08:26 +0000 (15:08 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Tue, 12 Mar 2024 17:08:35 +0000 (17:08 +0000)
Also removed ToSBufList(): Unused since inception (2013 commit 6a17a36).

src/wordlist.cc
src/wordlist.h

index ce98e8047f661ff81c4900abb55e5847a8993c57..4be7aeffb59d7a46f5a24e6823f59480d8412bbc 100644 (file)
@@ -34,21 +34,8 @@ wordlistAdd(wordlist ** list, const char *key)
 void
 wordlistCat(const wordlist * w, MemBuf * mb)
 {
-    while (nullptr != w) {
-        mb->appendf("%s\n", w->key);
-        w = w->next;
-    }
-}
-
-SBufList
-ToSBufList(wordlist *wl)
-{
-    SBufList rv;
-    while (wl != nullptr) {
-        rv.push_back(SBuf(wl->key));
-        wl = wl->next;
-    }
-    return rv;
+    for (const auto &word: *w)
+        mb->appendf("%s\n", word);
 }
 
 char *
index 1114296cb3ad85c2d40b5955b032660a22e044ac..c87d97a9653e6e2a33c97d40ced35a3e30536674 100644 (file)
 #include "globals.h"
 #include "sbuf/List.h"
 
+#include <iterator>
+
+class wordlist;
+
+/// minimal iterator for read-only traversal of wordlist objects
+class WordlistIterator
+{
+public:
+    using value_type = const char *;
+
+    explicit WordlistIterator(const wordlist * const wl): w(wl) {}
+
+    auto operator ==(const WordlistIterator &rhs) const { return this->w == rhs.w; }
+    auto operator !=(const WordlistIterator &rhs) const { return this->w != rhs.w; }
+
+    inline value_type operator *() const;
+    inline WordlistIterator &operator++();
+
+private:
+    const wordlist *w;
+};
+
 /** A list of C-strings
  *
  * \deprecated use SBufList instead
@@ -22,6 +44,8 @@ class wordlist
     friend char *wordlistChopHead(wordlist **);
 
 public:
+    using const_iterator = WordlistIterator;
+
     wordlist() : key(nullptr), next(nullptr) {}
     // create a new wordlist node, with a copy of k as key
     explicit wordlist(const char *k) : key(xstrdup(k)), next(nullptr) {}
@@ -29,6 +53,9 @@ public:
     wordlist(const wordlist &) = delete;
     wordlist &operator=(const wordlist &) = delete;
 
+    auto begin() const { return const_iterator(this); }
+    auto end() const { return const_iterator(nullptr); }
+
     char *key;
     wordlist *next;
 
@@ -62,8 +89,18 @@ void wordlistDestroy(wordlist **);
  */
 char *wordlistChopHead(wordlist **);
 
-/// convert a wordlist to a SBufList
-SBufList ToSBufList(wordlist *);
+inline WordlistIterator &
+WordlistIterator::operator++()
+{
+    w = w->next;
+    return *this;
+}
+
+inline WordlistIterator::value_type
+WordlistIterator::operator*() const
+{
+    return w->key;
+}
 
 #endif /* SQUID_SRC_WORDLIST_H */