12#include "../3rdparty/catch2/catch.hpp"
14#include "../string_func.h"
15#include "../table/control_codes.h"
19TEST_CASE(
"StrCompareIgnoreCase - std::string")
43TEST_CASE(
"StrCompareIgnoreCase - char pointer")
67TEST_CASE(
"StrCompareIgnoreCase - std::string_view")
77 std::string_view base{
"aaAbB"};
101TEST_CASE(
"StrEqualsIgnoreCase - std::string")
119TEST_CASE(
"StrEqualsIgnoreCase - char pointer")
137TEST_CASE(
"StrEqualsIgnoreCase - std::string_view")
147 std::string_view base{
"aaAb"};
167TEST_CASE(
"StrStartsWithIgnoreCase - std::string")
192TEST_CASE(
"StrStartsWithIgnoreCase - char pointer")
217TEST_CASE(
"StrStartsWithIgnoreCase - std::string_view")
227 std::string_view base{
"aabAb"};
254TEST_CASE(
"StrEndsWithIgnoreCase - std::string")
279TEST_CASE(
"StrEndsWithIgnoreCase - char pointer")
304TEST_CASE(
"StrEndsWithIgnoreCase - std::string_view")
314 std::string_view base{
"aabAba"};
340TEST_CASE(
"FormatArrayAsHex")
344 CHECK(
FormatArrayAsHex(std::array<uint8_t, 4>{0x13, 0x38, 0x42, 0xAF}) ==
"133842AF");
347TEST_CASE(
"ConvertHexToBytes")
353 std::array<uint8_t, 1> bytes1;
356 CHECK(bytes1[0] == 0x12);
361 std::array<uint8_t, 2> bytes2;
364 CHECK(bytes2[0] == 0x12);
365 CHECK(bytes2[1] == 0x34);
367 std::array<uint8_t, 8> bytes3;
369 CHECK(bytes3[0] == 0x12);
370 CHECK(bytes3[1] == 0x34);
371 CHECK(bytes3[2] == 0x56);
372 CHECK(bytes3[3] == 0x78);
373 CHECK(bytes3[4] == 0x9a);
374 CHECK(bytes3[5] == 0xbc);
375 CHECK(bytes3[6] == 0xde);
376 CHECK(bytes3[7] == 0xf0);
379 CHECK(bytes3[0] == 0x12);
380 CHECK(bytes3[1] == 0x34);
381 CHECK(bytes3[2] == 0x56);
382 CHECK(bytes3[3] == 0x78);
383 CHECK(bytes3[4] == 0x9a);
384 CHECK(bytes3[5] == 0xbc);
385 CHECK(bytes3[6] == 0xde);
386 CHECK(bytes3[7] == 0xf0);
389static const std::vector<std::pair<std::string, std::string>> _str_trim_testcases = {
394 {
" a b c ",
"a b c"},
398TEST_CASE(
"StrTrimInPlace")
400 for (
auto [input, expected] : _str_trim_testcases) {
402 CHECK(input == expected);
406TEST_CASE(
"StrTrimView") {
407 for (
const auto& [input, expected] : _str_trim_testcases) {
408 CHECK(StrTrimView(input) == expected);
415static std::string FixSCCEncodedWrapper(
const std::string &str,
bool fix_code)
417 std::string result = str;
423static void ComposePart(std::back_insert_iterator<std::string> &output,
char32_t c)
429static void ComposePart(std::back_insert_iterator<std::string> &output,
const std::string &value)
431 for (
const auto &c : value) *output = c;
435template <
typename... Args>
436static std::string Compose(Args &&... args)
439 auto output = std::back_inserter(result);
440 (ComposePart(output, args), ...);
444TEST_CASE(
"FixSCCEncoded")
447 CHECK(FixSCCEncodedWrapper(
"",
false) ==
"");
450 CHECK(FixSCCEncodedWrapper(
"\uE0280",
true) == Compose(
SCC_ENCODED,
"0"));
453 CHECK(FixSCCEncodedWrapper(
"\uE0280:\uE0281",
true) == Compose(
SCC_ENCODED,
"0", SCC_RECORD_SEPARATOR,
SCC_ENCODED,
"1"));
456 CHECK(FixSCCEncodedWrapper(
"\uE0001",
false) == Compose(
SCC_ENCODED,
"1"));
471 CHECK(FixSCCEncodedWrapper(
"\uE0006:\"Foo\":7CA:\"Bar\"",
false) == Compose(
SCC_ENCODED,
"6", SCC_RECORD_SEPARATOR,
SCC_ENCODED_STRING,
"Foo", SCC_RECORD_SEPARATOR,
SCC_ENCODED_NUMERIC,
"7CA", SCC_RECORD_SEPARATOR,
SCC_ENCODED_STRING,
"Bar"));
474 CHECK(FixSCCEncodedWrapper(
"\uE000777:\uE0008888:\"Foo\":\"BarBaz\"",
false) == Compose(
SCC_ENCODED,
"777", SCC_RECORD_SEPARATOR,
SCC_ENCODED,
"8888", SCC_RECORD_SEPARATOR,
SCC_ENCODED_STRING,
"Foo", SCC_RECORD_SEPARATOR,
SCC_ENCODED_STRING,
"BarBaz"));
@ SCC_ENCODED
Encoded string marker and sub-string parameter.
@ SCC_ENCODED_NUMERIC
Encoded numeric parameter.
@ SCC_ENCODED_STRING
Encoded string parameter.
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
int StrCompareIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
bool StrEndsWithIgnoreCase(std::string_view str, const std::string_view suffix)
Check whether the given string ends with the given suffix, ignoring case.
void StrTrimInPlace(std::string &str)
Trim the spaces from given string in place, i.e.
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
void FixSCCEncoded(std::string &str, bool fix_code)
Scan the string for old values of SCC_ENCODED and fix it to it's new, value.