Implements piping StringView into streams.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-07-08 13:28:43 +02:00
parent 97b15650f1
commit 950464ae79
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 23 additions and 5 deletions

View File

@ -1,5 +1,6 @@
#ifndef ARBUTILS_BASICSTRINGVIEW_HPP
#define ARBUTILS_BASICSTRINGVIEW_HPP
#include <iostream>
#include <string>
namespace ArbUt {
@ -28,7 +29,11 @@ namespace ArbUt {
virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0;
virtual constexpr bool operator==(const char* rhs) const noexcept = 0;
virtual constexpr bool operator!=(const char* rhs) const noexcept = 0;
friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) {
out << c.c_str();
return out;
}
};
}
#endif // ARBUTILS_BASICSTRINGVIEW_HPP

View File

@ -1,5 +1,6 @@
#ifdef TESTS_BUILD
#include <cstring>
#include <sstream>
#include <unordered_map>
#include "../extern/catch.hpp"
#include "../src/StringView.hpp"
@ -11,7 +12,7 @@ TEST_CASE("Initialize compile time", "[Utilities]") {
TEST_CASE("Compare compile time", "[Utilities]") { static_assert("foo"_cnc != "bar"_cnc); }
TEST_CASE("Compare compile time with CaseInsensitiveConstString", "[Utilities]") {
TEST_CASE("Compare compile time with CaseInsensitiveStringview", "[Utilities]") {
static_assert("foo"_cnc == ArbUt::StringViewLiteral("foo"));
}
@ -34,18 +35,30 @@ TEST_CASE("Use case insensitive const string in switch case", "[Utilities]") {
}
}
TEST_CASE("Literal conststring to non literal, then use", "[Utilities]") {
TEST_CASE("Literal stringview to non literal, then use", "[Utilities]") {
ArbUt::StringView val;
{ val = "foobar"_cnc; }
INFO(val.c_str());
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
}
TEST_CASE("Pipe stringview into strean", "[Utilities]") {
std::stringstream ss;
ArbUt::StringView val = "foo";
ss << val;
REQUIRE(ss.str() == "foo");
ss << val;
REQUIRE(ss.str() == "foofoo");
ArbUt::StringView val2 = "bar";
ss << val2;
REQUIRE(ss.str() == "foofoobar");
}
#ifndef WINDOWS
__attribute__((optnone))
#endif
static ArbUt::StringView
TestCreateConstString() {
TestCreateStringview() {
char originalVal[7];
originalVal[0] = 'f';
originalVal[1] = 'o';
@ -58,7 +71,7 @@ TestCreateConstString() {
}
TEST_CASE("Out of scope char* doesn't lose reference", "[Utilities]") {
ArbUt::StringView val = TestCreateConstString();
ArbUt::StringView val = TestCreateStringview();
INFO(val.c_str());
REQUIRE(strcmp(val.c_str(), "foobar") == 0);
}