summaryrefslogtreecommitdiff
path: root/asl
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-11-14 23:23:29 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:58 +0100
commitae9608d4f69a07c3efcf6ba9f38cb4ac23cc2110 (patch)
tree0d6bd8af8b3c334a12aaaf4bcfdd186942414220 /asl
parent75e956eac30050bb10d131b8f14ecbc396abcf17 (diff)
Add string_view == operator
Diffstat (limited to 'asl')
-rw-r--r--asl/string_view.hpp9
-rw-r--r--asl/tests/format_tests.cpp38
-rw-r--r--asl/tests/string_view_tests.cpp21
3 files changed, 38 insertions, 30 deletions
diff --git a/asl/string_view.hpp b/asl/string_view.hpp
index 2e8efff..85f1323 100644
--- a/asl/string_view.hpp
+++ b/asl/string_view.hpp
@@ -70,6 +70,13 @@ public:
ASL_ASSERT(offset >= 0 && offset <= m_size);
return string_view{m_data + offset, m_size - offset}; // NOLINT(*-pointer-arithmetic)
}
+
+ constexpr bool operator==(string_view other) const
+ {
+ if (m_size != other.m_size) { return false; }
+ // @Todo Remove builtin_memcmp
+ return __builtin_memcmp(m_data, other.m_data, static_cast<size_t>(m_size)) == 0;
+ }
};
} // namespace asl
@@ -78,5 +85,3 @@ constexpr asl::string_view operator ""_sv(const char* s, size_t len)
{
return asl::string_view(s, static_cast<isize_t>(len));
}
-
-// @Todo Make comparison operator, replace in format and string_view tests
diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp
index dd31efc..cb7a13f 100644
--- a/asl/tests/format_tests.cpp
+++ b/asl/tests/format_tests.cpp
@@ -19,13 +19,12 @@ class StringSink : public asl::writer
public:
void write(asl::span<const asl::byte> str) override
{
- m_data = (char*)realloc(m_data, (size_t)(m_current_len + str.size() + 1));
+ m_data = (char*)realloc(m_data, (size_t)(m_current_len + str.size()));
memcpy(m_data + m_current_len, str.data(), (size_t)str.size());
m_current_len += str.size();
- m_data[m_current_len] = '\0';
}
- constexpr const char* cstr() const { return m_data; }
+ constexpr asl::string_view str() const { return {m_data, m_current_len}; }
void reset()
{
@@ -40,46 +39,45 @@ ASL_TEST(format_args)
StringSink sink;
// @Todo Introduce ASL_TEST_EXPECT_EQ, or ASL_TEST_EXPECT_STREQ
- // @Todo Don't use strcmp for string comparison
asl::format(&sink, "Hello, world!");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world!") == 0);
+ ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv);
sink.reset();
asl::format(&sink, "");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "") == 0);
+ ASL_TEST_EXPECT(sink.str() == ""_sv);
sink.reset();
asl::format(&sink, "Hello, {}!", "world");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world!") == 0);
+ ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv);
sink.reset();
asl::format(&sink, "Hello, {}! {}", "world");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, world! <ERROR>") == 0);
+ ASL_TEST_EXPECT(sink.str() == "Hello, world! <ERROR>"_sv);
sink.reset();
asl::format(&sink, "Hello, pup!", "world");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "Hello, pup!") == 0);
+ ASL_TEST_EXPECT(sink.str() == "Hello, pup!"_sv);
sink.reset();
asl::format(&sink, "{}", "CHEESE");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "CHEESE") == 0);
+ ASL_TEST_EXPECT(sink.str() == "CHEESE"_sv);
sink.reset();
asl::format(&sink, "{ ", "CHEESE");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "<ERROR> ") == 0);
+ ASL_TEST_EXPECT(sink.str() == "<ERROR> "_sv);
sink.reset();
asl::format(&sink, "{", "CHEESE");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "<ERROR>") == 0);
+ ASL_TEST_EXPECT(sink.str() == "<ERROR>"_sv);
sink.reset();
asl::format(&sink, "a{{b");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "a{b") == 0);
+ ASL_TEST_EXPECT(sink.str() == "a{b"_sv);
sink.reset();
asl::format(&sink, "{{{}}} }", "CHEESE");
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "{CHEESE} }") == 0);
+ ASL_TEST_EXPECT(sink.str() == "{CHEESE} }"_sv);
}
ASL_TEST(format_integers)
@@ -88,23 +86,23 @@ ASL_TEST(format_integers)
sink.reset();
asl::format(&sink, "{} {} {}", 0, 1, 2);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "0 1 2") == 0);
+ ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv);
sink.reset();
asl::format(&sink, "{} {} {}", 10, 11, 12);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "10 11 12") == 0);
+ ASL_TEST_EXPECT(sink.str() == "10 11 12"_sv);
sink.reset();
asl::format(&sink, "{} {} {}", 100, 101, 102);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "100 101 102") == 0);
+ ASL_TEST_EXPECT(sink.str() == "100 101 102"_sv);
sink.reset();
asl::format(&sink, "{} {} {}", 1000, 1001, 1002);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "1000 1001 1002") == 0);
+ ASL_TEST_EXPECT(sink.str() == "1000 1001 1002"_sv);
sink.reset();
asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "-1 -23 -456 -7890") == 0);
+ ASL_TEST_EXPECT(sink.str() == "-1 -23 -456 -7890"_sv);
}
ASL_TEST(format_boolean)
@@ -113,5 +111,5 @@ ASL_TEST(format_boolean)
sink.reset();
asl::format(&sink, "{} {}", true, false);
- ASL_TEST_EXPECT(strcmp(sink.cstr(), "true false") == 0);
+ ASL_TEST_EXPECT(sink.str() == "true false"_sv);
}
diff --git a/asl/tests/string_view_tests.cpp b/asl/tests/string_view_tests.cpp
index 1c12603..8f7ff28 100644
--- a/asl/tests/string_view_tests.cpp
+++ b/asl/tests/string_view_tests.cpp
@@ -1,9 +1,6 @@
#include "asl/string_view.hpp"
#include "asl/testing/testing.hpp"
-// @Todo Don't use stdlib, remake memcmp
-#include <string.h>
-
static_assert(asl::trivially_destructible<asl::string_view>);
static_assert(asl::trivially_copyable<asl::string_view>);
@@ -20,7 +17,7 @@ ASL_TEST(from_literal)
{
asl::string_view s1 = "Hello"_sv;
ASL_TEST_ASSERT(s1.size() == 5);
- ASL_TEST_EXPECT(memcmp(s1.data(), "Hello", 5) == 0);
+ ASL_TEST_EXPECT(__builtin_memcmp(s1.data(), "Hello", 5) == 0);
asl::string_view s2 = ""_sv;
ASL_TEST_EXPECT(s2.is_empty());
@@ -32,11 +29,11 @@ ASL_TEST(substr1)
asl::string_view s2 = s1.substr(0);
ASL_TEST_ASSERT(s2.size() == 4);
- ASL_TEST_EXPECT(memcmp(s2.data(), "abcd", 4) == 0);
+ ASL_TEST_EXPECT(__builtin_memcmp(s2.data(), "abcd", 4) == 0);
s2 = s1.substr(2);
ASL_TEST_ASSERT(s2.size() == 2);
- ASL_TEST_EXPECT(memcmp(s2.data(), "cd", 2) == 0);
+ ASL_TEST_EXPECT(__builtin_memcmp(s2.data(), "cd", 2) == 0);
s2 = s1.substr(4);
ASL_TEST_ASSERT(s2.size() == 0);
@@ -48,11 +45,11 @@ ASL_TEST(substr2)
asl::string_view s2 = s1.substr(0, 4);
ASL_TEST_ASSERT(s2.size() == 4);
- ASL_TEST_EXPECT(memcmp(s2.data(), "abcd", 4) == 0);
+ ASL_TEST_EXPECT(__builtin_memcmp(s2.data(), "abcd", 4) == 0);
s2 = s1.substr(1, 2);
ASL_TEST_ASSERT(s2.size() == 2);
- ASL_TEST_EXPECT(memcmp(s2.data(), "bc", 2) == 0);
+ ASL_TEST_EXPECT(__builtin_memcmp(s2.data(), "bc", 2) == 0);
s2 = s1.substr(4, 0);
ASL_TEST_ASSERT(s2.size() == 0);
@@ -60,3 +57,11 @@ ASL_TEST(substr2)
s2 = s1.substr(1, 0);
ASL_TEST_ASSERT(s2.size() == 0);
}
+
+ASL_TEST(equal)
+{
+ ASL_TEST_EXPECT("abc"_sv == "abc"_sv);
+ ASL_TEST_EXPECT(""_sv == ""_sv);
+ ASL_TEST_EXPECT("abc"_sv != "ab"_sv);
+ ASL_TEST_EXPECT("abc"_sv != "abd"_sv);
+}