1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 // NOLINTNEXTLINE(bugprone-reserved-identifier)
4 #define _SCL_SECURE_NO_WARNINGS
6 #include "cmString.hxx"
12 #include <type_traits>
16 static std::string const empty_string_;
18 void String::internally_mutate_to_stable_string()
20 // We assume that only one thread mutates this instance at
21 // a time even if we point to a shared string buffer referenced
23 *this = String(this->data(), this->size());
26 bool String::is_stable() const
28 return this->str_if_stable() != nullptr;
31 void String::stabilize()
33 if (this->is_stable()) {
36 this->internally_mutate_to_stable_string();
39 std::string const* String::str_if_stable() const
43 // This is stable for the lifetime of our current value.
44 return &empty_string_;
47 if (this->string_ && this->data() == this->string_->data() &&
48 this->size() == this->string_->size()) {
49 // We view an entire string.
50 // This is stable for the lifetime of our current value.
51 return this->string_.get();
57 std::string const& String::str()
59 if (std::string const* s = this->str_if_stable()) {
62 // Mutate to hold a std::string that is stable for the lifetime
63 // of our current value.
64 this->internally_mutate_to_stable_string();
65 return *this->string_;
68 const char* String::c_str()
70 const char* c = this->data();
75 // We always point into a null-terminated string so it is safe to
76 // access one past the end. If it is a null byte then we can use
77 // the pointer directly.
78 if (c[this->size()] == '\0') {
82 // Mutate to hold a std::string so we can get a null terminator.
83 this->internally_mutate_to_stable_string();
84 c = this->string_->c_str();
88 String& String::insert(size_type index, size_type count, char ch)
91 s.reserve(this->size() + count);
92 s.assign(this->data(), this->size());
93 s.insert(index, count, ch);
94 return *this = std::move(s);
97 String& String::erase(size_type index, size_type count)
99 if (index > this->size()) {
100 throw std::out_of_range("Index out of range in String::erase");
102 size_type const rcount = std::min(count, this->size() - index);
103 size_type const rindex = index + rcount;
105 s.reserve(this->size() - rcount);
106 s.assign(this->data(), index);
107 s.append(this->data() + rindex, this->size() - rindex);
108 return *this = std::move(s);
111 String String::substr(size_type pos, size_type count) const
113 if (pos > this->size()) {
114 throw std::out_of_range("Index out of range in String::substr");
116 return String(*this, pos, count);
119 String::String(std::string&& s, Private)
120 : string_(std::make_shared<std::string>(std::move(s)))
121 , view_(this->string_->data(), this->string_->size())
125 String::size_type String::copy(char* dest, size_type count,
128 return this->view_.copy(dest, count, pos);
131 std::ostream& operator<<(std::ostream& os, String const& s)
133 return os.write(s.data(), s.size());
136 std::string& operator+=(std::string& self, String const& s)
138 return self += s.view();
141 String IntoString<char*>::into_string(const char* s)
146 return std::string(s);
149 string_view AsStringView<String>::view(String const& s)