Add initial string_view implementation
This commit is contained in:
parent
f0dfd29692
commit
99bb7d4b6e
@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.5)
|
|||||||
project(mtl LANGUAGES CXX C ASM)
|
project(mtl LANGUAGES CXX C ASM)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
set(TARGET_SRC_FILES src/memcpy.s src/string.cpp src/fixed.s)
|
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/fsm.hpp)
|
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})
|
add_library(${PROJECT_NAME} STATIC ${TARGET_SRC_FILES} ${TARGET_PUB_INCLUDE_FILES})
|
||||||
|
|
||||||
|
|||||||
117
include/mtl/string_view.hpp
Normal file
117
include/mtl/string_view.hpp
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string> // std::char_traits
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
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<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
|
||||||
12
src/string_view.cpp
Normal file
12
src/string_view.cpp
Normal file
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user