diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index d228166f..5cc37f10 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -1497,7 +1497,7 @@ namespace etl } //********************************************************************* - /// Checks that the string is within the string + /// Checks that the string is within this string //********************************************************************* bool contains(const etl::ibasic_string& str) const { @@ -1505,7 +1505,7 @@ namespace etl } //********************************************************************* - /// Checks that the view is within the string + /// Checks that the view is within this string //********************************************************************* template bool contains(const etl::basic_string_view& view) const @@ -1514,7 +1514,7 @@ namespace etl } //********************************************************************* - /// Checks that text is within the string + /// Checks that text is within this string //********************************************************************* bool contains(const_pointer s) const { @@ -1522,13 +1522,98 @@ namespace etl } //********************************************************************* - /// Checks that character is within the string + /// Checks that character is within this string //********************************************************************* bool contains(value_type c) const { return find(c) != npos; } + //********************************************************************* + /// Checks that the string is the start of this string + //********************************************************************* + bool starts_with(const ibasic_string& str) const + { + return compare(0, str.size(), str) == 0; + } + + //********************************************************************* + /// Checks that the view is the start of this string + //********************************************************************* + template + bool starts_with(const basic_string_view& view) const + { + return compare(0, view.size(), view) == 0; + } + + //********************************************************************* + /// Checks that the string is the start of this string + //********************************************************************* + bool starts_with(const_pointer s) const + { + size_t len = etl::strlen(s); + + return compare(0, len, s, len) == 0; + } + + //********************************************************************* + /// Checks that the character is the start of this string + //********************************************************************* + bool starts_with(value_type c) const + { + return !empty() && (front() == c); + } + + //********************************************************************* + /// Checks that the string is the end of this string + //********************************************************************* + bool ends_with(const ibasic_string& str) const + { + if (str.size() > size()) + { + return false; + } + + return compare(size() - str.size(), str.size(), str) == 0; + } + + //********************************************************************* + /// Checks that the view is the end of this string + //********************************************************************* + template + bool ends_with(const basic_string_view& view) const + { + if (view.size() > size()) + { + return false; + } + + return compare(size() - view.size(), view.size(), view) == 0; + } + + //********************************************************************* + /// Checks that the string is the end of this string + //********************************************************************* + bool ends_with(const_pointer s) const + { + size_t len = etl::strlen(s); + + if (len > size()) + { + return false; + } + + return compare(size() - len, len, s, len) == 0; + } + + //********************************************************************* + /// Checks that the character is the end of this string + //********************************************************************* + bool ends_with(value_type c) const + { + return !empty() && (back() == c); + } + //********************************************************************* /// Replace 'length' characters from 'position' with 'str'. ///\param position The position to start from. diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 4da5716b..5c1e0c99 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -3644,6 +3644,90 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text start(STR("A haystack")); + Text not_start(STR("a needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text end(STR("else")); + Text not_end(STR("needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_char_external_buffer.cpp b/test/test_string_char_external_buffer.cpp index b2998e63..4ef70851 100644 --- a/test/test_string_char_external_buffer.cpp +++ b/test/test_string_char_external_buffer.cpp @@ -3947,6 +3947,104 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text start(STR("A haystack"), buffer2.data(), buffer2.size()); + Text not_start(STR("a needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text end(STR("else"), buffer2.data(), buffer2.size()); + Text not_end(STR("needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u16.cpp b/test/test_string_u16.cpp index a09f9a59..f7565b01 100644 --- a/test/test_string_u16.cpp +++ b/test/test_string_u16.cpp @@ -3658,6 +3658,90 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text start(STR("A haystack")); + Text not_start(STR("a needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text end(STR("else")); + Text not_end(STR("needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u16_external_buffer.cpp b/test/test_string_u16_external_buffer.cpp index 96fd23d5..b15a4772 100644 --- a/test/test_string_u16_external_buffer.cpp +++ b/test/test_string_u16_external_buffer.cpp @@ -3961,6 +3961,104 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text start(STR("A haystack"), buffer2.data(), buffer2.size()); + Text not_start(STR("a needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text end(STR("else"), buffer2.data(), buffer2.size()); + Text not_end(STR("needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u32.cpp b/test/test_string_u32.cpp index 2b607b33..b27fcaff 100644 --- a/test/test_string_u32.cpp +++ b/test/test_string_u32.cpp @@ -3658,6 +3658,90 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text start(STR("A haystack")); + Text not_start(STR("a needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text end(STR("else")); + Text not_end(STR("needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u32_external_buffer.cpp b/test/test_string_u32_external_buffer.cpp index bdbaf7b2..11858685 100644 --- a/test/test_string_u32_external_buffer.cpp +++ b/test/test_string_u32_external_buffer.cpp @@ -3961,6 +3961,104 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text start(STR("A haystack"), buffer2.data(), buffer2.size()); + Text not_start(STR("a needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text end(STR("else"), buffer2.data(), buffer2.size()); + Text not_end(STR("needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u8.cpp b/test/test_string_u8.cpp index 5ce38042..d8d9c6a3 100644 --- a/test/test_string_u8.cpp +++ b/test/test_string_u8.cpp @@ -3661,6 +3661,90 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text start(STR("A haystack")); + Text not_start(STR("a needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text end(STR("else")); + Text not_end(STR("needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_u8_external_buffer.cpp b/test/test_string_u8_external_buffer.cpp index 085f7738..e7613008 100644 --- a/test/test_string_u8_external_buffer.cpp +++ b/test/test_string_u8_external_buffer.cpp @@ -3933,6 +3933,104 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text start(STR("A haystack"), buffer2.data(), buffer2.size()); + Text not_start(STR("a needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text end(STR("else"), buffer2.data(), buffer2.size()); + Text not_end(STR("needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_char_pointer_n) { diff --git a/test/test_string_wchar_t.cpp b/test/test_string_wchar_t.cpp index 1a8ed43b..8b3ae997 100644 --- a/test/test_string_wchar_t.cpp +++ b/test/test_string_wchar_t.cpp @@ -3659,6 +3659,90 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text start(STR("A haystack")); + Text not_start(STR("a needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + Text end(STR("else")); + Text not_end(STR("needle")); + Text excess(STR("Really gigantic text that's really really big")); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextL haystack(STR("A haystack with a needle and nothing else")); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) { diff --git a/test/test_string_wchar_t_external_buffer.cpp b/test/test_string_wchar_t_external_buffer.cpp index 5d277936..7ce900d0 100644 --- a/test/test_string_wchar_t_external_buffer.cpp +++ b/test/test_string_wchar_t_external_buffer.cpp @@ -3964,6 +3964,104 @@ namespace CHECK_FALSE(haystack.contains(STR('p'))); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text start(STR("A haystack"), buffer2.data(), buffer2.size()); + Text not_start(STR("a needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.starts_with(start)); + CHECK_FALSE(haystack.starts_with(not_start)); + CHECK_FALSE(haystack.starts_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(View(STR("A haystack")))); + CHECK_FALSE(haystack.starts_with(View(STR("a needle")))); + CHECK_FALSE(haystack.starts_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(STR("A haystack"))); + CHECK_FALSE(haystack.starts_with(STR("a needle"))); + CHECK_FALSE(haystack.starts_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_starts_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.starts_with(haystack[0])); + CHECK_FALSE(haystack.starts_with(haystack[1])); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_string) + { + TextBufferL buffer1{0}; + TextBuffer buffer2{0}; + TextBuffer buffer3{0}; + TextBuffer buffer4{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + Text end(STR("else"), buffer2.data(), buffer2.size()); + Text not_end(STR("needle"), buffer3.data(), buffer3.size()); + Text excess(STR("Really gigantic text that's really really big"), buffer4.data(), buffer4.size()); + + CHECK_TRUE(haystack.ends_with(end)); + CHECK_FALSE(haystack.ends_with(not_end)); + CHECK_FALSE(haystack.ends_with(excess)); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_view) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(View(STR("else")))); + CHECK_FALSE(haystack.ends_with(View(STR("needle")))); + CHECK_FALSE(haystack.ends_with(View(STR("Really gigantic text that's really really big")))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_pointer) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(STR("else"))); + CHECK_FALSE(haystack.ends_with(STR("needle"))); + CHECK_FALSE(haystack.ends_with(STR("Really gigantic text that's really really big"))); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_ends_with_char) + { + TextBufferL buffer1{0}; + Text haystack(STR("A haystack with a needle and nothing else"), buffer1.data(), buffer1.size()); + + CHECK_TRUE(haystack.ends_with(haystack[haystack.size() - 1])); + CHECK_FALSE(haystack.ends_with(haystack[haystack.size() - 2])); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_rfind_string) {