Add string substring operation

This commit is contained in:
rexy712 2020-09-17 07:23:06 -07:00
parent f930dde989
commit 2ea8c617f0
3 changed files with 22 additions and 1 deletions

View File

@ -295,6 +295,10 @@ namespace rexy{
void append(const_pointer data)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
template<class Alloc = allocator_type>
basic_string<value_type,Alloc> substring(size_type start, size_type end)const;
pointer release(void)noexcept(noexcept(this->allocate(0)));
using detail::hasallocator<Allocator>::allocator;
};

View File

@ -211,6 +211,16 @@ namespace rexy{
append(data, cx::strlen(data));
}
template<class Char, class Allocator>
template<class Alloc>
auto basic_string<Char,Allocator>::substring(size_type start, size_type end)const -> basic_string<value_type,Alloc>{
if(start > end || end > this->length())
return {};
const size_type newlen = end - start;
basic_string<value_type,Alloc> tmp(newlen);
tmp.append(this->get() + start, newlen);
return tmp;
}
template<class Char, class Allocator>
auto basic_string<Char,Allocator>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{
if(this->islong()){

View File

@ -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();
}