1
0

feat: add utf8 format

- move utf8 stream and format patch from string to patch namespace.
- add ordinay format alias and utf8 format in our format patch.
- add char8_t and char inter-cast in string reinterpret namespace.
- fix bug of utf8 formatter.
- add test for utf8 format.
This commit is contained in:
2025-09-25 15:29:55 +08:00
parent a61955bb09
commit c8d763bdcf
17 changed files with 257 additions and 36 deletions

View File

@ -0,0 +1,35 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/patch/format.hpp>
#define FORMAT ::yycc::patch::format
namespace yycctest::patch::format {
static constexpr char8_t PROBE[] = u8"hello";
static std::u8string PROBE_STRING(PROBE);
static constexpr std::u8string_view PROBE_STRING_VIEW(PROBE);
TEST(PatchFormat, OrdinaryFormat) {
auto rv = FORMAT::format("{}{}{}{}{}{} world!",
PROBE[0],
PROBE_STRING.data(),
PROBE_STRING.c_str(),
PROBE,
PROBE_STRING,
PROBE_STRING_VIEW);
EXPECT_EQ(rv, "104hellohellohellohellohello world!");
}
TEST(PatchFormat, Utf8Format) {
auto rv = FORMAT::format(u8"{}{}{}{}{}{} world!",
PROBE[0],
PROBE_STRING.data(),
PROBE_STRING.c_str(),
PROBE,
PROBE_STRING,
PROBE_STRING_VIEW);
EXPECT_EQ(rv, u8"104hellohellohellohellohello world!");
}
}

View File

@ -1,28 +1,28 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/string/stream.hpp>
#include <yycc/patch/stream.hpp>
#include <yycc/string/reinterpret.hpp>
#include <sstream>
#define REINTERPRET ::yycc::string::reinterpret
using namespace std::literals::string_view_literals;
using namespace ::yycc::string::stream;
using namespace ::yycc::patch::stream;
namespace yycctest::string::stream {
namespace yycctest::patch::stream {
TEST(StringStream, StringView) {
TEST(PatchStream, StringView) {
std::stringstream ss;
ss << u8"hello"sv;
EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"hello"sv);
}
TEST(StringStream, CStrPtr) {
TEST(PatchStream, CStrPtr) {
std::stringstream ss;
ss << u8"hello";
EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"hello");
}
TEST(StringStream, Character) {
TEST(PatchStream, Character) {
std::stringstream ss;
ss << u8'y';
EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"y");

View File

@ -6,6 +6,7 @@
#include <yycc/rust/prelude.hpp>
#define REINTERPRET ::yycc::string::reinterpret
#define AS_UINT8(p) static_cast<u8>(p)
#define CONST_VOID_PTR(p) reinterpret_cast<const void*>(p)
#define VOID_PTR(p) reinterpret_cast<void*>(p)
@ -13,6 +14,16 @@ namespace yycctest::string::reinterpret {
static std::u8string PROBE(u8"Test");
TEST(StringReinterpret, Character) {
const auto& src = PROBE[0];
const auto dst = REINTERPRET::as_ordinary(src);
const auto new_src = REINTERPRET::as_utf8(dst);
// Value should be the same after casting.
EXPECT_EQ(AS_UINT8(src), AS_UINT8(dst));
EXPECT_EQ(AS_UINT8(src), AS_UINT8(new_src));
}
TEST(StringReinterpret, ConstPointer) {
const auto* src = PROBE.data();
const auto* dst = REINTERPRET::as_ordinary(src);
@ -33,7 +44,7 @@ namespace yycctest::string::reinterpret {
EXPECT_EQ(VOID_PTR(src), VOID_PTR(new_src));
}
TEST(StringReinterpret, String) {
TEST(StringReinterpret, StlString) {
auto src = std::u8string(PROBE);
auto dst = REINTERPRET::as_ordinary(src);
auto new_src = REINTERPRET::as_utf8(dst);
@ -45,7 +56,7 @@ namespace yycctest::string::reinterpret {
EXPECT_TRUE(std::memcmp(src.data(), new_src.data(), src.length()) == 0);
}
TEST(StringReinterpret, StringView) {
TEST(StringReinterpret, StlStringView) {
auto src = std::u8string_view(PROBE);
auto dst = REINTERPRET::as_ordinary_view(src);
auto new_src = REINTERPRET::as_utf8_view(dst);