diff options
author | Brad King <brad.king@kitware.com> | 2018-12-13 14:58:30 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2018-12-13 14:58:50 (GMT) |
commit | 0be5ed7128b59c6ac3b508fa89fe793f5cfdf07c (patch) | |
tree | c29f1443d50156e4c5f62ef149cff8bc27751458 | |
parent | dc90cd68778b374122162cd3a87a7b6d27ed6866 (diff) | |
parent | 2d68b2c593b0bc19273e95f98ff29b3d3cff90c7 (diff) | |
download | CMake-0be5ed7128b59c6ac3b508fa89fe793f5cfdf07c.zip CMake-0be5ed7128b59c6ac3b508fa89fe793f5cfdf07c.tar.gz CMake-0be5ed7128b59c6ac3b508fa89fe793f5cfdf07c.tar.bz2 |
Merge topic 'custom-string'
2d68b2c593 String: Add str_if_stable() as a const alternative to str()
a0841b59bd String: Add support for a ""_s string literal syntax
9d5fe8e96a String: Add 'borrow' member to construct borrowing instances
80802a002c String: Add support for concatenation by operator+
ff69763ca0 String: Add a custom string type
410a3e4b22 Add support for using C++17 string_view or a fallback
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Daniel Pfeifer <daniel@pfeifer-mail.de>
Acked-by: Pavel Solodovnikov <hellyeahdominate@gmail.com>
Merge-request: !2578
-rw-r--r-- | Source/CMakeLists.txt | 5 | ||||
-rw-r--r-- | Source/cmString.cxx | 152 | ||||
-rw-r--r-- | Source/cmString.hxx | 815 | ||||
-rw-r--r-- | Source/cm_static_string_view.hxx | 41 | ||||
-rw-r--r-- | Source/cm_string_view.cxx | 301 | ||||
-rw-r--r-- | Source/cm_string_view.hxx | 217 | ||||
-rw-r--r-- | Tests/CMakeLib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/CMakeLib/testString.cxx | 1347 | ||||
-rwxr-xr-x | bootstrap | 1 |
9 files changed, 2880 insertions, 0 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index e86cc2f..91c45bc 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -383,6 +383,9 @@ set(SRCS cmXMLWriter.h cmake.cxx cmake.h + cm_string_view.cxx + cm_string_view.hxx + cm_static_string_view.hxx cmCommand.cxx cmCommand.h @@ -574,6 +577,8 @@ set(SRCS cmSiteNameCommand.h cmSourceGroupCommand.cxx cmSourceGroupCommand.h + cmString.cxx + cmString.hxx cmStringReplaceHelper.cxx cmStringCommand.cxx cmStringCommand.h diff --git a/Source/cmString.cxx b/Source/cmString.cxx new file mode 100644 index 0000000..2a0c125 --- /dev/null +++ b/Source/cmString.cxx @@ -0,0 +1,152 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#define _SCL_SECURE_NO_WARNINGS + +#include "cmString.hxx" + +#include <memory> +#include <ostream> +#include <stdexcept> +#include <string> +#include <type_traits> + +namespace cm { + +static std::string const empty_string_; + +void String::internally_mutate_to_stable_string() +{ + // We assume that only one thread mutates this instance at + // a time even if we point to a shared string buffer refernced + // by other threads. + *this = String(data(), size()); +} + +bool String::is_stable() const +{ + return str_if_stable() != nullptr; +} + +void String::stabilize() +{ + if (is_stable()) { + return; + } + this->internally_mutate_to_stable_string(); +} + +std::string const* String::str_if_stable() const +{ + if (!data()) { + // We view no string. + // This is stable for the lifetime of our current value. + return &empty_string_; + } + + if (string_ && data() == string_->data() && size() == string_->size()) { + // We view an entire string. + // This is stable for the lifetime of our current value. + return string_.get(); + } + + return nullptr; +} + +std::string const& String::str() +{ + if (std::string const* s = str_if_stable()) { + return *s; + } + // Mutate to hold a std::string that is stable for the lifetime + // of our current value. + this->internally_mutate_to_stable_string(); + return *string_; +} + +const char* String::c_str() +{ + const char* c = data(); + if (c == nullptr) { + return c; + } + + // We always point into a null-terminated string so it is safe to + // access one past the end. If it is a null byte then we can use + // the pointer directly. + if (c[size()] == '\0') { + return c; + } + + // Mutate to hold a std::string so we can get a null terminator. + this->internally_mutate_to_stable_string(); + c = string_->c_str(); + return c; +} + +String& String::insert(size_type index, size_type count, char ch) +{ + std::string s; + s.reserve(size() + count); + s.assign(data(), size()); + s.insert(index, count, ch); + return *this = std::move(s); +} + +String& String::erase(size_type index, size_type count) +{ + if (index > size()) { + throw std::out_of_range("Index out of range in String::erase"); + } + size_type const rcount = std::min(count, size() - index); + size_type const rindex = index + rcount; + std::string s; + s.reserve(size() - rcount); + s.assign(data(), index); + s.append(data() + rindex, size() - rindex); + return *this = std::move(s); +} + +String String::substr(size_type pos, size_type count) const +{ + if (pos > size()) { + throw std::out_of_range("Index out of range in String::substr"); + } + return String(*this, pos, count); +} + +String::String(std::string&& s, Private) + : string_(std::make_shared<std::string>(std::move(s))) + , view_(string_->data(), string_->size()) +{ +} + +String::size_type String::copy(char* dest, size_type count, + size_type pos) const +{ + return view_.copy(dest, count, pos); +} + +std::ostream& operator<<(std::ostream& os, String const& s) +{ + return os.write(s.data(), s.size()); +} + +std::string& operator+=(std::string& self, String const& s) +{ + return self += s.view(); +} + +String IntoString<char*>::into_string(const char* s) +{ + if (!s) { + return String(); + } + return std::string(s); +} + +string_view AsStringView<String>::view(String const& s) +{ + return s.view(); +} + +} // namespace cm diff --git a/Source/cmString.hxx b/Source/cmString.hxx new file mode 100644 index 0000000..1623a43 --- /dev/null +++ b/Source/cmString.hxx @@ -0,0 +1,815 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef cmString_hxx +#define cmString_hxx + +#include "cmConfigure.h" // IWYU pragma: keep + +#include "cm_static_string_view.hxx" +#include "cm_string_view.hxx" + +#include <algorithm> +#include <functional> +#include <initializer_list> +#include <memory> +#include <ostream> +#include <string> +#include <type_traits> + +namespace cm { + +class String; + +/** + * Trait to convert type T into a String. + * Implementations must derive from 'std::true_type' + * and define an 'into_string' member that accepts + * type T (by value or reference) and returns one of: + * + * - 'std::string' to construct an owned instance. + * - 'cm::string_view' to construct a borrowed or null instances. + * The buffer from which the view is borrowed must outlive + * all copies of the resulting String, e.g. static storage. + * - 'cm::String' for already-constructed instances. + */ +template <typename T> +struct IntoString : std::false_type +{ +}; + +template <typename T> +struct IntoString<T&> : IntoString<T> +{ +}; + +template <typename T> +struct IntoString<T const> : IntoString<T> +{ +}; + +template <typename T> +struct IntoString<T const*> : IntoString<T*> +{ +}; + +template <typename T, std::string::size_type N> +struct IntoString<T const[N]> : IntoString<T[N]> +{ +}; + +template <> +struct IntoString<char*> : std::true_type +{ + static String into_string(const char* s); +}; + +template <> +struct IntoString<std::nullptr_t> : std::true_type +{ + static string_view into_string(std::nullptr_t) { return string_view(); } +}; + +template <std::string::size_type N> +struct IntoString<char[N]> : std::true_type +{ + static std::string into_string(char const (&s)[N]) + { + return std::string(s, N - 1); + } +}; + +template <> +struct IntoString<std::string> : std::true_type +{ + static std::string into_string(std::string s) { return s; } +}; + +template <> +struct IntoString<string_view> : std::true_type +{ + static std::string into_string(string_view s) { return std::string(s); } +}; + +template <> +struct IntoString<static_string_view> : std::true_type +{ + static string_view into_string(static_string_view s) { return s; } +}; + +template <> +struct IntoString<char> : std::true_type +{ + static std::string into_string(char const& c) { return std::string(1, c); } +}; + +/** + * Trait to convert type T into a 'cm::string_view'. + * Implementations must derive from 'std::true_type' and + * define a 'view' member that accepts type T (by reference) + * and returns a 'cm::string_view'. + */ +template <typename T> +struct AsStringView : std::false_type +{ +}; + +template <typename T> +struct AsStringView<T&> : AsStringView<T> +{ +}; + +template <typename T> +struct AsStringView<T const> : AsStringView<T> +{ +}; + +template <typename T> +struct AsStringView<T const*> : AsStringView<T*> +{ +}; + +template <typename T, std::string::size_type N> +struct AsStringView<T const[N]> : AsStringView<T[N]> +{ +}; + +template <> +struct AsStringView<char*> : std::true_type +{ + static string_view view(const char* s) { return s; } +}; + +template <std::string::size_type N> +struct AsStringView<char[N]> : std::true_type +{ + static string_view view(char const (&s)[N]) { return string_view(s, N - 1); } +}; + +template <> +struct AsStringView<std::string> : std::true_type +{ + static string_view view(std::string const& s) { return s; } +}; + +template <> +struct AsStringView<char> : std::true_type +{ + static string_view view(const char& s) { return string_view(&s, 1); } +}; + +template <> +struct AsStringView<string_view> : std::true_type +{ + static string_view view(string_view const& s) { return s; } +}; + +template <> +struct AsStringView<static_string_view> : std::true_type +{ + static string_view view(static_string_view const& s) { return s; } +}; + +template <> +struct AsStringView<String> : std::true_type +{ + static string_view view(String const& s); +}; + +/** + * \class String + * + * A custom string type that holds a view of a string buffer + * and optionally shares ownership of the buffer. Instances + * may have one of the following states: + * + * - null: views and owns nothing. + * Conversion to 'bool' is 'false'. + * 'data()' and 'c_str()' return nullptr. + * 'size()' returns 0. + * 'str()' returns an empty string. + * + * - borrowed: views a string but does not own it. This is used + * to bind to static storage (e.g. string literals) or for + * temporary instances that do not outlive the borrowed buffer. + * Copies and substrings still borrow the original buffer. + * Mutation allocates a new internal string and converts to + * the 'owned' state. + * Conversion to 'bool' is 'true'. + * 'c_str()' may internally mutate to the 'owned' state. + * 'str()' internally mutates to the 'owned' state. + * + * - owned: views an immutable 'std::string' instance owned internally. + * Copies and substrings share ownership of the internal string. + * Mutation allocates a new internal string. + * Conversion to 'bool' is 'true'. + */ +class String +{ + enum class Private + { + }; + +public: + using traits_type = std::string::traits_type; + using value_type = string_view::value_type; + using pointer = string_view::pointer; + using const_pointer = string_view::const_pointer; + using reference = string_view::reference; + using const_reference = string_view::const_reference; + using const_iterator = string_view::const_iterator; + using iterator = string_view::const_iterator; + using const_reverse_iterator = string_view::const_reverse_iterator; + using reverse_iterator = string_view::const_reverse_iterator; + using difference_type = string_view::difference_type; + using size_type = string_view::size_type; + + static size_type const npos = string_view::npos; + + /** Construct a null string. */ + String() = default; + + /** Construct from any type implementing the IntoString trait. */ + template <typename T, + typename = typename std::enable_if<IntoString<T>::value>::type> + String(T&& s) + : String(IntoString<T>::into_string(std::forward<T>(s)), Private()) + { + } + + /** Construct via std::string initializer list constructor. */ + String(std::initializer_list<char> il) + : String(std::string(il)) + { + } + + /** Construct by copying the specified buffer. */ + String(const char* d, size_type s) + : String(std::string(d, s)) + { + } + + /** Construct by copying from input iterator range. */ + template <typename InputIterator> + String(InputIterator first, InputIterator last) + : String(std::string(first, last)) + { + } + + /** Construct a string with 'n' copies of character 'c'. */ + String(size_type n, char c) + : String(std::string(n, c)) + { + } + + /** Construct from a substring of another String instance. + This shares ownership of the other string's buffer + but views only a substring. */ + String(String const& s, size_type pos, size_type count = npos) + : string_(s.string_) + , view_(s.data() + pos, std::min(count, s.size() - pos)) + { + } + + /** Construct by moving from another String instance. + The other instance is left as a null string. */ + String(String&& s) noexcept + : string_(std::move(s.string_)) + , view_(s.view_) + { + s.view_ = string_view(); + } + + /** Construct by copying from another String instance. + This shares ownership of the other string's buffer. */ + String(String const&) noexcept = default; + + ~String() = default; + + /** Construct by borrowing an externally-owned buffer. The buffer + must outlive the returned instance and all copies of it. */ + static String borrow(string_view v) { return String(v, Private()); } + + /** Assign by moving from another String instance. + The other instance is left as a null string. */ + String& operator=(String&& s) noexcept + { + string_ = std::move(s.string_); + view_ = s.view_; + s.view_ = string_view(); + return *this; + } + + /** Assign by copying from another String instance. + This shares ownership of the other string's buffer. */ + String& operator=(String const&) noexcept = default; + + /** Assign from any type implementing the IntoString trait. */ + template <typename T> + typename // NOLINT(*) + std::enable_if<IntoString<T>::value, String&>::type + operator=(T&& s) + { + *this = String(std::forward<T>(s)); + return *this; + } + + /** Assign via std::string initializer list constructor. */ + String& operator=(std::initializer_list<char> il) + { + *this = String(il); + return *this; + } + + /** Return true if the instance is not a null string. */ + explicit operator bool() const noexcept { return data() != nullptr; } + + /** Return a view of the string. */ + string_view view() const noexcept { return view_; } + + /** Return true if the instance is an empty stringn or null string. */ + bool empty() const noexcept { return view_.empty(); } + + /** Return a pointer to the start of the string. */ + const char* data() const noexcept { return view_.data(); } + + /** Return the length of the string in bytes. */ + size_type size() const noexcept { return view_.size(); } + size_type length() const noexcept { return view_.length(); } + + /** Return the character at the given position. + No bounds checking is performed. */ + char operator[](size_type pos) const noexcept { return view_[pos]; } + + /** Return the character at the given position. + If the position is out of bounds, throws std::out_of_range. */ + char at(size_type pos) const { return view_.at(pos); } + + char front() const noexcept { return view_.front(); } + + char back() const noexcept { return view_.back(); } + + /** Return true if this instance is stable and otherwise false. + An instance is stable if it is in the 'null' state or if it is + an 'owned' state not produced by substring operations, or + after a call to 'stabilize()' or 'str()'. */ + bool is_stable() const; + + /** If 'is_stable()' does not return true, mutate so it does. */ + void stabilize(); + + /** Get a pointer to a normal std::string if 'is_stable()' returns + true and otherwise nullptr. The pointer is valid until this + instance is mutated or destroyed. */ + std::string const* str_if_stable() const; + + /** Get a refernce to a normal std::string. The reference + is valid until this instance is mutated or destroyed. */ + std::string const& str(); + + /** Get a pointer to a C-style null-terminated string + containing the same value as this instance. The pointer + is valid until this instance is mutated, destroyed, + or str() is called. */ + const char* c_str(); + + const_iterator begin() const noexcept { return view_.begin(); } + const_iterator end() const noexcept { return view_.end(); } + const_iterator cbegin() const noexcept { return begin(); } + const_iterator cend() const noexcept { return end(); } + + const_reverse_iterator rbegin() const noexcept { return view_.rbegin(); } + const_reverse_iterator rend() const noexcept { return view_.rend(); } + const_reverse_iterator crbegin() const noexcept { return rbegin(); } + const_reverse_iterator crend() const noexcept { return rend(); } + + /** Append to the string using any type that implements the + AsStringView trait. */ + template <typename T> + typename std::enable_if<AsStringView<T>::value, String&>::type operator+=( + T&& s) + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + std::string r; + r.reserve(size() + v.size()); + r.assign(data(), size()); + r.append(v.data(), v.size()); + return *this = std::move(r); + } + + /** Assign to an empty string. */ + void clear() { *this = ""_s; } + + /** Insert 'count' copies of 'ch' at position 'index'. */ + String& insert(size_type index, size_type count, char ch); + + /** Erase 'count' characters starting at position 'index'. */ + String& erase(size_type index = 0, size_type count = npos); + + void push_back(char ch) + { + std::string s; + s.reserve(size() + 1); + s.assign(data(), size()); + s.push_back(ch); + *this = std::move(s); + } + + void pop_back() { *this = String(*this, 0, size() - 1); } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, String&>::type replace( + size_type pos, size_type count, T&& s) + { + const_iterator first = begin() + pos; + const_iterator last = first + count; + return replace(first, last, std::forward<T>(s)); + } + + template <typename InputIterator> + String& replace(const_iterator first, const_iterator last, + InputIterator first2, InputIterator last2) + { + std::string out; + out.append(view_.begin(), first); + out.append(first2, last2); + out.append(last, view_.end()); + return *this = std::move(out); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, String&>::type replace( + const_iterator first, const_iterator last, T&& s) + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + std::string out; + out.reserve((first - view_.begin()) + v.size() + (view_.end() - last)); + out.append(view_.begin(), first); + out.append(v.data(), v.size()); + out.append(last, view_.end()); + return *this = std::move(out); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, String&>::type replace( + size_type pos, size_type count, T&& s, size_type pos2, + size_type count2 = npos) + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + v = v.substr(pos2, count2); + return replace(pos, count, v); + } + + String& replace(size_type pos, size_type count, size_type count2, char ch) + { + const_iterator first = begin() + pos; + const_iterator last = first + count; + return replace(first, last, count2, ch); + } + + String& replace(const_iterator first, const_iterator last, size_type count2, + char ch) + { + std::string out; + out.reserve((first - view_.begin()) + count2 + (view_.end() - last)); + out.append(view_.begin(), first); + out.append(count2, ch); + out.append(last, view_.end()); + return *this = std::move(out); + } + + size_type copy(char* dest, size_type count, size_type pos = 0) const; + + void resize(size_type count) { resize(count, char()); } + + void resize(size_type count, char ch) + { + std::string s; + s.reserve(count); + if (count <= size()) { + s.assign(data(), count); + } else { + s.assign(data(), size()); + s.resize(count, ch); + } + *this = std::move(s); + } + + void swap(String& other) + { + std::swap(string_, other.string_); + std::swap(view_, other.view_); + } + + /** Return a substring starting at position 'pos' and + consisting of at most 'count' characters. */ + String substr(size_type pos = 0, size_type count = npos) const; + + template <typename T> + typename std::enable_if<AsStringView<T>::value, int>::type compare( + T&& s) const + { + return view_.compare(AsStringView<T>::view(std::forward<T>(s))); + } + + int compare(size_type pos1, size_type count1, string_view v) const + { + return view_.compare(pos1, count1, v); + } + + int compare(size_type pos1, size_type count1, string_view v, size_type pos2, + size_type count2) const + { + return view_.compare(pos1, count1, v, pos2, count2); + } + + int compare(size_type pos1, size_type count1, const char* s) const + { + return view_.compare(pos1, count1, s); + } + + int compare(size_type pos1, size_type count1, const char* s, + size_type count2) const + { + return view_.compare(pos1, count1, s, count2); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type find( + T&& s, size_type pos = 0) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.find(v, pos); + } + + size_type find(const char* s, size_type pos, size_type count) const + { + return view_.find(s, pos, count); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type rfind( + T&& s, size_type pos = npos) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.rfind(v, pos); + } + + size_type rfind(const char* s, size_type pos, size_type count) const + { + return view_.rfind(s, pos, count); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type + find_first_of(T&& s, size_type pos = 0) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.find_first_of(v, pos); + } + + size_type find_first_of(const char* s, size_type pos, size_type count) const + { + return view_.find_first_of(s, pos, count); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type + find_first_not_of(T&& s, size_type pos = 0) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.find_first_not_of(v, pos); + } + + size_type find_first_not_of(const char* s, size_type pos, + size_type count) const + { + return view_.find_first_not_of(s, pos, count); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type + find_last_of(T&& s, size_type pos = npos) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.find_last_of(v, pos); + } + + size_type find_last_of(const char* s, size_type pos, size_type count) const + { + return view_.find_last_of(s, pos, count); + } + + template <typename T> + typename std::enable_if<AsStringView<T>::value, size_type>::type + find_last_not_of(T&& s, size_type pos = npos) const + { + string_view v = AsStringView<T>::view(std::forward<T>(s)); + return view_.find_last_not_of(v, pos); + } + + size_type find_last_not_of(const char* s, size_type pos, + size_type count) const + { + return view_.find_last_not_of(s, pos, count); + } + +private: + // Internal constructor to move from existing String. + String(String&& s, Private) noexcept + : String(std::move(s)) + { + } + + // Internal constructor for dynamically allocated string. + String(std::string&& s, Private); + + // Internal constructor for view of statically allocated string. + String(string_view v, Private) + : string_() + , view_(v) + { + } + + void internally_mutate_to_stable_string(); + + std::shared_ptr<std::string const> string_; + string_view view_; +}; + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator==(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) == + AsStringView<R>::view(std::forward<R>(r))); +} + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator!=(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) != + AsStringView<R>::view(std::forward<R>(r))); +} + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator<(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) < + AsStringView<R>::view(std::forward<R>(r))); +} + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator<=(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) <= + AsStringView<R>::view(std::forward<R>(r))); +} + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator>(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) > + AsStringView<R>::view(std::forward<R>(r))); +} + +template <typename L, typename R> +typename std::enable_if<AsStringView<L>::value && AsStringView<R>::value, + bool>::type +operator>=(L&& l, R&& r) +{ + return (AsStringView<L>::view(std::forward<L>(l)) >= + AsStringView<R>::view(std::forward<R>(r))); +} + +std::ostream& operator<<(std::ostream& os, String const& s); +std::string& operator+=(std::string& self, String const& s); + +template <typename L, typename R> +struct StringOpPlus +{ + L l; + R r; +#if defined(__SUNPRO_CC) + StringOpPlus(L in_l, R in_r) + : l(in_l) + , r(in_r) + { + } +#endif + operator std::string() const; + std::string::size_type size() const { return l.size() + r.size(); } +}; + +template <typename T> +struct StringAdd +{ + static const bool value = AsStringView<T>::value; + typedef string_view temp_type; + template <typename S> + static temp_type temp(S&& s) + { + return AsStringView<T>::view(std::forward<S>(s)); + } +}; + +template <typename L, typename R> +struct StringAdd<StringOpPlus<L, R>> : std::true_type +{ + typedef StringOpPlus<L, R> const& temp_type; + static temp_type temp(temp_type s) { return s; } +}; + +template <typename L, typename R> +StringOpPlus<L, R>::operator std::string() const +{ + std::string s; + s.reserve(size()); + s += *this; + return s; +} + +template <typename L, typename R> +std::string& operator+=(std::string& s, StringOpPlus<L, R> const& a) +{ + s.reserve(s.size() + a.size()); + s += a.l; + s += a.r; + return s; +} + +template <typename L, typename R> +String& operator+=(String& s, StringOpPlus<L, R> const& a) +{ + std::string r; + r.reserve(s.size() + a.size()); + r.assign(s.data(), s.size()); + r += a.l; + r += a.r; + s = std::move(r); + return s; +} + +template <typename L, typename R> +std::ostream& operator<<(std::ostream& os, StringOpPlus<L, R> const& a) +{ + return os << a.l << a.r; +} + +template <typename L, typename R> +struct IntoString<StringOpPlus<L, R>> : std::true_type +{ + static std::string into_string(StringOpPlus<L, R> const& a) { return a; } +}; + +template <typename L, typename R> +typename std::enable_if<StringAdd<L>::value && StringAdd<R>::value, + StringOpPlus<typename StringAdd<L>::temp_type, + typename StringAdd<R>::temp_type>>::type +operator+(L&& l, R&& r) +{ + return { StringAdd<L>::temp(std::forward<L>(l)), + StringAdd<R>::temp(std::forward<R>(r)) }; +} + +template <typename LL, typename LR, typename R> +typename std::enable_if<AsStringView<R>::value, bool>::type operator==( + StringOpPlus<LL, LR> const& l, R&& r) +{ + return std::string(l) == AsStringView<R>::view(std::forward<R>(r)); +} + +template <typename L, typename RL, typename RR> +typename std::enable_if<AsStringView<L>::value, bool>::type operator==( + L&& l, StringOpPlus<RL, RR> const& r) +{ + return AsStringView<L>::view(std::forward<L>(l)) == std::string(r); +} + +} // namespace cm + +namespace std { + +template <> +struct hash<cm::String> +{ + typedef cm::String argument_type; + typedef size_t result_type; + + result_type operator()(argument_type const& s) const noexcept + { + result_type const h(std::hash<cm::string_view>{}(s.view())); + return h; + } +}; +} + +#endif diff --git a/Source/cm_static_string_view.hxx b/Source/cm_static_string_view.hxx new file mode 100644 index 0000000..1bef0c6 --- /dev/null +++ b/Source/cm_static_string_view.hxx @@ -0,0 +1,41 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef cm_static_string_view_hxx +#define cm_static_string_view_hxx + +#include "cmConfigure.h" // IWYU pragma: keep + +#include "cm_string_view.hxx" + +#include <cstddef> + +namespace cm { + +/** A string_view that only binds to static storage. + * + * This is used together with the `""_s` user-defined literal operator + * to construct a type-safe abstraction of a string_view that only views + * statically allocated strings. These strings are const and available + * for the entire lifetime of the program. + */ +class static_string_view : public string_view +{ + static_string_view(string_view v) + : string_view(v) + { + } + + friend static_string_view operator"" _s(const char* data, size_t size); +}; + +/** Create a static_string_view using `""_s` literal syntax. */ +inline static_string_view operator"" _s(const char* data, size_t size) +{ + return string_view(data, size); +} + +} // namespace cm + +using cm::operator"" _s; + +#endif diff --git a/Source/cm_string_view.cxx b/Source/cm_string_view.cxx new file mode 100644 index 0000000..61fa80e --- /dev/null +++ b/Source/cm_string_view.cxx @@ -0,0 +1,301 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include "cm_string_view.hxx" + +#ifndef CMake_HAVE_CXX_STRING_VIEW + +# include "cm_kwiml.h" + +# include <algorithm> +# include <ostream> +# include <stdexcept> + +namespace cm { + +string_view::const_reference string_view::at(size_type pos) const +{ + if (pos >= size_) { + throw std::out_of_range("Index out of range in string_view::at"); + } + return data_[pos]; +} + +string_view::size_type string_view::copy(char* dest, size_type count, + size_type pos) const +{ + if (pos > size_) { + throw std::out_of_range("Index out of range in string_view::copy"); + } + size_type const rcount = std::min(count, size_ - pos); + traits_type::copy(dest, data_ + pos, rcount); + return rcount; +} + +string_view string_view::substr(size_type pos, size_type count) const +{ + if (pos > size_) { + throw std::out_of_range("Index out of range in string_view::substr"); + } + size_type const rcount = std::min(count, size_ - pos); + return string_view(data_ + pos, rcount); +} + +int string_view::compare(string_view v) const noexcept +{ + size_type const rlen = std::min(size_, v.size_); + int c = traits_type::compare(data_, v.data_, rlen); + if (c == 0) { + if (size_ < v.size_) { + c = -1; + } else if (size_ > v.size_) { + c = 1; + } + } + return c; +} + +int string_view::compare(size_type pos1, size_type count1, string_view v) const +{ + return substr(pos1, count1).compare(v); +} + +int string_view::compare(size_type pos1, size_type count1, string_view v, + size_type pos2, size_type count2) const +{ + return substr(pos1, count1).compare(v.substr(pos2, count2)); +} + +int string_view::compare(const char* s) const +{ + return compare(string_view(s)); +} + +int string_view::compare(size_type pos1, size_type count1, const char* s) const +{ + return substr(pos1, count1).compare(string_view(s)); +} + +int string_view::compare(size_type pos1, size_type count1, const char* s, + size_type count2) const +{ + return substr(pos1, count1).compare(string_view(s, count2)); +} + +string_view::size_type string_view::find(string_view v, size_type pos) const + noexcept +{ + for (; pos + v.size_ <= size_; ++pos) { + if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) == 0) { + return pos; + } + } + return npos; +} + +string_view::size_type string_view::find(char c, size_type pos) const noexcept +{ + return find(string_view(&c, 1), pos); +} + +string_view::size_type string_view::find(const char* s, size_type pos, + size_type count) const +{ + return find(string_view(s, count), pos); +} + +string_view::size_type string_view::find(const char* s, size_type pos) const +{ + return find(string_view(s), pos); +} + +string_view::size_type string_view::rfind(string_view v, size_type pos) const + noexcept +{ + if (size_ >= v.size_) { + for (pos = std::min(pos, size_ - v.size_) + 1; pos > 0;) { + --pos; + if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) == + 0) { + return pos; + } + } + } + return npos; +} + +string_view::size_type string_view::rfind(char c, size_type pos) const noexcept +{ + return rfind(string_view(&c, 1), pos); +} + +string_view::size_type string_view::rfind(const char* s, size_type pos, + size_type count) const +{ + return rfind(string_view(s, count), pos); +} + +string_view::size_type string_view::rfind(const char* s, size_type pos) const +{ + return rfind(string_view(s), pos); +} + +string_view::size_type string_view::find_first_of(string_view v, + size_type pos) const noexcept +{ + for (; pos < size_; ++pos) { + if (traits_type::find(v.data_, v.size_, data_[pos])) { + return pos; + } + } + return npos; +} + +string_view::size_type string_view::find_first_of(char c, size_type pos) const + noexcept +{ + return find_first_of(string_view(&c, 1), pos); +} + +string_view::size_type string_view::find_first_of(const char* s, size_type pos, + size_type count) const +{ + return find_first_of(string_view(s, count), pos); +} + +string_view::size_type string_view::find_first_of(const char* s, + size_type pos) const +{ + return find_first_of(string_view(s), pos); +} + +string_view::size_type string_view::find_last_of(string_view v, + size_type pos) const noexcept +{ + if (size_ > 0) { + for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) { + --pos; + if (traits_type::find(v.data_, v.size_, data_[pos])) { + return pos; + } + } + } + return npos; +} + +string_view::size_type string_view::find_last_of(char c, size_type pos) const + noexcept +{ + return find_last_of(string_view(&c, 1), pos); +} + +string_view::size_type string_view::find_last_of(const char* s, size_type pos, + size_type count) const +{ + return find_last_of(string_view(s, count), pos); +} + +string_view::size_type string_view::find_last_of(const char* s, + size_type pos) const +{ + return find_last_of(string_view(s), pos); +} + +string_view::size_type string_view::find_first_not_of(string_view v, + size_type pos) const + noexcept +{ + for (; pos < size_; ++pos) { + if (!traits_type::find(v.data_, v.size_, data_[pos])) { + return pos; + } + } + return npos; +} + +string_view::size_type string_view::find_first_not_of(char c, + size_type pos) const + noexcept +{ + return find_first_not_of(string_view(&c, 1), pos); +} + +string_view::size_type string_view::find_first_not_of(const char* s, + size_type pos, + size_type count) const +{ + return find_first_not_of(string_view(s, count), pos); +} + +string_view::size_type string_view::find_first_not_of(const char* s, + size_type pos) const +{ + return find_first_not_of(string_view(s), pos); +} + +string_view::size_type string_view::find_last_not_of(string_view v, + size_type pos) const + noexcept +{ + if (size_ > 0) { + for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) { + --pos; + if (!traits_type::find(v.data_, v.size_, data_[pos])) { + return pos; + } + } + } + return npos; +} + +string_view::size_type string_view::find_last_not_of(char c, + size_type pos) const + noexcept +{ + return find_last_not_of(string_view(&c, 1), pos); +} + +string_view::size_type string_view::find_last_not_of(const char* s, + size_type pos, + size_type count) const +{ + return find_last_not_of(string_view(s, count), pos); +} + +string_view::size_type string_view::find_last_not_of(const char* s, + size_type pos) const +{ + return find_last_not_of(string_view(s), pos); +} + +std::ostream& operator<<(std::ostream& o, string_view v) +{ + return o.write(v.data(), v.size()); +} + +std::string& operator+=(std::string& s, string_view v) +{ + s.append(v.data(), v.size()); + return s; +} +} + +std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()( + argument_type const& s) const noexcept +{ + // FNV-1a hash. + static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325; + static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3; + KWIML_INT_uint64_t h = fnv_offset_basis; + for (char const& c : s) { + h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c)); + h = h * fnv_prime; + } + return result_type(h); +} +#else +// Avoid empty translation unit. +void cm_string_view_cxx() +{ +} +#endif diff --git a/Source/cm_string_view.hxx b/Source/cm_string_view.hxx new file mode 100644 index 0000000..d368ed8 --- /dev/null +++ b/Source/cm_string_view.hxx @@ -0,0 +1,217 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef cm_string_view_hxx +#define cm_string_view_hxx + +#include "cmConfigure.h" // IWYU pragma: keep + +#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L +# define CMake_HAVE_CXX_STRING_VIEW +#endif + +#ifdef CMake_HAVE_CXX_STRING_VIEW +# include <string_view> +namespace cm { +using std::string_view; +} +#else +# include <cstddef> +# include <functional> +# include <iosfwd> +# include <iterator> +# include <string> + +namespace cm { + +class string_view +{ +public: + using traits_type = std::string::traits_type; + using value_type = char; + using pointer = char*; + using const_pointer = const char*; + using reference = char&; + using const_reference = char const&; + using const_iterator = const char*; + using iterator = const_iterator; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + using reverse_iterator = const_reverse_iterator; + using size_type = std::string::size_type; + using difference_type = std::string::difference_type; + + static size_type const npos = static_cast<size_type>(-1); + + string_view() noexcept = default; + string_view(string_view const&) noexcept = default; + + string_view(const char* s, size_t count) noexcept + : data_(s) + , size_(count) + { + } + + string_view(const char* s) noexcept + : data_(s) + , size_(traits_type::length(s)) + { + } + + // C++17 does not define this constructor. Instead it defines + // a conversion operator on std::string to create a string_view. + // Since this implementation is used in C++11, std::string does + // not have that conversion. + string_view(std::string const& s) noexcept + : data_(s.data()) + , size_(s.size()) + { + } + + // C++17 does not define this conversion. Instead it defines + // a constructor on std::string that can take a string_view. + // Since this implementation is used in C++11, std::string does + // not have that constructor. + explicit operator std::string() const { return std::string(data_, size_); } + + string_view& operator=(string_view const&) = default; + + const_iterator begin() const noexcept { return data_; } + const_iterator end() const noexcept { return data_ + size_; } + const_iterator cbegin() const noexcept { return begin(); } + const_iterator cend() const noexcept { return end(); } + + const_reverse_iterator rbegin() const noexcept + { + return const_reverse_iterator(end()); + } + const_reverse_iterator rend() const noexcept + { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crbegin() const noexcept { return rbegin(); } + const_reverse_iterator crend() const noexcept { return rend(); } + + const_reference operator[](size_type pos) const noexcept + { + return data_[pos]; + } + const_reference at(size_type pos) const; + const_reference front() const noexcept { return data_[0]; } + const_reference back() const noexcept { return data_[size_ - 1]; } + const_pointer data() const noexcept { return data_; } + + size_type size() const noexcept { return size_; } + size_type length() const noexcept { return size_; } + size_type max_size() const noexcept { return npos - 1; } + bool empty() const noexcept { return size_ == 0; } + + void remove_prefix(size_type n) noexcept + { + data_ += n; + size_ -= n; + } + void remove_suffix(size_type n) noexcept { size_ -= n; } + void swap(string_view& v) noexcept + { + string_view tmp = v; + v = *this; + *this = tmp; + } + + size_type copy(char* dest, size_type count, size_type pos = 0) const; + string_view substr(size_type pos = 0, size_type count = npos) const; + + int compare(string_view v) const noexcept; + int compare(size_type pos1, size_type count1, string_view v) const; + int compare(size_type pos1, size_type count1, string_view v, size_type pos2, + size_type count2) const; + int compare(const char* s) const; + int compare(size_type pos1, size_type count1, const char* s) const; + int compare(size_type pos1, size_type count1, const char* s, + size_type count2) const; + + size_type find(string_view v, size_type pos = 0) const noexcept; + size_type find(char c, size_type pos = 0) const noexcept; + size_type find(const char* s, size_type pos, size_type count) const; + size_type find(const char* s, size_type pos = 0) const; + + size_type rfind(string_view v, size_type pos = npos) const noexcept; + size_type rfind(char c, size_type pos = npos) const noexcept; + size_type rfind(const char* s, size_type pos, size_type count) const; + size_type rfind(const char* s, size_type pos = npos) const; + + size_type find_first_of(string_view v, size_type pos = 0) const noexcept; + size_type find_first_of(char c, size_type pos = 0) const noexcept; + size_type find_first_of(const char* s, size_type pos, size_type count) const; + size_type find_first_of(const char* s, size_type pos = 0) const; + + size_type find_last_of(string_view v, size_type pos = npos) const noexcept; + size_type find_last_of(char c, size_type pos = npos) const noexcept; + size_type find_last_of(const char* s, size_type pos, size_type count) const; + size_type find_last_of(const char* s, size_type pos = npos) const; + + size_type find_first_not_of(string_view v, size_type pos = 0) const noexcept; + size_type find_first_not_of(char c, size_type pos = 0) const noexcept; + size_type find_first_not_of(const char* s, size_type pos, + size_type count) const; + size_type find_first_not_of(const char* s, size_type pos = 0) const; + + size_type find_last_not_of(string_view v, size_type pos = npos) const + noexcept; + size_type find_last_not_of(char c, size_type pos = npos) const noexcept; + size_type find_last_not_of(const char* s, size_type pos, + size_type count) const; + size_type find_last_not_of(const char* s, size_type pos = npos) const; + +private: + const char* data_ = nullptr; + size_type size_ = 0; +}; + +std::ostream& operator<<(std::ostream& o, string_view v); + +std::string& operator+=(std::string& s, string_view v); + +inline bool operator==(string_view l, string_view r) noexcept +{ + return l.compare(r) == 0; +} + +inline bool operator!=(string_view l, string_view r) noexcept +{ + return l.compare(r) != 0; +} + +inline bool operator<(string_view l, string_view r) noexcept +{ + return l.compare(r) < 0; +} + +inline bool operator<=(string_view l, string_view r) noexcept +{ + return l.compare(r) <= 0; +} + +inline bool operator>(string_view l, string_view r) noexcept +{ + return l.compare(r) > 0; +} + +inline bool operator>=(string_view l, string_view r) noexcept +{ + return l.compare(r) >= 0; +} +} + +namespace std { + +template <> +struct hash<cm::string_view> +{ + typedef cm::string_view argument_type; + typedef size_t result_type; + result_type operator()(argument_type const& s) const noexcept; +}; +} + +#endif +#endif diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index 126076d..f6a9153 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories( set(CMakeLib_TESTS testGeneratedFileStream.cxx testRST.cxx + testString.cxx testSystemTools.cxx testUTF8.cxx testXMLParser.cxx diff --git a/Tests/CMakeLib/testString.cxx b/Tests/CMakeLib/testString.cxx new file mode 100644 index 0000000..2aa1459 --- /dev/null +++ b/Tests/CMakeLib/testString.cxx @@ -0,0 +1,1347 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include "cmString.hxx" + +#include "cm_static_string_view.hxx" +#include "cm_string_view.hxx" + +#include <cstring> +#include <iostream> +#include <iterator> +#include <sstream> +#include <stdexcept> +#include <string> +#include <type_traits> +#include <utility> + +#define ASSERT_TRUE(x) \ + if (!(x)) { \ + std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \ + return false; \ + } + +static bool testConstructDefault() +{ + std::cout << "testConstructDefault()\n"; + cm::String str; + cm::String const& str_const = str; + ASSERT_TRUE(bool(str_const) == false); + ASSERT_TRUE(str_const.data() == nullptr); + ASSERT_TRUE(str_const.size() == 0); + ASSERT_TRUE(str_const.empty()); + ASSERT_TRUE(str_const.is_stable()); + ASSERT_TRUE(str.c_str() == nullptr); + ASSERT_TRUE(str.str().empty()); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testFromNullPtr(cm::String str) +{ + cm::String const& str_const = str; + ASSERT_TRUE(bool(str_const) == false); + ASSERT_TRUE(str_const.data() == nullptr); + ASSERT_TRUE(str_const.size() == 0); + ASSERT_TRUE(str_const.empty()); + ASSERT_TRUE(str_const.is_stable()); + ASSERT_TRUE(str.c_str() == nullptr); + ASSERT_TRUE(str.str().empty()); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromNullPtr() +{ + std::cout << "testConstructFromNullPtr()\n"; + return testFromNullPtr(nullptr); +} + +static bool testAssignFromNullPtr() +{ + std::cout << "testAssignFromNullPtr()\n"; + cm::String str; + str = nullptr; + return testFromNullPtr(str); +} + +static bool testFromCStrNull(cm::String str) +{ + cm::String const& str_const = str; + ASSERT_TRUE(bool(str_const) == false); + ASSERT_TRUE(str_const.data() == nullptr); + ASSERT_TRUE(str_const.size() == 0); + ASSERT_TRUE(str_const.empty()); + ASSERT_TRUE(str_const.is_stable()); + ASSERT_TRUE(str.c_str() == nullptr); + ASSERT_TRUE(str.str().empty()); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromCStrNull() +{ + std::cout << "testConstructFromCStrNull()\n"; + const char* null = nullptr; + return testFromCStrNull(null); +} + +static bool testAssignFromCStrNull() +{ + std::cout << "testAssignFromCStrNull()\n"; + const char* null = nullptr; + cm::String str; + str = null; + return testFromCStrNull(str); +} + +static char const charArray[] = "abc"; + +static bool testFromCharArray(cm::String str) +{ + cm::String const& str_const = str; + ASSERT_TRUE(str_const.data() != charArray); + ASSERT_TRUE(str_const.size() == sizeof(charArray) - 1); + ASSERT_TRUE(str_const.is_stable()); + ASSERT_TRUE(str.c_str() != charArray); + ASSERT_TRUE(str.is_stable()); + cm::String substr = str.substr(1); + cm::String const& substr_const = substr; + ASSERT_TRUE(substr_const.data() != &charArray[1]); + ASSERT_TRUE(substr_const.size() == 2); + ASSERT_TRUE(!substr_const.is_stable()); + ASSERT_TRUE(substr.c_str() != &charArray[1]); + ASSERT_TRUE(!substr.is_stable()); + return true; +} + +static bool testConstructFromCharArray() +{ + std::cout << "testConstructFromCharArray()\n"; + return testFromCharArray(charArray); +} + +static bool testAssignFromCharArray() +{ + std::cout << "testAssignFromCharArray()\n"; + cm::String str; + str = charArray; + return testFromCharArray(str); +} + +static const char* cstr = "abc"; + +static bool testFromCStr(cm::String const& str) +{ + ASSERT_TRUE(str.data() != cstr); + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), cstr, 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromCStr() +{ + std::cout << "testConstructFromCStr()\n"; + return testFromCStr(cstr); + ; +} + +static bool testAssignFromCStr() +{ + std::cout << "testAssignFromCStr()\n"; + cm::String str; + str = cstr; + return testFromCStr(str); + ; +} + +static const std::string stdstr = "abc"; + +static bool testFromStdString(cm::String const& str) +{ +#if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI + // It would be nice to check this everywhere, but several platforms + // still use a CoW implementation even in C++11. + ASSERT_TRUE(str.data() != stdstr.data()); +#endif + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), stdstr.data(), 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromStdString() +{ + std::cout << "testConstructFromStdString()\n"; + return testFromStdString(stdstr); +} + +static bool testAssignFromStdString() +{ + std::cout << "testAssignFromStdString()\n"; + cm::String str; + str = stdstr; + return testFromStdString(str); +} + +static bool testConstructFromView() +{ + std::cout << "testConstructFromView()\n"; + cm::string_view view = cstr; + return testFromCStr(view); +} + +static bool testAssignFromView() +{ + std::cout << "testAssignFromView()\n"; + cm::string_view view = cstr; + cm::String str; + str = view; + return testFromCStr(str); +} + +static bool testFromChar(cm::String const& str) +{ + ASSERT_TRUE(str.size() == 1); + ASSERT_TRUE(std::strncmp(str.data(), "a", 1) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromChar() +{ + std::cout << "testConstructFromChar()\n"; + return testFromChar('a'); +} + +static bool testAssignFromChar() +{ + std::cout << "testAssignFromChar()\n"; + cm::String str; + str = 'a'; + return testFromChar(str); +} + +static bool testConstructFromInitList() +{ + std::cout << "testConstructFromInitList()\n"; + cm::String const str{ 'a', 'b', 'c' }; + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), "abc", 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testAssignFromInitList() +{ + std::cout << "testAssignFromInitList()\n"; + cm::String str; + str = { 'a', 'b', 'c' }; + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), "abc", 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromBuffer() +{ + std::cout << "testConstructFromBuffer()\n"; + cm::String const str(cstr, 3); + return testFromCStr(str); +} + +static bool testConstructFromInputIterator() +{ + std::cout << "testConstructFromInputIterator()\n"; + cm::String const str(cstr, cstr + 3); + ASSERT_TRUE(str.data() != cstr); + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), cstr, 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testConstructFromN() +{ + std::cout << "testConstructFromN()\n"; + cm::String const str(3, 'a'); + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), "aaa", 3) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static const auto staticStringView = "abc"_s; + +static bool testFromStaticStringView(cm::String str) +{ + cm::String const& str_const = str; + ASSERT_TRUE(str_const.data() == staticStringView.data()); + ASSERT_TRUE(str_const.size() == staticStringView.size()); + ASSERT_TRUE(!str_const.is_stable()); + ASSERT_TRUE(str.c_str() == staticStringView); + ASSERT_TRUE(!str.is_stable()); + cm::String substr = str.substr(1); + cm::String const& substr_const = substr; + ASSERT_TRUE(substr_const.data() == &staticStringView[1]); + ASSERT_TRUE(substr_const.size() == 2); + ASSERT_TRUE(!substr_const.is_stable()); + ASSERT_TRUE(substr.c_str() == &staticStringView[1]); + ASSERT_TRUE(!substr.is_stable()); + return true; +} + +static bool testConstructFromStaticStringView() +{ + std::cout << "testConstructFromStaticStringView()\n"; + return testFromStaticStringView(staticStringView); +} + +static bool testAssignFromStaticStringView() +{ + std::cout << "testAssignFromStaticStringView()\n"; + cm::String str; + str = staticStringView; + return testFromStaticStringView(str); +} + +static bool testConstructCopy() +{ + std::cout << "testConstructCopy()\n"; + cm::String s1 = std::string("abc"); + cm::String s2 = s1; + ASSERT_TRUE(s1.data() == s2.data()); + ASSERT_TRUE(s1.size() == 3); + ASSERT_TRUE(s2.size() == 3); + ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0); + ASSERT_TRUE(s1.is_stable()); + ASSERT_TRUE(s2.is_stable()); + return true; +} + +static bool testConstructMove() +{ + std::cout << "testConstructMove()\n"; + cm::String s1 = std::string("abc"); + cm::String s2 = std::move(s1); + ASSERT_TRUE(s1.data() == nullptr); + ASSERT_TRUE(s1.size() == 0); + ASSERT_TRUE(s2.size() == 3); + ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0); + ASSERT_TRUE(s1.is_stable()); + ASSERT_TRUE(s2.is_stable()); + return true; +} + +static bool testAssignCopy() +{ + std::cout << "testAssignCopy()\n"; + cm::String s1 = std::string("abc"); + cm::String s2; + s2 = s1; + ASSERT_TRUE(s1.data() == s2.data()); + ASSERT_TRUE(s1.size() == 3); + ASSERT_TRUE(s2.size() == 3); + ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0); + ASSERT_TRUE(s1.is_stable()); + ASSERT_TRUE(s2.is_stable()); + return true; +} + +static bool testAssignMove() +{ + std::cout << "testAssignMove()\n"; + cm::String s1 = std::string("abc"); + cm::String s2; + s2 = std::move(s1); + ASSERT_TRUE(s1.data() == nullptr); + ASSERT_TRUE(s1.size() == 0); + ASSERT_TRUE(s2.size() == 3); + ASSERT_TRUE(std::strncmp(s2.data(), "abc", 3) == 0); + ASSERT_TRUE(s1.is_stable()); + ASSERT_TRUE(s2.is_stable()); + return true; +} + +static bool testOperatorBool() +{ + std::cout << "testOperatorBool()\n"; + cm::String str; + ASSERT_TRUE(!str); + str = ""; + ASSERT_TRUE(str); + str = static_cast<const char*>(nullptr); + ASSERT_TRUE(!str); + str = std::string(); + ASSERT_TRUE(str); + str = nullptr; + ASSERT_TRUE(!str); + return true; +} + +static bool testOperatorIndex() +{ + std::cout << "testOperatorIndex()\n"; + cm::String str = "abc"; + ASSERT_TRUE(str[0] == 'a'); + ASSERT_TRUE(str[1] == 'b'); + ASSERT_TRUE(str[2] == 'c'); + return true; +} + +static bool testOperatorPlusEqual() +{ + std::cout << "testOperatorPlusEqual()\n"; + cm::String str = "a"; + str += "b"; + { + const char* c = "c"; + str += c; + } + str += 'd'; + str += std::string("e"); + str += cm::string_view("f", 1); + str += cm::String("g"); + ASSERT_TRUE(str.size() == 7); + ASSERT_TRUE(std::strncmp(str.data(), "abcdefg", 7) == 0); + ASSERT_TRUE(str.is_stable()); + return true; +} + +static bool testOperatorCompare() +{ + std::cout << "testOperatorCompare()\n"; + cm::String str = "b"; + { + ASSERT_TRUE(str == "b"); + ASSERT_TRUE("b" == str); + ASSERT_TRUE(str != "a"); + ASSERT_TRUE("a" != str); + ASSERT_TRUE(str < "c"); + ASSERT_TRUE("a" < str); + ASSERT_TRUE(str > "a"); + ASSERT_TRUE("c" > str); + ASSERT_TRUE(str <= "b"); + ASSERT_TRUE("b" <= str); + ASSERT_TRUE(str >= "b"); + ASSERT_TRUE("b" >= str); + } + { + const char* a = "a"; + const char* b = "b"; + const char* c = "c"; + ASSERT_TRUE(str == b); + ASSERT_TRUE(b == str); + ASSERT_TRUE(str != a); + ASSERT_TRUE(a != str); + ASSERT_TRUE(str < c); + ASSERT_TRUE(a < str); + ASSERT_TRUE(str > a); + ASSERT_TRUE(c > str); + ASSERT_TRUE(str <= b); + ASSERT_TRUE(b <= str); + ASSERT_TRUE(str >= b); + ASSERT_TRUE(b >= str); + } + { + ASSERT_TRUE(str == 'b'); + ASSERT_TRUE('b' == str); + ASSERT_TRUE(str != 'a'); + ASSERT_TRUE('a' != str); + ASSERT_TRUE(str < 'c'); + ASSERT_TRUE('a' < str); + ASSERT_TRUE(str > 'a'); + ASSERT_TRUE('c' > str); + ASSERT_TRUE(str <= 'b'); + ASSERT_TRUE('b' <= str); + ASSERT_TRUE(str >= 'b'); + ASSERT_TRUE('b' >= str); + } + { + std::string const a = "a"; + std::string const b = "b"; + std::string const c = "c"; + ASSERT_TRUE(str == b); + ASSERT_TRUE(b == str); + ASSERT_TRUE(str != a); + ASSERT_TRUE(a != str); + ASSERT_TRUE(str < c); + ASSERT_TRUE(a < str); + ASSERT_TRUE(str > a); + ASSERT_TRUE(c > str); + ASSERT_TRUE(str <= b); + ASSERT_TRUE(b <= str); + ASSERT_TRUE(str >= b); + ASSERT_TRUE(b >= str); + } + { + cm::string_view const a("a", 1); + cm::string_view const b("b", 1); + cm::string_view const c("c", 1); + ASSERT_TRUE(str == b); + ASSERT_TRUE(b == str); + ASSERT_TRUE(str != a); + ASSERT_TRUE(a != str); + ASSERT_TRUE(str < c); + ASSERT_TRUE(a < str); + ASSERT_TRUE(str > a); + ASSERT_TRUE(c > str); + ASSERT_TRUE(str <= b); + ASSERT_TRUE(b <= str); + ASSERT_TRUE(str >= b); + ASSERT_TRUE(b >= str); + } + { + cm::String const a("a"); + cm::String const b("b"); + cm::String const c("c"); + ASSERT_TRUE(str == b); + ASSERT_TRUE(b == str); + ASSERT_TRUE(str != a); + ASSERT_TRUE(a != str); + ASSERT_TRUE(str < c); + ASSERT_TRUE(a < str); + ASSERT_TRUE(str > a); + ASSERT_TRUE(c > str); + ASSERT_TRUE(str <= b); + ASSERT_TRUE(b <= str); + ASSERT_TRUE(str >= b); + ASSERT_TRUE(b >= str); + } + return true; +} + +static bool testOperatorStream() +{ + std::cout << "testOperatorStream()\n"; + std::ostringstream ss; + ss << "a" << cm::String("b") << 'c'; + ASSERT_TRUE(ss.str() == "abc"); + return true; +} + +static bool testOperatorStdStringPlusEqual() +{ + std::cout << "testOperatorStdStringPlusEqual()\n"; + std::string s = "a"; + s += cm::String("b"); + ASSERT_TRUE(s == "ab"); + return true; +} + +static bool testMethod_borrow() +{ + std::cout << "testMethod_borrow()\n"; + std::string s = "abc"; + cm::String str = cm::String::borrow(s); + ASSERT_TRUE(str.data() == s.data()); + ASSERT_TRUE(str.size() == s.size()); + ASSERT_TRUE(str == s); + return true; +} + +static bool testMethod_view() +{ + std::cout << "testMethod_view()\n"; + cm::String str; + ASSERT_TRUE(str.view().data() == nullptr); + ASSERT_TRUE(str.view().size() == 0); + str = charArray; + ASSERT_TRUE(str.view().data() != charArray); + ASSERT_TRUE(str.view().size() == sizeof(charArray) - 1); + str = std::string("abc"); + ASSERT_TRUE(str.view().size() == 3); + ASSERT_TRUE(strncmp(str.view().data(), "abc", 3) == 0); + return true; +} + +static bool testMethod_empty() +{ + std::cout << "testMethod_empty()\n"; + cm::String str; + ASSERT_TRUE(str.empty()); + str = ""; + ASSERT_TRUE(str.empty()); + str = "abc"; + ASSERT_TRUE(!str.empty()); + str = std::string(); + ASSERT_TRUE(str.empty()); + str = std::string("abc"); + ASSERT_TRUE(!str.empty()); + return true; +} + +static bool testMethod_length() +{ + std::cout << "testMethod_length()\n"; + cm::String str; + ASSERT_TRUE(str.length() == 0); + str = ""; + ASSERT_TRUE(str.length() == 0); + str = "abc"; + ASSERT_TRUE(str.length() == 3); + str = std::string(); + ASSERT_TRUE(str.length() == 0); + str = std::string("abc"); + ASSERT_TRUE(str.length() == 3); + return true; +} + +static bool testMethod_at() +{ + std::cout << "testMethod_at()\n"; + cm::String str = "abc"; + ASSERT_TRUE(str.at(0) == 'a'); + ASSERT_TRUE(str.at(1) == 'b'); + ASSERT_TRUE(str.at(2) == 'c'); + bool except_out_of_range = false; + try { + str.at(3); + } catch (std::out_of_range&) { + except_out_of_range = true; + } + ASSERT_TRUE(except_out_of_range); + return true; +} + +static bool testMethod_front_back() +{ + std::cout << "testMethod_front_back()\n"; + cm::String str = "abc"; + ASSERT_TRUE(str.front() == 'a'); + ASSERT_TRUE(str.back() == 'c'); + return true; +} + +static bool testMethodIterators() +{ + std::cout << "testMethodIterators()\n"; + cm::String str = "abc"; + ASSERT_TRUE(*str.begin() == 'a'); + ASSERT_TRUE(*(str.end() - 1) == 'c'); + ASSERT_TRUE(str.end() - str.begin() == 3); + ASSERT_TRUE(*str.cbegin() == 'a'); + ASSERT_TRUE(*(str.cend() - 1) == 'c'); + ASSERT_TRUE(str.cend() - str.cbegin() == 3); + ASSERT_TRUE(*str.rbegin() == 'c'); + ASSERT_TRUE(*(str.rend() - 1) == 'a'); + ASSERT_TRUE(str.rend() - str.rbegin() == 3); + ASSERT_TRUE(*str.crbegin() == 'c'); + ASSERT_TRUE(*(str.crend() - 1) == 'a'); + ASSERT_TRUE(str.crend() - str.crbegin() == 3); + return true; +} + +static bool testMethod_clear() +{ + std::cout << "testMethod_clear()\n"; + cm::String str = "abc"; + ASSERT_TRUE(!str.empty()); + str.clear(); + ASSERT_TRUE(str.empty()); + return true; +} + +static bool testMethod_insert() +{ + std::cout << "testMethod_insert()\n"; + cm::String str = "abc"; + str.insert(1, 2, 'd').insert(0, 1, '_'); + ASSERT_TRUE(str.size() == 6); + ASSERT_TRUE(std::strncmp(str.data(), "_addbc", 6) == 0); + return true; +} + +static bool testMethod_erase() +{ + std::cout << "testMethod_erase()\n"; + cm::String str = "abcdefg"; + str.erase(5).erase(1, 2); + ASSERT_TRUE(str.size() == 3); + ASSERT_TRUE(std::strncmp(str.data(), "ade", 3) == 0); + return true; +} + +static bool testMethod_push_back() +{ + std::cout << "testMethod_push_back()\n"; + cm::String str = "abc"; + str.push_back('d'); + ASSERT_TRUE(str == "abcd"); + return true; +} + +static bool testMethod_pop_back() +{ + std::cout << "testMethod_pop_back()\n"; + cm::String str = "abc"; + str.pop_back(); + ASSERT_TRUE(str == "ab"); + return true; +} + +static bool testMethod_replace() +{ + std::cout << "testMethod_replace()\n"; + { + cm::String str = "abcd"; + const char* bc = "bc"; + ASSERT_TRUE(str.replace(1, 2, "BC") == "aBCd"); + ASSERT_TRUE(str.replace(1, 2, bc) == "abcd"); + ASSERT_TRUE(str.replace(1, 2, 'x') == "axd"); + ASSERT_TRUE(str.replace(1, 1, std::string("bc")) == "abcd"); + ASSERT_TRUE(str.replace(1, 2, cm::string_view("BC", 2)) == "aBCd"); + ASSERT_TRUE(str.replace(1, 2, cm::String("bc")) == "abcd"); + } + { + cm::String str = "abcd"; + const char* bc = "bc"; + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, "BC") == "aBCd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, bc) == "abcd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 'x') == "axd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 2, + std::string("bc")) == "abcd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, + cm::string_view("BC", 2)) == "aBCd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, + cm::String("bc")) == "abcd"); + } + { + cm::String str = "abcd"; + const char* bc = "_bc"; + ASSERT_TRUE(str.replace(1, 2, "_BC_", 1, 2) == "aBCd"); + ASSERT_TRUE(str.replace(1, 2, bc, 1) == "abcd"); + ASSERT_TRUE(str.replace(1, 2, 'x', 0) == "axd"); + ASSERT_TRUE(str.replace(1, 1, std::string("_bc_"), 1, 2) == "abcd"); + ASSERT_TRUE(str.replace(1, 2, cm::string_view("_BC", 3), 1) == "aBCd"); + ASSERT_TRUE(str.replace(1, 2, cm::String("_bc_"), 1, 2) == "abcd"); + } + { + cm::String str = "abcd"; + const char* bc = "_bc_"; + ASSERT_TRUE(str.replace(1, 2, 2, 'x') == "axxd"); + ASSERT_TRUE(str.replace(str.begin() + 1, str.begin() + 3, 2, 'y') == + "ayyd"); + ASSERT_TRUE( + str.replace(str.begin() + 1, str.begin() + 3, bc + 1, bc + 3) == "abcd"); + } + return true; +} + +static bool testMethod_copy() +{ + std::cout << "testMethod_copy()\n"; + cm::String str = "abc"; + char dest[2]; + cm::String::size_type n = str.copy(dest, 2, 1); + ASSERT_TRUE(n == 2); + ASSERT_TRUE(std::strncmp(dest, "bc", 2) == 0); + n = str.copy(dest, 2); + ASSERT_TRUE(n == 2); + ASSERT_TRUE(std::strncmp(dest, "ab", 2) == 0); + return true; +} + +static bool testMethod_resize() +{ + std::cout << "testMethod_resize()\n"; + cm::String str = "abc"; + str.resize(3); + ASSERT_TRUE(str == "abc"); + str.resize(2); + ASSERT_TRUE(str == "ab"); + str.resize(3, 'c'); + ASSERT_TRUE(str == "abc"); + return true; +} + +static bool testMethod_swap() +{ + std::cout << "testMethod_swap()\n"; + cm::String str1 = std::string("1"); + cm::String str2 = std::string("2"); + str1.swap(str2); + ASSERT_TRUE(str1 == "2"); + ASSERT_TRUE(str2 == "1"); + return true; +} + +static bool testMethod_substr_AtEnd(cm::String str) +{ + cm::String substr = str.substr(1); + ASSERT_TRUE(substr.data() == str.data() + 1); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(!substr.is_stable()); + + // c_str() at the end of the buffer does not internally mutate. + ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0); + ASSERT_TRUE(substr.c_str() == str.data() + 1); + ASSERT_TRUE(substr.data() == str.data() + 1); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(!substr.is_stable()); + + // str() internally mutates. + ASSERT_TRUE(substr.str() == "bc"); + ASSERT_TRUE(substr.is_stable()); + ASSERT_TRUE(substr.data() != str.data() + 1); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(substr.c_str() != str.data() + 1); + ASSERT_TRUE(std::strcmp(substr.c_str(), "bc") == 0); + return true; +} + +static bool testMethod_substr_AtEndBorrowed() +{ + std::cout << "testMethod_substr_AtEndBorrowed()\n"; + return testMethod_substr_AtEnd("abc"_s); +} + +static bool testMethod_substr_AtEndOwned() +{ + std::cout << "testMethod_substr_AtEndOwned()\n"; + return testMethod_substr_AtEnd(std::string("abc")); +} + +static bool testMethod_substr_AtStart(cm::String str) +{ + { + cm::String substr = str.substr(0, 2); + ASSERT_TRUE(substr.data() == str.data()); + ASSERT_TRUE(substr.size() == 2); + + // c_str() not at the end of the buffer internally mutates. + const char* substr_c = substr.c_str(); + ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0); + ASSERT_TRUE(substr_c != str.data()); + ASSERT_TRUE(substr.data() != str.data()); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(substr.is_stable()); + + // str() does not need to internally mutate after c_str() did so + ASSERT_TRUE(substr.str() == "ab"); + ASSERT_TRUE(substr.is_stable()); + ASSERT_TRUE(substr.data() == substr_c); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(substr.c_str() == substr_c); + } + + { + cm::String substr = str.substr(0, 2); + ASSERT_TRUE(substr.data() == str.data()); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(!substr.is_stable()); + + // str() internally mutates. + ASSERT_TRUE(substr.str() == "ab"); + ASSERT_TRUE(substr.is_stable()); + ASSERT_TRUE(substr.data() != str.data()); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(substr.c_str() != str.data()); + + // c_str() does not internally after str() did so + const char* substr_c = substr.c_str(); + ASSERT_TRUE(std::strcmp(substr_c, "ab") == 0); + ASSERT_TRUE(substr_c == substr.data()); + ASSERT_TRUE(substr.size() == 2); + ASSERT_TRUE(substr.is_stable()); + } + + return true; +} + +static bool testMethod_substr_AtStartBorrowed() +{ + std::cout << "testMethod_substr_AtStartBorrowed()\n"; + return testMethod_substr_AtStart("abc"_s); +} + +static bool testMethod_substr_AtStartOwned() +{ + std::cout << "testMethod_substr_AtStartOwned()\n"; + return testMethod_substr_AtStart(std::string("abc")); +} + +static bool testMethod_compare() +{ + std::cout << "testMethod_compare()\n"; + cm::String str = "b"; + ASSERT_TRUE(str.compare("a") > 0); + ASSERT_TRUE(str.compare("b") == 0); + ASSERT_TRUE(str.compare("c") < 0); + { + const char* a = "a"; + const char* b = "b"; + const char* c = "c"; + ASSERT_TRUE(str.compare(a) > 0); + ASSERT_TRUE(str.compare(b) == 0); + ASSERT_TRUE(str.compare(c) < 0); + } + ASSERT_TRUE(str.compare('a') > 0); + ASSERT_TRUE(str.compare('b') == 0); + ASSERT_TRUE(str.compare('c') < 0); + ASSERT_TRUE(str.compare(std::string("a")) > 0); + ASSERT_TRUE(str.compare(std::string("b")) == 0); + ASSERT_TRUE(str.compare(std::string("c")) < 0); + ASSERT_TRUE(str.compare(cm::string_view("a_", 1)) > 0); + ASSERT_TRUE(str.compare(cm::string_view("b_", 1)) == 0); + ASSERT_TRUE(str.compare(cm::string_view("c_", 1)) < 0); + ASSERT_TRUE(str.compare(cm::String("a")) > 0); + ASSERT_TRUE(str.compare(cm::String("b")) == 0); + ASSERT_TRUE(str.compare(cm::String("c")) < 0); + ASSERT_TRUE(str.compare(0, 1, cm::string_view("a", 1)) > 0); + ASSERT_TRUE(str.compare(1, 0, cm::string_view("", 0)) == 0); + ASSERT_TRUE(str.compare(0, 1, cm::string_view("ac", 2), 1, 1) < 0); + ASSERT_TRUE(str.compare(1, 0, "") == 0); + ASSERT_TRUE(str.compare(1, 0, "_", 0) == 0); + return true; +} + +static bool testMethod_find() +{ + std::cout << "testMethod_find()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.find("a") == 0); + ASSERT_TRUE(str.find("a", 1) == 3); + { + const char* a = "a"; + ASSERT_TRUE(str.find(a) == 0); + ASSERT_TRUE(str.find(a, 1) == 3); + } + ASSERT_TRUE(str.find('a') == 0); + ASSERT_TRUE(str.find('a', 1) == 3); + ASSERT_TRUE(str.find(std::string("a")) == 0); + ASSERT_TRUE(str.find(std::string("a"), 1) == 3); + ASSERT_TRUE(str.find(cm::string_view("a_", 1)) == 0); + ASSERT_TRUE(str.find(cm::string_view("a_", 1), 1) == 3); + ASSERT_TRUE(str.find(cm::String("a")) == 0); + ASSERT_TRUE(str.find(cm::String("a"), 1) == 3); + ASSERT_TRUE(str.find("ab_", 1, 2) == 3); + return true; +} + +static bool testMethod_rfind() +{ + std::cout << "testMethod_rfind()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.rfind("a") == 3); + ASSERT_TRUE(str.rfind("a", 1) == 0); + { + const char* a = "a"; + ASSERT_TRUE(str.rfind(a) == 3); + ASSERT_TRUE(str.rfind(a, 1) == 0); + } + ASSERT_TRUE(str.rfind('a') == 3); + ASSERT_TRUE(str.rfind('a', 1) == 0); + ASSERT_TRUE(str.rfind(std::string("a")) == 3); + ASSERT_TRUE(str.rfind(std::string("a"), 1) == 0); + ASSERT_TRUE(str.rfind(cm::string_view("a_", 1)) == 3); + ASSERT_TRUE(str.rfind(cm::string_view("a_", 1), 1) == 0); + ASSERT_TRUE(str.rfind(cm::String("a")) == 3); + ASSERT_TRUE(str.rfind(cm::String("a"), 1) == 0); + ASSERT_TRUE(str.rfind("ab_", 1, 2) == 0); + return true; +} + +static bool testMethod_find_first_of() +{ + std::cout << "testMethod_find_first_of()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.find_first_of("_a") == 0); + ASSERT_TRUE(str.find_first_of("_a", 1) == 3); + { + const char* a = "_a"; + ASSERT_TRUE(str.find_first_of(a) == 0); + ASSERT_TRUE(str.find_first_of(a, 1) == 3); + } + ASSERT_TRUE(str.find_first_of('a') == 0); + ASSERT_TRUE(str.find_first_of('a', 1) == 3); + ASSERT_TRUE(str.find_first_of(std::string("_a")) == 0); + ASSERT_TRUE(str.find_first_of(std::string("_a"), 1) == 3); + ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1)) == 1); + ASSERT_TRUE(str.find_first_of(cm::string_view("ba_", 1), 2) == 4); + ASSERT_TRUE(str.find_first_of(cm::String("ab")) == 0); + ASSERT_TRUE(str.find_first_of(cm::String("ab"), 2) == 3); + ASSERT_TRUE(str.find_first_of("_ab", 1, 2) == 3); + return true; +} + +static bool testMethod_find_first_not_of() +{ + std::cout << "testMethod_find_first_not_of()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.find_first_not_of("_a") == 1); + ASSERT_TRUE(str.find_first_not_of("_a", 2) == 2); + { + const char* a = "_a"; + ASSERT_TRUE(str.find_first_not_of(a) == 1); + ASSERT_TRUE(str.find_first_not_of(a, 2) == 2); + } + ASSERT_TRUE(str.find_first_not_of('a') == 1); + ASSERT_TRUE(str.find_first_not_of('a', 2) == 2); + ASSERT_TRUE(str.find_first_not_of(std::string("_a")) == 1); + ASSERT_TRUE(str.find_first_not_of(std::string("_a"), 2) == 2); + ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1)) == 0); + ASSERT_TRUE(str.find_first_not_of(cm::string_view("ba_", 1), 1) == 2); + ASSERT_TRUE(str.find_first_not_of(cm::String("_a")) == 1); + ASSERT_TRUE(str.find_first_not_of(cm::String("_a"), 2) == 2); + ASSERT_TRUE(str.find_first_not_of("_bca", 1, 3) == 3); + return true; +} + +static bool testMethod_find_last_of() +{ + std::cout << "testMethod_find_last_of()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.find_last_of("_a") == 3); + ASSERT_TRUE(str.find_last_of("_a", 1) == 0); + { + const char* a = "_a"; + ASSERT_TRUE(str.find_last_of(a) == 3); + ASSERT_TRUE(str.find_last_of(a, 1) == 0); + } + ASSERT_TRUE(str.find_last_of('a') == 3); + ASSERT_TRUE(str.find_last_of('a', 1) == 0); + ASSERT_TRUE(str.find_last_of(std::string("_a")) == 3); + ASSERT_TRUE(str.find_last_of(std::string("_a"), 1) == 0); + ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1)) == 4); + ASSERT_TRUE(str.find_last_of(cm::string_view("ba_", 1), 2) == 1); + ASSERT_TRUE(str.find_last_of(cm::String("ab")) == 4); + ASSERT_TRUE(str.find_last_of(cm::String("ab"), 2) == 1); + ASSERT_TRUE(str.find_last_of("_ab", 1, 2) == 0); + return true; +} + +static bool testMethod_find_last_not_of() +{ + std::cout << "testMethod_find_last_not_of()\n"; + cm::String str = "abcabc"; + ASSERT_TRUE(str.find_last_not_of("_a") == 5); + ASSERT_TRUE(str.find_last_not_of("_a", 1) == 1); + { + const char* a = "_a"; + ASSERT_TRUE(str.find_last_not_of(a) == 5); + ASSERT_TRUE(str.find_last_not_of(a, 1) == 1); + } + ASSERT_TRUE(str.find_last_not_of('a') == 5); + ASSERT_TRUE(str.find_last_not_of('a', 1) == 1); + ASSERT_TRUE(str.find_last_not_of(std::string("_a")) == 5); + ASSERT_TRUE(str.find_last_not_of(std::string("_a"), 1) == 1); + ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1)) == 4); + ASSERT_TRUE(str.find_last_not_of(cm::string_view("cb_", 1), 2) == 1); + ASSERT_TRUE(str.find_last_not_of(cm::String("_a")) == 5); + ASSERT_TRUE(str.find_last_not_of(cm::String("_a"), 1) == 1); + ASSERT_TRUE(str.find_last_not_of("cb_", 2, 2) == 0); + return true; +} + +static bool testAddition() +{ + std::cout << "testAddition()\n"; + { + ASSERT_TRUE(cm::String("a") + "b" == "ab"); + ASSERT_TRUE("ab" == "a" + cm::String("b")); + ASSERT_TRUE("a" + cm::String("b") + "c" == "abc"); + ASSERT_TRUE("abc" == "a" + cm::String("b") + "c"); + ASSERT_TRUE("a" + (cm::String("b") + "c") + "d" == "abcd"); + ASSERT_TRUE("abcd" == "a" + (cm::String("b") + "c") + "d"); + } + { + ASSERT_TRUE(cm::String("a"_s) + "b"_s == "ab"_s); + ASSERT_TRUE("ab"_s == "a"_s + cm::String("b"_s)); + ASSERT_TRUE("a"_s + cm::String("b"_s) + "c"_s == "abc"_s); + ASSERT_TRUE("abc"_s == "a"_s + cm::String("b"_s) + "c"_s); + ASSERT_TRUE("a"_s + (cm::String("b"_s) + "c"_s) + "d"_s == "abcd"_s); + ASSERT_TRUE("abcd"_s == "a"_s + (cm::String("b"_s) + "c"_s) + "d"_s); + } + { + const char* a = "a"; + const char* b = "b"; + const char* ab = "ab"; + ASSERT_TRUE(cm::String(a) + b == ab); + ASSERT_TRUE(ab == a + cm::String(b)); + const char* c = "c"; + const char* abc = "abc"; + ASSERT_TRUE(a + cm::String(b) + c == abc); + ASSERT_TRUE(abc == a + cm::String(b) + c); + const char* d = "d"; + const char* abcd = "abcd"; + ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd); + ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d); + } + { + ASSERT_TRUE(cm::String('a') + 'b' == "ab"); + ASSERT_TRUE("ab" == 'a' + cm::String('b')); + ASSERT_TRUE('a' + cm::String('b') + 'c' == "abc"); + ASSERT_TRUE("abc" == 'a' + cm::String('b') + 'c'); + ASSERT_TRUE('a' + (cm::String('b') + 'c') + 'd' == "abcd"); + ASSERT_TRUE("abcd" == 'a' + (cm::String('b') + 'c') + 'd'); + } + { + std::string a = "a"; + std::string b = "b"; + std::string ab = "ab"; + ASSERT_TRUE(cm::String(a) + b == ab); + ASSERT_TRUE(ab == a + cm::String(b)); + std::string c = "c"; + std::string abc = "abc"; + ASSERT_TRUE(a + cm::String(b) + c == abc); + ASSERT_TRUE(abc == a + cm::String(b) + c); + std::string d = "d"; + std::string abcd = "abcd"; + ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd); + ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d); + } + { + cm::string_view a("a", 1); + cm::string_view b("b", 1); + cm::string_view ab("ab", 2); + ASSERT_TRUE(cm::String(a) + b == ab); + ASSERT_TRUE(ab == a + cm::String(b)); + cm::string_view c("c", 1); + cm::string_view abc("abc", 3); + ASSERT_TRUE(a + cm::String(b) + c == abc); + ASSERT_TRUE(abc == a + cm::String(b) + c); + cm::string_view d("d", 1); + cm::string_view abcd("abcd", 4); + ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd); + ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d); + } + { + cm::String a = "a"; + cm::String b = "b"; + cm::String ab = "ab"; + ASSERT_TRUE(a + b == ab); + ASSERT_TRUE(ab == a + b); + cm::String c = "c"; + cm::String abc = "abc"; + ASSERT_TRUE(a + cm::String(b) + c == abc); + ASSERT_TRUE(abc == a + cm::String(b) + c); + cm::String d = "d"; + cm::String abcd = "abcd"; + ASSERT_TRUE(a + (cm::String(b) + c) + d == abcd); + ASSERT_TRUE(abcd == a + (cm::String(b) + c) + d); + } + { + cm::String str; + str += "a" + cm::String("b") + 'c'; + ASSERT_TRUE(str == "abc"); + ASSERT_TRUE(str.is_stable()); + } + { + std::string s; + s += "a" + cm::String("b") + 'c'; + ASSERT_TRUE(s == "abc"); + } + { + std::ostringstream ss; + ss << ("a" + cm::String("b") + 'c'); + ASSERT_TRUE(ss.str() == "abc"); + } + return true; +} + +static bool testStability() +{ + std::cout << "testStability()\n"; + cm::String str = "abc"_s; + ASSERT_TRUE(!str.is_stable()); + ASSERT_TRUE(str.str_if_stable() == nullptr); + str.stabilize(); + ASSERT_TRUE(str.is_stable()); + std::string const* str_if_stable = str.str_if_stable(); + ASSERT_TRUE(str_if_stable != nullptr); + ASSERT_TRUE(*str_if_stable == "abc"); + str.stabilize(); + ASSERT_TRUE(str.is_stable()); + ASSERT_TRUE(str.str_if_stable() == str_if_stable); + return true; +} + +int testString(int /*unused*/, char* /*unused*/ []) +{ + if (!testConstructDefault()) { + return 1; + } + if (!testConstructFromNullPtr()) { + return 1; + } + if (!testConstructFromCStrNull()) { + return 1; + } + if (!testConstructFromCharArray()) { + return 1; + } + if (!testConstructFromCStr()) { + return 1; + } + if (!testConstructFromStdString()) { + return 1; + } + if (!testConstructFromView()) { + return 1; + } + if (!testConstructFromChar()) { + return 1; + } + if (!testConstructFromInitList()) { + return 1; + } + if (!testConstructFromBuffer()) { + return 1; + } + if (!testConstructFromInputIterator()) { + return 1; + } + if (!testConstructFromN()) { + return 1; + } + if (!testConstructFromStaticStringView()) { + return 1; + } + if (!testConstructCopy()) { + return 1; + } + if (!testConstructMove()) { + return 1; + } + if (!testAssignCopy()) { + return 1; + } + if (!testAssignMove()) { + return 1; + } + if (!testAssignFromChar()) { + return 1; + } + if (!testAssignFromView()) { + return 1; + } + if (!testAssignFromStdString()) { + return 1; + } + if (!testAssignFromCStr()) { + return 1; + } + if (!testAssignFromCharArray()) { + return 1; + } + if (!testAssignFromCStrNull()) { + return 1; + } + if (!testAssignFromNullPtr()) { + return 1; + } + if (!testAssignFromInitList()) { + return 1; + } + if (!testAssignFromStaticStringView()) { + return 1; + } + if (!testOperatorBool()) { + return 1; + } + if (!testOperatorIndex()) { + return 1; + } + if (!testOperatorPlusEqual()) { + return 1; + } + if (!testOperatorCompare()) { + return 1; + } + if (!testOperatorStream()) { + return 1; + } + if (!testOperatorStdStringPlusEqual()) { + return 1; + } + if (!testMethod_borrow()) { + return 1; + } + if (!testMethod_view()) { + return 1; + } + if (!testMethod_empty()) { + return 1; + } + if (!testMethod_length()) { + return 1; + } + if (!testMethod_at()) { + return 1; + } + if (!testMethod_front_back()) { + return 1; + } + if (!testMethod_clear()) { + return 1; + } + if (!testMethod_insert()) { + return 1; + } + if (!testMethod_erase()) { + return 1; + } + if (!testMethod_push_back()) { + return 1; + } + if (!testMethod_pop_back()) { + return 1; + } + if (!testMethod_replace()) { + return 1; + } + if (!testMethod_copy()) { + return 1; + } + if (!testMethod_resize()) { + return 1; + } + if (!testMethod_swap()) { + return 1; + } + if (!testMethodIterators()) { + return 1; + } + if (!testMethod_substr_AtEndBorrowed()) { + return 1; + } + if (!testMethod_substr_AtEndOwned()) { + return 1; + } + if (!testMethod_substr_AtStartBorrowed()) { + return 1; + } + if (!testMethod_substr_AtStartOwned()) { + return 1; + } + if (!testMethod_compare()) { + return 1; + } + if (!testMethod_find()) { + return 1; + } + if (!testMethod_rfind()) { + return 1; + } + if (!testMethod_find_first_of()) { + return 1; + } + if (!testMethod_find_first_not_of()) { + return 1; + } + if (!testMethod_find_last_of()) { + return 1; + } + if (!testMethod_find_last_not_of()) { + return 1; + } + if (!testAddition()) { + return 1; + } + if (!testStability()) { + return 1; + } + return 0; +} @@ -430,6 +430,7 @@ CMAKE_CXX_SOURCES="\ cmake \ cmakemain \ cmcmd \ + cm_string_view \ " if ${cmake_system_mingw}; then |