From 8d35aa3ecc2dd682225937bd48b3330c8f58ce98 Mon Sep 17 00:00:00 2001 From: Jief L Date: Sat, 25 Apr 2020 14:33:01 +0300 Subject: [PATCH] new trim method in XString. --- rEFIt_UEFI/cpp_foundation/XStringAbstract.h | 25 +++- rEFIt_UEFI/cpp_unit_test/XString_test.cpp | 128 ++++++++++++++------ 2 files changed, 109 insertions(+), 44 deletions(-) diff --git a/rEFIt_UEFI/cpp_foundation/XStringAbstract.h b/rEFIt_UEFI/cpp_foundation/XStringAbstract.h index 87e261c77..a250bf4ea 100755 --- a/rEFIt_UEFI/cpp_foundation/XStringAbstract.h +++ b/rEFIt_UEFI/cpp_foundation/XStringAbstract.h @@ -49,9 +49,12 @@ struct XStringAbstract__true_type { template struct XStringAbstract__make_unsigned {}; -template <> struct XStringAbstract__make_unsigned< char> {typedef unsigned char type;}; -template <> struct XStringAbstract__make_unsigned< signed char> {typedef unsigned char type;}; -template <> struct XStringAbstract__make_unsigned {typedef unsigned char type;}; +template <> struct XStringAbstract__make_unsigned< char> {typedef unsigned char type;}; +template <> struct XStringAbstract__make_unsigned< signed char> {typedef unsigned char type;}; +template <> struct XStringAbstract__make_unsigned {typedef unsigned char type;}; +template <> struct XStringAbstract__make_unsigned< char16_t> {typedef char16_t type;}; +template <> struct XStringAbstract__make_unsigned< char32_t> {typedef char32_t type;}; +template <> struct XStringAbstract__make_unsigned< wchar_t> {typedef wchar_t type;}; template <> struct XStringAbstract__make_unsigned< signed short> {typedef unsigned short type;}; template <> struct XStringAbstract__make_unsigned {typedef unsigned short type;}; template <> struct XStringAbstract__make_unsigned< signed int> {typedef unsigned int type;}; @@ -535,16 +538,28 @@ public: //--------------------------------------------------------------------- - XStringAbstract lowerAscii() + void lowerAscii() { T* s = m_data; while ( *s ) { *s = asciiToLower(*s); s++; } - return *this; } + void trim() + { + T* start = 0; + size_t count = 0; + T* s = m_data; + while ( *s && unsigned_type(T)(*s) <= 32 ) s++; + start = s; + while ( *s && unsigned_type(T)(*s) > 32 ) s++; + count = uintptr_t(s - start); + memmove(m_data, start, count*sizeof(T)); + m_data[count] = 0; + } + // void deleteCountCharsAt(size_t pos, size_t count=1); //{ // if ( pos < size() ) { diff --git a/rEFIt_UEFI/cpp_unit_test/XString_test.cpp b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp index 2400aa689..c963ef108 100755 --- a/rEFIt_UEFI/cpp_unit_test/XString_test.cpp +++ b/rEFIt_UEFI/cpp_unit_test/XString_test.cpp @@ -172,57 +172,71 @@ struct TestStringMultiCoded TestString utf16; TestString utf32; TestString wchar; - -// template -// TestStringRet& getTestString4Encoding(const char* enc) { -// if ( strcmp(enc, "utf8") == 0 ) return utf8; -// if ( strcmp(enc, "utf16") == 0 ) return utf16; -// if ( strcmp(enc, "utf32") == 0 ) return utf32; -// if ( strcmp(enc, "wchar") == 0 ) return wchar; -// panic(); -// } }; #define nbchar(s) (sizeof(s)/sizeof(*s)-1) -#define testStringArray_LINE(utf) { \ +#define testStringArray_LINE(utf) \ TestString(nbchar(utf), utf, nbchar(PREFIX_U(utf)), PREFIX_U(utf)), \ TestString(nbchar(PREFIX_u(utf)), PREFIX_u(utf), nbchar(PREFIX_U(utf)), PREFIX_U(utf)), \ TestString(nbchar(PREFIX_U(utf)), PREFIX_U(utf), nbchar(PREFIX_U(utf)), PREFIX_U(utf)), \ TestString(nbchar(PREFIX_L(utf)), PREFIX_L(utf), nbchar(PREFIX_U(utf)), PREFIX_U(utf)), \ -} + + +struct TestStringMultiCodedAndExpectedResult +{ + TestString utf8; + TestString utf16; + TestString utf32; + TestString wchar; + TestString utf8_expectedResult; + TestString utf16_expectedResult; + TestString utf32_expectedResult; + TestString wchar_expectedResult; +}; + +#define testStringExpectedArray_LINE(utf, expected) \ + testStringArray_LINE(utf) testStringArray_LINE(expected) //TestString foo("", "utf8", 1, "a", 1, U"a"); const TestStringMultiCoded testStringMultiCodedArray[] = { - testStringArray_LINE(""), - testStringArray_LINE("a"), + { testStringArray_LINE("") }, + { testStringArray_LINE("a") }, #ifndef _MSC_VER - testStringArray_LINE(utf8_1), - testStringArray_LINE(utf8_2), - testStringArray_LINE(utf8_3), - testStringArray_LINE(utf8_4), - testStringArray_LINE(utf8_5), - testStringArray_LINE(utf8_6), - testStringArray_LINE(utf8_7), - testStringArray_LINE(utf8_8), -// testStringArray_LINE(utf8_9), -// testStringArray_LINE(utf8_10), -// testStringArray_LINE(utf8_11), + { testStringArray_LINE(utf8_1) }, + { testStringArray_LINE(utf8_2) }, + { testStringArray_LINE(utf8_3) }, + { testStringArray_LINE(utf8_4) }, + { testStringArray_LINE(utf8_5) }, + { testStringArray_LINE(utf8_6) }, + { testStringArray_LINE(utf8_7) }, + { testStringArray_LINE(utf8_8) }, +// { testStringArray_LINE(utf8_9) }, +// { testStringArray_LINE(utf8_10) }, +// { testStringArray_LINE(utf8_11) }, #endif }; size_t nbTestStringMultiCoded = *(&testStringMultiCodedArray + 1) - testStringMultiCodedArray; const TestStringMultiCoded testStringMultiCoded4CaseArray[] = { - testStringArray_LINE("ABCDEF"), - testStringArray_LINE("abcdeFGHIjklmn"), + { testStringArray_LINE("A൧൨BCギDEизF") }, + { testStringArray_LINE("abc聯輦deFGꇊHIjklmn") }, }; - size_t nbTestStringMultiCoded4CaseArray = *(&testStringMultiCoded4CaseArray + 1) - testStringMultiCoded4CaseArray; +const TestStringMultiCodedAndExpectedResult testStringMultiCoded4TrimArray[] = { + { testStringExpectedArray_LINE(" A൧൨BCギDEизF", "A൧൨BCギDEизF") }, + { testStringExpectedArray_LINE("A൧൨BCギDEизF ", "A൧൨BCギDEизF") }, + { testStringExpectedArray_LINE(" A൧൨BCギDEизF ", "A൧൨BCギDEизF") }, + { testStringExpectedArray_LINE("\1 \31 abc聯輦deFGꇊHIjklmn \1 \31 ", "abc聯輦deFGꇊHIjklmn") }, +}; + +size_t nbTestStringMultiCoded4TrimArray = *(&testStringMultiCoded4TrimArray + 1) - testStringMultiCoded4TrimArray; + template @@ -1337,6 +1351,41 @@ SimpleString testlastChar_(const InitialValue& initialValue) +/***************************** trim *****************************/ +template +SimpleString testtrim_(const InitialValue& initialValue, const ExpectedValue& expectedValue) +{ + TEST_TITLE(displayOnlyFailed, ssprintf("Test %s::trim(%s\"%s\"", XStringClassInfo::xStringClassName, XStringClassInfo::prefix, SimpleString(initialValue.cha).c_str())); + +// typedef typename XStringClassInfo::ch_t ch_t; +// ch_t c; // dummy for call utf function + + XStringClass str; + str.takeValueFrom(initialValue.cha); + + char32_t expectedChar = 0; + if ( initialValue.utf32_length > 0) expectedChar = initialValue.utf32[initialValue.utf32_length-1]; + + str.trim(); + + CHECK_RESULT(str.strcmp(expectedValue.cha) == 0, + ssprintf("str.strcmp(expectedValue.cha) == 0 (\"%s\")", SimpleString(expectedValue.cha).c_str()), + ssprintf("str.strcmp(expectedValue.cha) != 0 (\"%s\"!=\"%s\")", SimpleString(str.c_str()).c_str(), SimpleString(expectedValue.cha).c_str()) + ); +//str.takeValueFrom(initialValue.cha); +//str.trim(); + + return SimpleString(); +} + +#define testtrim(XStringClass, classEncoding) \ + printf("Test %s::testtrim\n", STRINGIFY(XStringClass)); \ + for ( size_t i = 0 ; i < nbTestStringMultiCoded4TrimArray ; i++ ) { \ + testtrim_(testStringMultiCoded4TrimArray[i].classEncoding, testStringMultiCoded4TrimArray[i].classEncoding##_expectedResult); \ + } \ + + + /***************************** *****************************/ #undef realloc // @@ -1414,18 +1463,18 @@ size_t utf32_size = sizeof(U"ギ") - 1; (void)utf32_size; // this char is 6 b #ifdef _MSC_VER //SetConsoleOutputCP(65001); #endif - -printf("%s", "Выход \n"); -XString ddd = "Выход "_XS; -printf(" xstring %s, asize=%zu, sizeinbyte=%zu sizeof=%zu lastcharat=%zu\n", ddd.c_str(), ddd.allocatedSize(), ddd.sizeInBytes(), sizeof(ddd), ddd.indexOf(ddd.lastChar())); - -TestString ts1 = TestString(nbchar("Выход "), "Выход ", nbchar(PREFIX_U("Выход ")), PREFIX_U("Выход ")); -testlastChar_(ts1); +// +//printf("%s", "Выход \n"); +//XString ddd = "Выход "_XS; +//printf(" xstring %s, asize=%zu, sizeinbyte=%zu sizeof=%zu lastcharat=%zu\n", ddd.c_str(), ddd.allocatedSize(), ddd.sizeInBytes(), sizeof(ddd), ddd.indexOf(ddd.lastChar())); +// +//TestString ts1 = TestString(nbchar("Выход "), "Выход ", nbchar(PREFIX_U("Выход ")), PREFIX_U("Выход ")); +//testlastChar_(ts1); //teststrncpy_("utf8", testStringMultiCodedArray[1].utf8, testStringMultiCodedArray[1].wchar); //testindexOf(XString, utf8, utf16); -testCompare(XString, utf8, utf16); +//testCompare(XString, utf8, utf16); //testindexOf_(testStringMultiCoded4CaseArray[0].utf8); @@ -1444,10 +1493,11 @@ testCompare(XString, utf8, utf16); // TEST_ALL_CLASSES(teststrncat, TEST_ALL_UTF_ALL_UTF); // 2101632 tests // // TEST_ALL_CLASSES(testSubString, __TEST0); - TEST_ALL_CLASSES(testCompare, TEST_ALL_UTF); - TEST_ALL_CLASSES(testindexOf, TEST_ALL_UTF); - - TEST_ALL_CLASSES(testlastChar, __TEST0); +// TEST_ALL_CLASSES(testCompare, TEST_ALL_UTF); +// TEST_ALL_CLASSES(testindexOf, TEST_ALL_UTF); +// +// TEST_ALL_CLASSES(testlastChar, __TEST0); + TEST_ALL_CLASSES(testtrim, __TEST0);