]>
Commit | Line | Data |
---|---|---|
fe7fa47c JAK |
1 | /* |
2 | * Basic implementation of string_view | |
3 | * | |
4 | * (C) 2015 Julian Andres Klode <jak@debian.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #if !defined(APT_STRINGVIEW_H) && defined(APT_PKG_EXPOSE_STRING_VIEW) | |
13 | #define APT_STRINGVIEW_H | |
14 | #include <string.h> | |
15 | #include <string> | |
16 | ||
17 | namespace APT { | |
18 | ||
19 | /** | |
20 | * \brief Simple subset of std::string_view from C++17 | |
21 | * | |
22 | * This is an internal implementation of the subset of std::string_view | |
23 | * used by APT. It is not meant to be used in programs, only inside the | |
24 | * library for performance critical paths. | |
25 | */ | |
26 | class StringView { | |
27 | const char *data_; | |
28 | size_t size_; | |
29 | ||
30 | public: | |
31 | static constexpr size_t npos = static_cast<size_t>(-1); | |
9b2845e1 | 32 | static_assert(APT::StringView::npos == std::string::npos, "npos values are different"); |
fe7fa47c JAK |
33 | |
34 | /* Constructors */ | |
35 | constexpr StringView() : data_(""), size_(0) {} | |
36 | constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {} | |
37 | ||
ef6cc0e2 | 38 | StringView(const char *data) : data_(data), size_(strlen(data)) {} |
ec6a4a83 | 39 | StringView(std::string const & str): data_(str.data()), size_(str.size()) {} |
fe7fa47c | 40 | |
ef6cc0e2 | 41 | |
fe7fa47c JAK |
42 | /* Viewers */ |
43 | constexpr StringView substr(size_t pos, size_t n = npos) const { | |
44 | return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n); | |
45 | } | |
46 | ||
9b2845e1 DK |
47 | size_t find(int c, size_t pos) const { |
48 | if (pos == 0) | |
49 | return find(c); | |
50 | size_t const found = substr(pos).find(c); | |
51 | if (found == npos) | |
52 | return npos; | |
53 | return pos + found; | |
54 | } | |
55 | size_t find(int c) const { | |
fe7fa47c JAK |
56 | const char *found = static_cast<const char*>(memchr(data_, c, size_)); |
57 | ||
58 | if (found == NULL) | |
59 | return npos; | |
60 | ||
61 | return found - data_; | |
62 | } | |
63 | ||
9b2845e1 DK |
64 | size_t rfind(int c, size_t pos) const { |
65 | if (pos == npos) | |
66 | return rfind(c); | |
67 | return APT::StringView(data_, pos).rfind(c); | |
68 | } | |
69 | size_t rfind(int c) const { | |
fe7fa47c JAK |
70 | const char *found = static_cast<const char*>(memrchr(data_, c, size_)); |
71 | ||
72 | if (found == NULL) | |
73 | return npos; | |
74 | ||
75 | return found - data_; | |
76 | } | |
77 | ||
78 | /* Conversions */ | |
79 | std::string to_string() const { | |
80 | return std::string(data_, size_); | |
81 | } | |
82 | ||
83 | /* Comparisons */ | |
84 | int compare(size_t pos, size_t n, StringView other) const { | |
85 | return substr(pos, n).compare(other); | |
86 | } | |
87 | ||
88 | int compare(StringView other) const { | |
89 | int res; | |
90 | ||
91 | res = memcmp(data_, other.data_, std::min(size_, other.size_)); | |
92 | if (res != 0) | |
93 | return res; | |
94 | if (size_ == other.size_) | |
95 | return res; | |
96 | ||
97 | return (size_ > other.size_) ? 1 : -1; | |
98 | } | |
99 | ||
100 | /* Optimization: If size not equal, string cannot be equal */ | |
101 | bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; } | |
102 | bool operator !=(StringView other) const { return !(*this == other); } | |
103 | ||
104 | /* Accessors */ | |
105 | constexpr bool empty() const { return size_ == 0; } | |
106 | constexpr const char* data() const { return data_; } | |
107 | constexpr const char* begin() const { return data_; } | |
108 | constexpr const char* end() const { return data_ + size_; } | |
109 | constexpr char operator [](size_t i) const { return data_[i]; } | |
110 | constexpr size_t size() const { return size_; } | |
111 | constexpr size_t length() const { return size_; } | |
112 | }; | |
113 | ||
114 | ||
115 | } | |
116 | ||
ef6cc0e2 JAK |
117 | inline bool operator ==(const char *other, APT::StringView that); |
118 | inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); } | |
fe7fa47c JAK |
119 | |
120 | #endif |