summaryrefslogtreecommitdiff
path: root/chromium/components/subresource_filter/core/common/string_splitter.h
blob: f3b4e85b06b41b476a923462bb85f195c19bd8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_
#define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_

#include <iterator>

#include "base/logging.h"
#include "base/strings/string_piece.h"

namespace subresource_filter {

// A zero-allocation string splitter. Splits a string into non-empty tokens
// divided by separator characters as defined by the IsSeparator predicate.
// However, instead of materializing and returning a collection of all tokens in
// the string, it provides an InputIterator that can be used to extract the
// tokens.
//
// TODO(pkalinnikov): Move it to "base/strings" after some generalization.
template <typename IsSeparator>
class StringSplitter {
 public:
  class Iterator
      : public std::iterator<std::input_iterator_tag, base::StringPiece> {
   public:
    // Creates an iterator, which points to the leftmost token within the
    // |splitter|'s |text|, starting from |head|.
    Iterator(const StringSplitter& splitter,
             base::StringPiece::const_iterator head)
        : splitter_(&splitter), current_(head, 0), end_(splitter.text_.end()) {
      DCHECK_GE(head, splitter_->text_.begin());
      DCHECK_LE(head, end_);

      Advance();
    }

    bool operator==(const Iterator& rhs) const {
      return current_.begin() == rhs.current_.begin();
    }

    bool operator!=(const Iterator& rhs) const { return !operator==(rhs); }

    base::StringPiece operator*() const { return current_; }
    const base::StringPiece* operator->() const { return &current_; }

    Iterator& operator++() {
      Advance();
      return *this;
    }

    Iterator operator++(int) {
      Iterator copy(*this);
      operator++();
      return copy;
    }

   private:
    void Advance() {
      auto begin = current_.end();
      while (begin != end_ && splitter_->is_separator_(*begin))
        ++begin;
      auto end = begin;
      while (end != end_ && !splitter_->is_separator_(*end))
        ++end;
      current_ = base::StringPiece(begin, end - begin);
    }

    const StringSplitter* splitter_;

    // Contains the token currently pointed to by the iterator.
    base::StringPiece current_;
    // Always points to the text_.end().
    base::StringPiece::const_iterator end_;
  };

  // Constructs a splitter for iterating over non-empty tokens contained in the
  // |text|. |is_separator| predicate is used to determine whether a certain
  // character is a separator.
  StringSplitter(base::StringPiece text,
                 IsSeparator is_separator = IsSeparator())
      : text_(text), is_separator_(is_separator) {}

  Iterator begin() const { return Iterator(*this, text_.begin()); }
  Iterator end() const { return Iterator(*this, text_.end()); }

 private:
  base::StringPiece text_;
  IsSeparator is_separator_;
};

template <typename IsSeparator>
StringSplitter<IsSeparator> CreateStringSplitter(base::StringPiece text,
                                                 IsSeparator is_separator) {
  return StringSplitter<IsSeparator>(text, is_separator);
}

}  // namespace subresource_filter

#endif  // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_STRING_SPLITTER_H_