]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/string_view.h
operator==(char*, StringView) use StringView.operator==
[apt.git] / apt-pkg / contrib / string_view.h
CommitLineData
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
17namespace 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 */
26class StringView {
27 const char *data_;
28 size_t size_;
29
30public:
31 static constexpr size_t npos = static_cast<size_t>(-1);
32
33 /* Constructors */
34 constexpr StringView() : data_(""), size_(0) {}
35 constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
36
37 StringView(const char *data) : data_(data), size_(strlen(data)) {}
38 StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
39
40
41 /* Viewers */
42 constexpr StringView substr(size_t pos, size_t n = npos) const {
43 return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
44 }
45
46 size_t find(int c, size_t pos=0) const {
47 if (pos != 0)
48 return substr(pos).find(c);
49
50 const char *found = static_cast<const char*>(memchr(data_, c, size_));
51
52 if (found == NULL)
53 return npos;
54
55 return found - data_;
56 }
57
eee8ee17
JAK
58 size_t rfind(int c, size_t pos=npos) const {
59 if (pos != npos)
0e58689a 60 return substr(0, pos).rfind(c);
fe7fa47c
JAK
61
62 const char *found = static_cast<const char*>(memrchr(data_, c, size_));
63
64 if (found == NULL)
65 return npos;
66
67 return found - data_;
68 }
69
70 /* Conversions */
71 std::string to_string() const {
72 return std::string(data_, size_);
73 }
74
75 /* Comparisons */
76 int compare(size_t pos, size_t n, StringView other) const {
77 return substr(pos, n).compare(other);
78 }
79
80 int compare(StringView other) const {
81 int res;
82
83 res = memcmp(data_, other.data_, std::min(size_, other.size_));
84 if (res != 0)
85 return res;
86 if (size_ == other.size_)
87 return res;
88
89 return (size_ > other.size_) ? 1 : -1;
90 }
91
92 /* Optimization: If size not equal, string cannot be equal */
93 bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
94 bool operator !=(StringView other) const { return !(*this == other); }
95
96 /* Accessors */
97 constexpr bool empty() const { return size_ == 0; }
98 constexpr const char* data() const { return data_; }
99 constexpr const char* begin() const { return data_; }
100 constexpr const char* end() const { return data_ + size_; }
101 constexpr char operator [](size_t i) const { return data_[i]; }
102 constexpr size_t size() const { return size_; }
103 constexpr size_t length() const { return size_; }
104};
105
106
107}
108
109inline bool operator ==(const char *other, APT::StringView that);
0cc8987a 110inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
fe7fa47c
JAK
111
112#endif