summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-03-18 00:20:38 +0100
committerAki <please@ignore.pl>2024-03-18 00:20:38 +0100
commit7117768feb2b2acb8d2498229cbaa4afbad377f1 (patch)
treec5abefb044bc885604afa9cb48e3af1434e279db
parent5b1ce74b74a47f8812470efe9c56e03bcb0e456d (diff)
downloadstarshatter-7117768feb2b2acb8d2498229cbaa4afbad377f1.zip
starshatter-7117768feb2b2acb8d2498229cbaa4afbad377f1.tar.gz
starshatter-7117768feb2b2acb8d2498229cbaa4afbad377f1.tar.bz2
Text operations that return new Text are now const
-rw-r--r--FoundationEx/include/Text.h6
-rw-r--r--FoundationEx/src/Text.cpp6
-rw-r--r--FoundationEx/test/Text.cpp22
3 files changed, 26 insertions, 8 deletions
diff --git a/FoundationEx/include/Text.h b/FoundationEx/include/Text.h
index 851cadf..c40c636 100644
--- a/FoundationEx/include/Text.h
+++ b/FoundationEx/include/Text.h
@@ -124,9 +124,9 @@ public:
void toUpper();
// substring
- Text substring(int start, int length);
- Text trim();
- Text replace(const char* pattern, const char* substitution);
+ Text substring(int start, int length) const;
+ Text trim() const;
+ Text replace(const char* pattern, const char* substitution) const;
Text concat(const char* tail) const;
static Text format(const char* fmt, ...);
diff --git a/FoundationEx/src/Text.cpp b/FoundationEx/src/Text.cpp
index be03ce3..cba7720 100644
--- a/FoundationEx/src/Text.cpp
+++ b/FoundationEx/src/Text.cpp
@@ -598,7 +598,7 @@ Text::toUpper()
}
Text
-Text::substring(int start, int length)
+Text::substring(int start, int length) const
{
Text result;
@@ -620,7 +620,7 @@ Text::substring(int start, int length)
}
Text
-Text::trim()
+Text::trim() const
{
Text result;
@@ -638,7 +638,7 @@ Text::trim()
}
Text
-Text::replace(const char* pattern, const char* substitution)
+Text::replace(const char* pattern, const char* substitution) const
{
Text result;
diff --git a/FoundationEx/test/Text.cpp b/FoundationEx/test/Text.cpp
index 7400eb1..deaf742 100644
--- a/FoundationEx/test/Text.cpp
+++ b/FoundationEx/test/Text.cpp
@@ -31,7 +31,7 @@ TEST(FoundationEx, TextCanBeCopied)
TEST(FoundationEx, ConcatenateTextWithLiteralWithoutSideEffects)
{
- const Text a {"Hello"};
+ Text a {"Hello"};
const auto b = a + ", there";
ASSERT_EQ("Hello", a);
ASSERT_EQ("Hello, there", b);
@@ -41,7 +41,25 @@ TEST(FoundationEx, ConcatenateTextWithLiteralWithoutSideEffects)
TEST(FoundationEx, ReplaceInTextWithoutSideEffects)
{
Text a {"Hello, all"};
- const auto b = a.replace("Hello", "Goodbye"); // Test::replace should be const
+ const auto b = a.replace("Hello", "Goodbye");
ASSERT_EQ("Hello, all", a);
ASSERT_EQ("Goodbye, all", b);
}
+
+
+TEST(FoundationEx, SubstringFromTextWithoutSideEffects)
+{
+ Text a {"Hello, all"};
+ const auto b = a.substring(7, 3);
+ ASSERT_EQ("Hello, all", a);
+ ASSERT_EQ("all", b);
+}
+
+
+TEST(FoundationEx, TrimTextWithoutSideEffects)
+{
+ Text a {" Hi! "};
+ const auto b = a.trim();
+ ASSERT_EQ(" Hi! ", a);
+ ASSERT_EQ("Hi!", b);
+}