From 2ea8c617f088b8316f4ea9c877641bd110e2b749 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Thu, 17 Sep 2020 07:23:06 -0700 Subject: [PATCH] Add string substring operation --- include/rexy/string_base.hpp | 4 ++++ include/rexy/string_base.tpp | 10 ++++++++++ tests/basic_string.cpp | 9 ++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/rexy/string_base.hpp b/include/rexy/string_base.hpp index b976a82..66c96a6 100644 --- a/include/rexy/string_base.hpp +++ b/include/rexy/string_base.hpp @@ -295,6 +295,10 @@ namespace rexy{ void append(const_pointer data) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); + + template + basic_string substring(size_type start, size_type end)const; + pointer release(void)noexcept(noexcept(this->allocate(0))); using detail::hasallocator::allocator; }; diff --git a/include/rexy/string_base.tpp b/include/rexy/string_base.tpp index 49970e2..522ec81 100644 --- a/include/rexy/string_base.tpp +++ b/include/rexy/string_base.tpp @@ -211,6 +211,16 @@ namespace rexy{ append(data, cx::strlen(data)); } + template + template + auto basic_string::substring(size_type start, size_type end)const -> basic_string{ + if(start > end || end > this->length()) + return {}; + const size_type newlen = end - start; + basic_string tmp(newlen); + tmp.append(this->get() + start, newlen); + return tmp; + } template auto basic_string::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{ if(this->islong()){ diff --git a/tests/basic_string.cpp b/tests/basic_string.cpp index 5f8989c..150655f 100644 --- a/tests/basic_string.cpp +++ b/tests/basic_string.cpp @@ -235,7 +235,13 @@ void check_long_append(){ test_str str1(startdata1); str1.append("stuff"); if(strcmp(str1.get(), appendeddata)) - error("long append should have resulted in this is another really long string that should ensure that it makes some sort of dyn alloc for big bufstuff"); + error("long append should have resulted in this is another really long string that should ensure that it makes some sort of dyn alloc for big bufstuff\n"); +} +void check_substring(){ + rexy::string test = "this is a test string"; + rexy::string test2 = test.substring(5, 7); + if(strcmp(test2.get(), "is") || test2.length() != 2) + error("substring operation should have resulted in 'is'\n"); } int main(){ @@ -246,4 +252,5 @@ int main(){ check_long_assignment(); check_short_append(); check_long_append(); + check_substring(); }