From 99bb7d4b6efce373ff452f3820c814866817698a Mon Sep 17 00:00:00 2001 From: Myles Busig Date: Thu, 7 Mar 2024 14:24:10 -0700 Subject: [PATCH] Add initial string_view implementation --- CMakeLists.txt | 4 +- include/mtl/string_view.hpp | 117 ++++++++++++++++++++++++++++++++++++ src/string_view.cpp | 12 ++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 include/mtl/string_view.hpp create mode 100644 src/string_view.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1407480..c446016 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.5) project(mtl LANGUAGES CXX C ASM) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(TARGET_SRC_FILES src/memcpy.s src/string.cpp src/fixed.s) -set(TARGET_PUB_INCLUDE_FILES include/mtl/utility.hpp include/mtl/string.hpp include/mtl/fsm.hpp) +set(TARGET_SRC_FILES src/memcpy.s src/string.cpp src/string_view.cpp src/fixed.s) +set(TARGET_PUB_INCLUDE_FILES include/mtl/utility.hpp include/mtl/string.hpp include/mtl/string_view.hpp include/mtl/fsm.hpp) add_library(${PROJECT_NAME} STATIC ${TARGET_SRC_FILES} ${TARGET_PUB_INCLUDE_FILES}) diff --git a/include/mtl/string_view.hpp b/include/mtl/string_view.hpp new file mode 100644 index 0000000..de213e3 --- /dev/null +++ b/include/mtl/string_view.hpp @@ -0,0 +1,117 @@ +#pragma once + +#include +#include +#include // std::char_traits +#include + +namespace mtl { + +class istring; + +constexpr size_t strlen(const char* str) { + return *str != '\0' ? strlen(str + 1) + 1 : 0; +} + +class string_view { + const char* m_str; + size_t m_size; + + friend class istring; + +public: + typedef const char* iterator; + typedef std::reverse_iterator reverse_iterator; + + static constexpr size_t npos = -1; + + constexpr string_view(const char* str) : m_str(str), m_size(mtl::strlen(str)) {} + constexpr string_view(const char* str, size_t size) : m_str(str), m_size(size) {} + string_view(const istring& str); + string_view(const istring& str, size_t size); + + constexpr const char& at(size_t i) const { return m_str[i]; } + constexpr const char& operator[](size_t i) const { return m_str[i]; } + + constexpr const char& front() const { return m_str[0]; } + constexpr const char& back() const { return m_str[m_size - 1]; } + + constexpr const char* data() const { return m_str; } + constexpr const char* c_str() const { return m_str; } + + iterator begin() const { return m_str; } + iterator cbegin() const { return m_str; } + + iterator end() const { return m_str + m_size; } + iterator cend() const { return m_str + m_size; } + + reverse_iterator rbegin() const { return reverse_iterator(m_str + m_size); } + reverse_iterator crbegin() const { return reverse_iterator(m_str + m_size); } + + reverse_iterator rend() const { return reverse_iterator(m_str); } + reverse_iterator crend() const { return reverse_iterator(m_str); } + + constexpr bool empty() const { return m_size == 0; } + + constexpr size_t size() const { return m_size; } + constexpr size_t length() const { return m_size; } + + size_t copy(char* dest, size_t count, size_t pos = 0) const; + + // Search + + size_t find(const istring& str, size_t pos = 0) const; + size_t find(const string_view& str, size_t pos = 0) const; + size_t find(char ch, size_t pos = 0) const; + + size_t rfind(const istring& str, size_t pos = npos) const; + size_t rfind(const string_view&, size_t pos = npos) const; + size_t rfind(char ch, size_t pos = npos) const; + + size_t find_first_of(const istring& str, size_t pos = 0) const; + size_t find_first_of(const string_view& str, size_t pos = 0) const; + size_t find_first_of(char ch, size_t pos = 0) const; + + size_t find_first_not_of(const istring& str, size_t pos = 0) const; + size_t find_first_not_of(const string_view& str, size_t pos = 0) const; + size_t find_first_not_of(char ch, size_t pos = 0) const; + + size_t find_last_of(const istring& str, size_t pos = npos) const; + size_t find_last_of(const string_view& str, size_t pos = npos) const; + size_t find_last_of(char ch, size_t pos = npos) const; + + size_t find_last_not_of(const istring& str, size_t pos = npos) const; + size_t find_last_not_of(const string_view& str, size_t pos = npos) const; + size_t find_last_not_of(char ch, size_t pos = npos) const; + + // Comparison + + int32_t compare(const istring& str) const; + int32_t compare(size_t pos, size_t count, const istring& str) const; + int32_t compare(size_t pos1, size_t count1, istring& str, size_t pos2, size_t count2 = npos) const; + int32_t compare(const string_view& str) const; + int32_t compare(size_t pos, size_t count, const string_view& str) const; + int32_t compare(size_t pos1, size_t count1, const string_view& str, size_t pos2, size_t count2 = npos) const; + + friend bool operator==(const istring& lhs, const istring& rhs); + friend bool operator!=(const istring& lhs, const istring& rhs); + friend bool operator<(const istring& lhs, const istring& rhs); + friend bool operator<=(const istring& lhs, const istring& rhs); + friend bool operator>(const istring& lhs, const istring& rhs); + friend bool operator>=(const istring& lhs, const istring& rhs); + + friend bool operator==(const istring& lhs, const string_view& rhs); + friend bool operator==(const string_view& lhs, const istring& rhs); + friend bool operator!=(const istring& lhs, const string_view& rhs); + friend bool operator!=(const string_view& lhs, const istring& rhs); + friend bool operator<(const istring& lhs, const string_view& rhs); + friend bool operator<(const string_view& lhs, const istring& rhs); + friend bool operator<=(const istring& lhs, const string_view& rhs); + friend bool operator<=(const string_view& lhs, const istring& rhs); + friend bool operator>(const istring& lhs, const string_view& rhs); + friend bool operator>(const string_view& lhs, const istring& rhs); + friend bool operator>=(const istring& lhs, const string_view& rhs); + friend bool operator>=(const string_view& lhs, const istring& rhs); +}; + +} // namespace mtl diff --git a/src/string_view.cpp b/src/string_view.cpp new file mode 100644 index 0000000..add162c --- /dev/null +++ b/src/string_view.cpp @@ -0,0 +1,12 @@ +#include "mtl/string_view.hpp" + +#include "mtl/string.hpp" + +namespace mtl { + +string_view::string_view(const istring& str) + : m_str(str.c_str()), m_size(str.size()) {} +string_view::string_view(const istring& str, size_t size) + : m_str(str.c_str()), m_size(size) {} + +} // namespace mtl