]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/split.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
5 * Ceph - scalable distributed file system
7 * Copyright (C) 2019 Red Hat, Inc.
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
18 #include <string_view>
22 // a forward iterator over the parts of a split string
24 std::string_view str
; // full string
25 std::string_view delims
; // delimiters
27 using size_type
= std::string_view::size_type
;
28 size_type pos
= 0; // start position of current part
29 std::string_view part
; // view of current part
31 // return the next part after the given position
32 std::string_view
next(size_type end
) {
33 pos
= str
.find_first_not_of(delims
, end
);
34 if (pos
== str
.npos
) {
37 return str
.substr(pos
, str
.find_first_of(delims
, pos
) - pos
);
40 // types required by std::iterator_traits
41 using difference_type
= int;
42 using value_type
= std::string_view
;
43 using pointer
= const value_type
*;
44 using reference
= const value_type
&;
45 using iterator_category
= std::forward_iterator_tag
;
47 spliterator() = default;
49 spliterator(std::string_view str
, std::string_view delims
)
50 : str(str
), delims(delims
), pos(0), part(next(0))
53 spliterator
& operator++() {
54 part
= next(pos
+ part
.size());
57 spliterator
operator++(int) {
58 spliterator tmp
= *this;
59 part
= next(pos
+ part
.size());
63 reference
operator*() const { return part
; }
64 pointer
operator->() const { return &part
; }
66 friend bool operator==(const spliterator
& lhs
, const spliterator
& rhs
) {
67 return lhs
.part
.data() == rhs
.part
.data()
68 && lhs
.part
.size() == rhs
.part
.size();
70 friend bool operator!=(const spliterator
& lhs
, const spliterator
& rhs
) {
71 return lhs
.part
.data() != rhs
.part
.data()
72 || lhs
.part
.size() != rhs
.part
.size();
76 // represents an immutable range of split string parts
78 // ranged-for loop example:
80 // for (std::string_view s : split(input)) {
83 // container initialization example:
85 // auto parts = split(input);
87 // std::vector<std::string> strings;
88 // strings.assign(parts.begin(), parts.end());
91 std::string_view str
; // full string
92 std::string_view delims
; // delimiters
94 split(std::string_view str
, std::string_view delims
= ";,= \t\n")
95 : str(str
), delims(delims
) {}
97 using iterator
= spliterator
;
98 using const_iterator
= spliterator
;
100 iterator
begin() const { return {str
, delims
}; }
101 const_iterator
cbegin() const { return {str
, delims
}; }
103 iterator
end() const { return {}; }
104 const_iterator
cend() const { return {}; }