12#include "../3rdparty/catch2/catch.hpp"
14#include "../string_func.h"
15#include "../strings_func.h"
16#include "../core/string_builder.hpp"
17#include "../core/string_consumer.hpp"
18#include "../table/control_codes.h"
20#include "table/strings.h"
22#include "../safeguards.h"
26TEST_CASE(
"StrCompareIgnoreCase - std::string")
50TEST_CASE(
"StrCompareIgnoreCase - std::string_view")
60 std::string_view base{
"aaAbB"};
84TEST_CASE(
"StrEqualsIgnoreCase - std::string")
102TEST_CASE(
"StrEqualsIgnoreCase - std::string_view")
112 std::string_view base{
"aaAb"};
132TEST_CASE(
"StrStartsWithIgnoreCase - std::string")
157TEST_CASE(
"StrStartsWithIgnoreCase - std::string_view")
167 std::string_view base{
"aabAb"};
194TEST_CASE(
"StrEndsWithIgnoreCase - std::string")
219TEST_CASE(
"StrEndsWithIgnoreCase - std::string_view")
229 std::string_view base{
"aabAba"};
255TEST_CASE(
"FormatArrayAsHex")
259 CHECK(
FormatArrayAsHex(std::array<uint8_t, 4>{0x13, 0x38, 0x42, 0xAF}) ==
"133842AF");
262TEST_CASE(
"ConvertHexToBytes")
268 std::array<uint8_t, 1> bytes1;
271 CHECK(bytes1[0] == 0x12);
276 std::array<uint8_t, 2> bytes2;
279 CHECK(bytes2[0] == 0x12);
280 CHECK(bytes2[1] == 0x34);
282 std::array<uint8_t, 8> bytes3;
284 CHECK(bytes3[0] == 0x12);
285 CHECK(bytes3[1] == 0x34);
286 CHECK(bytes3[2] == 0x56);
287 CHECK(bytes3[3] == 0x78);
288 CHECK(bytes3[4] == 0x9a);
289 CHECK(bytes3[5] == 0xbc);
290 CHECK(bytes3[6] == 0xde);
291 CHECK(bytes3[7] == 0xf0);
294 CHECK(bytes3[0] == 0x12);
295 CHECK(bytes3[1] == 0x34);
296 CHECK(bytes3[2] == 0x56);
297 CHECK(bytes3[3] == 0x78);
298 CHECK(bytes3[4] == 0x9a);
299 CHECK(bytes3[5] == 0xbc);
300 CHECK(bytes3[6] == 0xde);
301 CHECK(bytes3[7] == 0xf0);
304static const std::vector<std::pair<std::string, std::string>> _str_trim_testcases = {
309 {
" a b c ",
"a b c"},
314TEST_CASE(
"StrTrimInPlace")
316 for (
auto [input, expected] : _str_trim_testcases) {
318 CHECK(input == expected);
322TEST_CASE(
"StrTrimView") {
323 for (
const auto& [input, expected] : _str_trim_testcases) {
331static std::string FixSCCEncodedWrapper(
const std::string &str,
bool fix_code)
333 std::string result = str;
345static void ComposePart(
StringBuilder &builder,
const std::string &value)
351template <
typename... Args>
352static std::string Compose(Args &&... args)
356 (ComposePart(builder, args), ...);
360TEST_CASE(
"FixSCCEncoded")
363 CHECK(FixSCCEncodedWrapper(
"",
false) ==
"");
366 CHECK(FixSCCEncodedWrapper(
"\uE0280",
true) == Compose(
SCC_ENCODED,
"0"));
369 CHECK(FixSCCEncodedWrapper(
"\uE0280:\uE0281",
true) == Compose(
SCC_ENCODED,
"0", SCC_RECORD_SEPARATOR,
SCC_ENCODED,
"1"));
372 CHECK(FixSCCEncodedWrapper(
"\uE0001",
false) == Compose(
SCC_ENCODED,
"1"));
390 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"));
393 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"));
399static std::string FixSCCEncodedNegativeWrapper(
const std::string &str)
401 std::string result = str;
406TEST_CASE(
"FixSCCEncodedNegative")
411 CHECK(FixSCCEncodedNegativeWrapper(
"") ==
"");
412 CHECK(FixSCCEncodedNegativeWrapper(positive) == positive);
413 CHECK(FixSCCEncodedNegativeWrapper(negative) == positive);
416TEST_CASE(
"EncodedString::ReplaceParam - positive")
421 CHECK(string1 != string2);
425 CHECK(string2 == string3);
428TEST_CASE(
"EncodedString::ReplaceParam - negative")
434 CHECK(string1 != string2);
436 CHECK(string1 == string3);
440 CHECK(string2 == string4);
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.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
@ 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.
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
bool StrEndsWithIgnoreCase(std::string_view str, 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.
bool StrStartsWithIgnoreCase(std::string_view str, std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
int StrCompareIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s, while ignoring the case of the characters.
void FixSCCEncodedNegative(std::string &str)
Scan the string for SCC_ENCODED_NUMERIC with negative values, and reencode them as uint64_t.
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.