12#include "../3rdparty/catch2/catch.hpp"
14#include "../string_func.h"
15#include "../strings_func.h"
16#include "../core/string_builder.hpp"
17#include "../table/control_codes.h"
19#include "table/strings.h"
23TEST_CASE(
"StrCompareIgnoreCase - std::string")
47TEST_CASE(
"StrCompareIgnoreCase - char pointer")
71TEST_CASE(
"StrCompareIgnoreCase - std::string_view")
81 std::string_view base{
"aaAbB"};
105TEST_CASE(
"StrEqualsIgnoreCase - std::string")
123TEST_CASE(
"StrEqualsIgnoreCase - char pointer")
141TEST_CASE(
"StrEqualsIgnoreCase - std::string_view")
151 std::string_view base{
"aaAb"};
171TEST_CASE(
"StrStartsWithIgnoreCase - std::string")
196TEST_CASE(
"StrStartsWithIgnoreCase - char pointer")
221TEST_CASE(
"StrStartsWithIgnoreCase - std::string_view")
231 std::string_view base{
"aabAb"};
258TEST_CASE(
"StrEndsWithIgnoreCase - std::string")
283TEST_CASE(
"StrEndsWithIgnoreCase - char pointer")
308TEST_CASE(
"StrEndsWithIgnoreCase - std::string_view")
318 std::string_view base{
"aabAba"};
344TEST_CASE(
"FormatArrayAsHex")
348 CHECK(
FormatArrayAsHex(std::array<uint8_t, 4>{0x13, 0x38, 0x42, 0xAF}) ==
"133842AF");
351TEST_CASE(
"ConvertHexToBytes")
357 std::array<uint8_t, 1> bytes1;
360 CHECK(bytes1[0] == 0x12);
365 std::array<uint8_t, 2> bytes2;
368 CHECK(bytes2[0] == 0x12);
369 CHECK(bytes2[1] == 0x34);
371 std::array<uint8_t, 8> bytes3;
373 CHECK(bytes3[0] == 0x12);
374 CHECK(bytes3[1] == 0x34);
375 CHECK(bytes3[2] == 0x56);
376 CHECK(bytes3[3] == 0x78);
377 CHECK(bytes3[4] == 0x9a);
378 CHECK(bytes3[5] == 0xbc);
379 CHECK(bytes3[6] == 0xde);
380 CHECK(bytes3[7] == 0xf0);
383 CHECK(bytes3[0] == 0x12);
384 CHECK(bytes3[1] == 0x34);
385 CHECK(bytes3[2] == 0x56);
386 CHECK(bytes3[3] == 0x78);
387 CHECK(bytes3[4] == 0x9a);
388 CHECK(bytes3[5] == 0xbc);
389 CHECK(bytes3[6] == 0xde);
390 CHECK(bytes3[7] == 0xf0);
393static const std::vector<std::pair<std::string, std::string>> _str_trim_testcases = {
398 {
" a b c ",
"a b c"},
402TEST_CASE(
"StrTrimInPlace")
404 for (
auto [input, expected] : _str_trim_testcases) {
406 CHECK(input == expected);
410TEST_CASE(
"StrTrimView") {
411 for (
const auto& [input, expected] : _str_trim_testcases) {
412 CHECK(StrTrimView(input) == expected);
419static std::string FixSCCEncodedWrapper(
const std::string &str,
bool fix_code)
421 std::string result = str;
433static void ComposePart(
StringBuilder &builder,
const std::string &value)
439template <
typename... Args>
440static std::string Compose(Args &&... args)
444 (ComposePart(builder, args), ...);
448TEST_CASE(
"FixSCCEncoded")
451 CHECK(FixSCCEncodedWrapper(
"",
false) ==
"");
454 CHECK(FixSCCEncodedWrapper(
"\uE0280",
true) == Compose(
SCC_ENCODED,
"0"));
457 CHECK(FixSCCEncodedWrapper(
"\uE0280:\uE0281",
true) == Compose(
SCC_ENCODED,
"0", SCC_RECORD_SEPARATOR,
SCC_ENCODED,
"1"));
460 CHECK(FixSCCEncodedWrapper(
"\uE0001",
false) == Compose(
SCC_ENCODED,
"1"));
475 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"));
478 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"));
481TEST_CASE(
"EncodedString::ReplaceParam")
486 CHECK(string1 != string2);
490 CHECK(string2 == string3);
void PutUtf8(char32_t c)
Append UTF.8 char.
Container for an encoded string, created by GetEncodedString.
EncodedString ReplaceParam(size_t param, StringParameter &&value) const
Replace a parameter of this EncodedString.
Compose data into a growing std::string.
@ 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.
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.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.