Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_view.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/string_view.hpp
3  *
4  * A simplified string_view implementation to reduce the number of allocations
5  * in the WordCount benchmark.
6  *
7  * Part of Project Thrill - http://project-thrill.org
8  *
9  * Copyright (C) 2015 Alexander Noe <[email protected]>
10  * Copyright (C) 2016 Timo Bingmann <[email protected]>
11  *
12  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
13  ******************************************************************************/
14 
15 #pragma once
16 #ifndef THRILL_COMMON_STRING_VIEW_HEADER
17 #define THRILL_COMMON_STRING_VIEW_HEADER
18 
19 #include <algorithm>
20 #include <ostream>
21 #include <string>
22 
23 namespace thrill {
24 namespace common {
25 
26 /*!
27  * StringView is a reference to a part of a string, consisting of only a char
28  * pointer and a length. It does not have ownership of the substring and is used
29  * mainly for temporary objects.
30  */
32 {
33 public:
34  using iterator = const char*;
35 
36  //! Default constructor for a StringView. Doesn't do anything.
37  StringView() = default;
38 
39  /*!
40  * Creates a new StringView, given a const char* and the size.
41  *
42  * \param data pointer to start of data
43  * \param size size of data in bytes.
44  * \return new StringView object.
45  */
46  StringView(const char* data, size_t size) noexcept
47  : data_(data), size_(size) { }
48 
49  /*!
50  * Creates a new StringView, given a const iterator to a std::string and the
51  * size.
52  *
53  * \param data iterator to start of data
54  * \param size size of data in character.
55  * \return new StringView object.
56  */
57  StringView(const std::string::const_iterator& data, size_t size) noexcept
58  : data_(&(*data)), size_(size) { }
59 
60  /*!
61  * Creates a new reference StringView, given two const iterators to a
62  * std::string.
63  *
64  * \param begin iterator to start of data
65  * \param end iterator to the end of data.
66  * \return new StringView object.
67  */
68  StringView(const std::string::const_iterator& begin,
69  const std::string::const_iterator& end) noexcept
70  : StringView(begin, end - begin) { }
71 
72  //! Construct a StringView to the whole std::string.
73  explicit StringView(const std::string& str) noexcept
74  : StringView(str.begin(), str.end()) { }
75 
76  //! Returns a pointer to the start of the data.
77  const char * data() const noexcept {
78  return data_;
79  }
80 
81  //! Returns a pointer to the beginning of the data.
82  iterator begin() const noexcept {
83  return data_;
84  }
85 
86  //! Returns a pointer beyond the end of the data.
87  iterator end() const noexcept {
88  return data_ + size_;
89  }
90 
91  //! Returns the size of this StringView
92  size_t size() const noexcept {
93  return size_;
94  }
95 
96  //! Equality operator to compare a StringView with another StringView
97  bool operator == (const StringView& other) const noexcept {
98  return size_ == other.size_ &&
99  std::equal(data_, data_ + size_, other.data_);
100  }
101 
102  //! Inequality operator to compare a StringView with another StringView
103  bool operator != (const StringView& other) const noexcept {
104  return !(operator == (other));
105  }
106 
107  //! Equality operator to compare a StringView with an std::string
108  bool operator == (const std::string& other) const noexcept {
109  return size_ == other.size() &&
110  std::equal(data_, data_ + size_, other.data());
111  }
112 
113  //! Inequality operator to compare a StringView with an std::string
114  bool operator != (const std::string& other) const noexcept {
115  return !(operator == (other));
116  }
117 
118  //! make StringView ostreamable
119  friend std::ostream& operator << (std::ostream& os, const StringView& sv) {
120  return os.write(sv.data(), sv.size());
121  }
122 
123  //! Returns the data of this StringView as an std::string
125  return std::string(data_, size_);
126  }
127 
128  // operator std::string () const { return ToString(); }
129 
130 private:
131  //! pointer to character data
132  const char* data_ = nullptr;
133  //! size of data
134  size_t size_ = 0;
135 };
136 
137 static inline
138 bool operator == (const std::string& a, const StringView& b) noexcept {
139  return b == a;
140 }
141 
142 static inline
143 bool operator != (const std::string& a, const StringView& b) noexcept {
144  return b != a;
145 }
146 
147 /*!
148  * Split the given string at each separator character into distinct substrings,
149  * and call the given callback for each substring, represented by two iterators
150  * begin and end. Multiple consecutive separators are considered individually
151  * and will result in empty split substrings.
152  *
153  * \param str string to split
154  * \param sep separator character
155  * \param callback callback taking begin and end iterator of substring
156  * \param limit maximum number of parts returned
157  */
158 template <typename F>
159 static inline
161  const std::string& str, char sep, F&& callback,
162  std::string::size_type limit = std::string::npos) {
163 
164  if (limit == 0)
165  {
166  callback(StringView(str.begin(), str.end()));
167  return;
168  }
169 
170  std::string::size_type count = 0;
171  auto it = str.begin(), last = it;
172 
173  for ( ; it != str.end(); ++it)
174  {
175  if (*it == sep)
176  {
177  if (count == limit)
178  {
179  callback(StringView(last, str.end()));
180  return;
181  }
182  callback(StringView(last, it));
183  ++count;
184  last = it + 1;
185  }
186  }
187  callback(StringView(last, it));
188 }
189 
190 } // namespace common
191 } // namespace thrill
192 
193 namespace std {
194 template <>
195 struct hash<thrill::common::StringView>{
196  size_t operator () (const thrill::common::StringView& sv) const {
197  // simple string hash taken from: http://www.cse.yorku.ca/~oz/hash.html
198  size_t hash = 5381;
199  for (const char* ctr = sv.begin(); ctr != sv.end(); ++ctr) {
200  // hash * 33 + c
201  hash = ((hash << 5) + hash) + *ctr;
202  }
203  return hash;
204  }
205 };
206 
207 } // namespace std
208 
209 #endif // !THRILL_COMMON_STRING_VIEW_HEADER
210 
211 /******************************************************************************/
bool operator==(const StringView &other) const noexcept
Equality operator to compare a StringView with another StringView.
Definition: string_view.hpp:97
StringView(const std::string &str) noexcept
Construct a StringView to the whole std::string.
Definition: string_view.hpp:73
static void SplitView(const std::string &str, char sep, F &&callback, std::string::size_type limit=std::string::npos)
Split the given string at each separator character into distinct substrings, and call the given callb...
static bool operator!=(const std::string &a, const StringView &b) noexcept
friend std::ostream & operator<<(std::ostream &os, const StringView &sv)
make StringView ostreamable
StringView(const char *data, size_t size) noexcept
Creates a new StringView, given a const char* and the size.
Definition: string_view.hpp:46
size_t size_
size of data
static bool operator==(const std::string &a, const StringView &b) noexcept
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
StringView(const std::string::const_iterator &data, size_t size) noexcept
Creates a new StringView, given a const iterator to a std::string and the size.
Definition: string_view.hpp:57
bool operator!=(const StringView &other) const noexcept
Inequality operator to compare a StringView with another StringView.
iterator end() const noexcept
Returns a pointer beyond the end of the data.
Definition: string_view.hpp:87
size_t size() const noexcept
Returns the size of this StringView.
Definition: string_view.hpp:92
std::string ToString() const
Returns the data of this StringView as an std::string.
StringView(const std::string::const_iterator &begin, const std::string::const_iterator &end) noexcept
Creates a new reference StringView, given two const iterators to a std::string.
Definition: string_view.hpp:68
StringView()=default
Default constructor for a StringView. Doesn't do anything.
HashCrc32< T > hash
Select a hashing method.
Definition: hash.hpp:257
iterator begin() const noexcept
Returns a pointer to the beginning of the data.
Definition: string_view.hpp:82
StringView is a reference to a part of a string, consisting of only a char pointer and a length...
Definition: string_view.hpp:31
const char * data_
pointer to character data
const char * data() const noexcept
Returns a pointer to the start of the data.
Definition: string_view.hpp:77