xref: /aosp_15_r20/external/google-breakpad/src/common/string_view.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2021 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #ifndef COMMON_STRING_VIEW_H__
30 #define COMMON_STRING_VIEW_H__
31 
32 #include <cassert>
33 #include <cstring>
34 #include <ostream>
35 #include "common/using_std_string.h"
36 
37 namespace google_breakpad {
38 
39 // A StringView is a string reference to a string object, but not own the
40 // string object. It's a compatibile layer until we can use std::string_view in
41 // C++17.
42 class StringView {
43  private:
44   // The start of the string, in an external buffer. It doesn't have to be
45   // null-terminated.
46   const char* data_ = "";
47 
48   size_t length_ = 0;
49 
50  public:
51   // Construct an empty StringView.
52   StringView() = default;
53 
54   // Disallow construct StringView from nullptr.
55   StringView(std::nullptr_t) = delete;
56 
57   // Construct a StringView from a cstring.
StringView(const char * str)58   StringView(const char* str) : data_(str) {
59     assert(str);
60     length_ = strlen(str);
61   }
62 
63   // Construct a StringView from a cstring with fixed length.
StringView(const char * str,size_t length)64   StringView(const char* str, size_t length) : data_(str), length_(length) {
65     assert(str);
66   }
67 
68   // Construct a StringView from an std::string.
StringView(const string & str)69   StringView(const string& str) : data_(str.data()), length_(str.length()) {}
70 
str()71   string str() const { return string(data_, length_); }
72 
data()73   const char* data() const { return data_; }
74 
empty()75   bool empty() const { return length_ == 0; }
76 
size()77   size_t size() const { return length_; }
78 
compare(StringView rhs)79   int compare(StringView rhs) const {
80     size_t min_len = std::min(size(), rhs.size());
81     int res = memcmp(data_, rhs.data(), min_len);
82     if (res != 0)
83       return res;
84     if (size() == rhs.size())
85       return 0;
86     return size() < rhs.size() ? -1 : 1;
87   }
88 };
89 
90 inline bool operator==(StringView lhs, StringView rhs) {
91   return lhs.compare(rhs) == 0;
92 }
93 
94 inline bool operator!=(StringView lhs, StringView rhs) {
95   return lhs.compare(rhs) != 0;
96 }
97 
98 inline bool operator<(StringView lhs, StringView rhs) {
99   return lhs.compare(rhs) < 0;
100 }
101 
102 inline bool operator>(StringView lhs, StringView rhs) {
103   return lhs.compare(rhs) > 0;
104 }
105 
106 inline std::ostream& operator<<(std::ostream& os, StringView s) {
107   os << s.str();
108   return os;
109 }
110 
111 }  // namespace google_breakpad
112 
113 #endif  // COMMON_STRING_VIEW_H__
114