]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/string_view.h
support "install ./foo.changes"
[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>
5359675e 16#include <apt-pkg/macros.h>
fe7fa47c
JAK
17
18namespace APT {
19
20/**
21 * \brief Simple subset of std::string_view from C++17
22 *
23 * This is an internal implementation of the subset of std::string_view
24 * used by APT. It is not meant to be used in programs, only inside the
25 * library for performance critical paths.
26 */
5359675e 27class APT_HIDDEN StringView {
fe7fa47c
JAK
28 const char *data_;
29 size_t size_;
30
31public:
32 static constexpr size_t npos = static_cast<size_t>(-1);
9b2845e1 33 static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
fe7fa47c
JAK
34
35 /* Constructors */
36 constexpr StringView() : data_(""), size_(0) {}
37 constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
38
ef6cc0e2 39 StringView(const char *data) : data_(data), size_(strlen(data)) {}
ec6a4a83 40 StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
fe7fa47c 41
ef6cc0e2 42
fe7fa47c
JAK
43 /* Viewers */
44 constexpr StringView substr(size_t pos, size_t n = npos) const {
45 return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
46 }
47
9b2845e1
DK
48 size_t find(int c, size_t pos) const {
49 if (pos == 0)
50 return find(c);
51 size_t const found = substr(pos).find(c);
52 if (found == npos)
53 return npos;
54 return pos + found;
55 }
56 size_t find(int c) const {
fe7fa47c
JAK
57 const char *found = static_cast<const char*>(memchr(data_, c, size_));
58
59 if (found == NULL)
60 return npos;
61
62 return found - data_;
63 }
64
9b2845e1
DK
65 size_t rfind(int c, size_t pos) const {
66 if (pos == npos)
67 return rfind(c);
68 return APT::StringView(data_, pos).rfind(c);
69 }
70 size_t rfind(int c) const {
fe7fa47c
JAK
71 const char *found = static_cast<const char*>(memrchr(data_, c, size_));
72
73 if (found == NULL)
74 return npos;
75
76 return found - data_;
77 }
78
79 /* Conversions */
80 std::string to_string() const {
81 return std::string(data_, size_);
82 }
83
84 /* Comparisons */
85 int compare(size_t pos, size_t n, StringView other) const {
86 return substr(pos, n).compare(other);
87 }
88
89 int compare(StringView other) const {
90 int res;
91
92 res = memcmp(data_, other.data_, std::min(size_, other.size_));
93 if (res != 0)
94 return res;
95 if (size_ == other.size_)
96 return res;
97
98 return (size_ > other.size_) ? 1 : -1;
99 }
100
101 /* Optimization: If size not equal, string cannot be equal */
102 bool operator ==(StringView other) const { return size_ == other.size_ && compare(other) == 0; }
103 bool operator !=(StringView other) const { return !(*this == other); }
104
105 /* Accessors */
106 constexpr bool empty() const { return size_ == 0; }
107 constexpr const char* data() const { return data_; }
108 constexpr const char* begin() const { return data_; }
109 constexpr const char* end() const { return data_ + size_; }
110 constexpr char operator [](size_t i) const { return data_[i]; }
111 constexpr size_t size() const { return size_; }
112 constexpr size_t length() const { return size_; }
113};
114
115
116}
117
ef6cc0e2
JAK
118inline bool operator ==(const char *other, APT::StringView that);
119inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
fe7fa47c
JAK
120
121#endif