Improve Split in XStringArray.

This commit is contained in:
jief 2020-04-26 14:52:10 +03:00
parent 7d43ca70d5
commit 2ccc95513e
5 changed files with 146 additions and 62 deletions

View File

@ -7,8 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
9A0B08562402FF7700E2B470 /* XStringWArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08472402FE9300E2B470 /* XStringWArray.cpp */; };
9A0B08582402FF7F00E2B470 /* XStringWArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */; };
9A0B08582402FF7F00E2B470 /* XStringArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringArray_test.cpp */; };
9A0B08592402FF8200E2B470 /* all_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08512402FE9B00E2B470 /* all_tests.cpp */; };
9A0B085A2402FF8400E2B470 /* XObjArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08522402FE9B00E2B470 /* XObjArray_tests.cpp */; };
9A0B085B2402FF8700E2B470 /* XArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08532402FE9B00E2B470 /* XArray_tests.cpp */; };
@ -16,10 +15,9 @@
9A0B08662403144C00E2B470 /* global_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08642403144C00E2B470 /* global_test.cpp */; };
9A0B08732403B08400E2B470 /* XObjArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08522402FE9B00E2B470 /* XObjArray_tests.cpp */; };
9A0B08742403B08400E2B470 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9223302402FD1000483CBA /* main.cpp */; };
9A0B08772403B08400E2B470 /* XStringWArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08472402FE9300E2B470 /* XStringWArray.cpp */; };
9A0B087A2403B08400E2B470 /* all_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08512402FE9B00E2B470 /* all_tests.cpp */; };
9A0B087B2403B08400E2B470 /* global_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08642403144C00E2B470 /* global_test.cpp */; };
9A0B087D2403B08400E2B470 /* XStringWArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */; };
9A0B087D2403B08400E2B470 /* XStringArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringArray_test.cpp */; };
9A0B087E2403B08400E2B470 /* XArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08532402FE9B00E2B470 /* XArray_tests.cpp */; };
9A0B087F2403B08400E2B470 /* Platform.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B085D240300E000E2B470 /* Platform.cpp */; };
9A28CD09241B8DD400F3D247 /* strncmp_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A28CD05241B8DD400F3D247 /* strncmp_test.cpp */; };
@ -57,10 +55,9 @@
9A4FFA852451C9740050B38B /* XString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A4FFA7C2451C8330050B38B /* XString.cpp */; };
9A57C21A2418B9A00029A39F /* XObjArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08522402FE9B00E2B470 /* XObjArray_tests.cpp */; };
9A57C21B2418B9A00029A39F /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9223302402FD1000483CBA /* main.cpp */; };
9A57C21D2418B9A00029A39F /* XStringWArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08472402FE9300E2B470 /* XStringWArray.cpp */; };
9A57C2222418B9A00029A39F /* all_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08512402FE9B00E2B470 /* all_tests.cpp */; };
9A57C2232418B9A00029A39F /* global_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08642403144C00E2B470 /* global_test.cpp */; };
9A57C2252418B9A00029A39F /* XStringWArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */; };
9A57C2252418B9A00029A39F /* XStringArray_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B084F2402FE9B00E2B470 /* XStringArray_test.cpp */; };
9A57C2272418B9A00029A39F /* XArray_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B08532402FE9B00E2B470 /* XArray_tests.cpp */; };
9A57C2282418B9A00029A39F /* Platform.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A0B085D240300E000E2B470 /* Platform.cpp */; };
9A9223312402FD1000483CBA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9223302402FD1000483CBA /* main.cpp */; };
@ -106,13 +103,11 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
9A0B08442402FE9300E2B470 /* XStringWArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XStringWArray.h; sourceTree = "<group>"; };
9A0B08472402FE9300E2B470 /* XStringWArray.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = XStringWArray.cpp; sourceTree = "<group>"; };
9A0B084A2402FE9300E2B470 /* XArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XArray.h; sourceTree = "<group>"; };
9A0B084B2402FE9300E2B470 /* XObjArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XObjArray.h; sourceTree = "<group>"; };
9A0B084D2402FE9B00E2B470 /* XStringWArray_test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XStringWArray_test.h; sourceTree = "<group>"; };
9A0B084D2402FE9B00E2B470 /* XStringArray_test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XStringArray_test.h; sourceTree = "<group>"; };
9A0B084E2402FE9B00E2B470 /* XArray_tests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XArray_tests.h; sourceTree = "<group>"; };
9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = XStringWArray_test.cpp; sourceTree = "<group>"; };
9A0B084F2402FE9B00E2B470 /* XStringArray_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = XStringArray_test.cpp; sourceTree = "<group>"; };
9A0B08502402FE9B00E2B470 /* XObjArray_tests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XObjArray_tests.h; sourceTree = "<group>"; };
9A0B08512402FE9B00E2B470 /* all_tests.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = all_tests.cpp; sourceTree = "<group>"; };
9A0B08522402FE9B00E2B470 /* XObjArray_tests.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = XObjArray_tests.cpp; sourceTree = "<group>"; };
@ -212,8 +207,6 @@
9A0B084B2402FE9300E2B470 /* XObjArray.h */,
9A4185BE2439F73A00BEAFB8 /* XStringArray.cpp */,
9A4185BF2439F73A00BEAFB8 /* XStringArray.h */,
9A0B08472402FE9300E2B470 /* XStringWArray.cpp */,
9A0B08442402FE9300E2B470 /* XStringWArray.h */,
);
name = cpp_foundation;
path = ../../rEFIt_UEFI/cpp_foundation;
@ -249,8 +242,8 @@
9A0B084E2402FE9B00E2B470 /* XArray_tests.h */,
9A0B08522402FE9B00E2B470 /* XObjArray_tests.cpp */,
9A0B08502402FE9B00E2B470 /* XObjArray_tests.h */,
9A0B084F2402FE9B00E2B470 /* XStringWArray_test.cpp */,
9A0B084D2402FE9B00E2B470 /* XStringWArray_test.h */,
9A0B084F2402FE9B00E2B470 /* XStringArray_test.cpp */,
9A0B084D2402FE9B00E2B470 /* XStringArray_test.h */,
);
name = cpp_unit_test;
path = ../../rEFIt_UEFI/cpp_unit_test;
@ -452,14 +445,13 @@
9A28CD4C241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */,
9A0B08742403B08400E2B470 /* main.cpp in Sources */,
9A28CD17241BACBB00F3D247 /* strlen_test.cpp in Sources */,
9A0B08772403B08400E2B470 /* XStringWArray.cpp in Sources */,
9A28CD0D241B8DD400F3D247 /* strcmp_test.cpp in Sources */,
9A28CD0A241B8DD400F3D247 /* strncmp_test.cpp in Sources */,
9A0B087A2403B08400E2B470 /* all_tests.cpp in Sources */,
9A9AEB8E243F752C00FBD7D8 /* unicode_conversions.cpp in Sources */,
9A0B087B2403B08400E2B470 /* global_test.cpp in Sources */,
9ACAB1192426255C00BDB3CF /* printf_lite.c in Sources */,
9A0B087D2403B08400E2B470 /* XStringWArray_test.cpp in Sources */,
9A0B087D2403B08400E2B470 /* XStringArray_test.cpp in Sources */,
9A28CD11241B9EF800F3D247 /* strcmp.cpp in Sources */,
9A4185C12439F73A00BEAFB8 /* XStringArray.cpp in Sources */,
9A0B087E2403B08400E2B470 /* XArray_tests.cpp in Sources */,
@ -481,14 +473,13 @@
9A28CD4D241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */,
9A57C21B2418B9A00029A39F /* main.cpp in Sources */,
9A28CD18241BACBB00F3D247 /* strlen_test.cpp in Sources */,
9A57C21D2418B9A00029A39F /* XStringWArray.cpp in Sources */,
9A28CD0E241B8DD400F3D247 /* strcmp_test.cpp in Sources */,
9A28CD0B241B8DD400F3D247 /* strncmp_test.cpp in Sources */,
9A57C2222418B9A00029A39F /* all_tests.cpp in Sources */,
9A9AEB8F243F752C00FBD7D8 /* unicode_conversions.cpp in Sources */,
9A57C2232418B9A00029A39F /* global_test.cpp in Sources */,
9ACAB11A2426255C00BDB3CF /* printf_lite.c in Sources */,
9A57C2252418B9A00029A39F /* XStringWArray_test.cpp in Sources */,
9A57C2252418B9A00029A39F /* XStringArray_test.cpp in Sources */,
9A28CD12241B9EF800F3D247 /* strcmp.cpp in Sources */,
9A4185C22439F73A00BEAFB8 /* XStringArray.cpp in Sources */,
9A57C2272418B9A00029A39F /* XArray_tests.cpp in Sources */,
@ -509,14 +500,13 @@
9A28CD4B241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */,
9A9223312402FD1000483CBA /* main.cpp in Sources */,
9A28CD16241BACBB00F3D247 /* strlen_test.cpp in Sources */,
9A0B08562402FF7700E2B470 /* XStringWArray.cpp in Sources */,
9A28CD0C241B8DD400F3D247 /* strcmp_test.cpp in Sources */,
9A28CD09241B8DD400F3D247 /* strncmp_test.cpp in Sources */,
9A0B08592402FF8200E2B470 /* all_tests.cpp in Sources */,
9A0B08662403144C00E2B470 /* global_test.cpp in Sources */,
9A4185B12439E4D600BEAFB8 /* LoadOptions_test.cpp in Sources */,
9ACAB117242623EE00BDB3CF /* printf_lite.c in Sources */,
9A0B08582402FF7F00E2B470 /* XStringWArray_test.cpp in Sources */,
9A0B08582402FF7F00E2B470 /* XStringArray_test.cpp in Sources */,
9A9AEB8D243F73CE00FBD7D8 /* unicode_conversions.cpp in Sources */,
9A4185C02439F73A00BEAFB8 /* XStringArray.cpp in Sources */,
9A28CD10241B9EF800F3D247 /* strcmp.cpp in Sources */,

View File

@ -95,6 +95,32 @@ int XStringAbstract__compare(const S* src, const O* other, bool ignoreCase)
return src_char32 > other_char32 ? 1 : -1;
}
template<typename S, typename O>
int XStringAbstract__ncompare(const S* src, const O* other, size_t n, bool ignoreCase)
{
if ( n == 0 ) return 0; // string of 0 length are equal.
const S* src2 = src;
const O* other2 = other;
char32_t src_char32;
char32_t other_char32;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
size_t nb = 1;
while ( src_char32 && nb < n ) {
if ( ignoreCase ) {
src_char32 = asciiToLower(src_char32);
other_char32 = asciiToLower(other_char32);
}
if ( src_char32 != other_char32 ) break;
src2 = get_char32_from_string(src2, &src_char32);
other2 = get_char32_from_string(other2, &other_char32);
nb += 1;
};
if ( src_char32 == other_char32 ) return 0;
return src_char32 > other_char32 ? 1 : -1;
}
template<typename O, typename P>
size_t XStringAbstract__indexOf(const O** s, const P* other, size_t offsetRet, bool toLower)
{

View File

@ -38,6 +38,8 @@ class XStringArray_/* : public XStringArraySuper*/
template<typename IntegralType, enable_if(is_integral(IntegralType))>
const XStringClass& operator [](IntegralType i) const { return array[i]; }
const XStringClass& dbg(size_t i) const { return array[i]; }
XStringClass ConcatAll(const XStringClass& Separator = ", "_XS, const XStringClass& Prefix = NullXString, const XStringClass& Suffix = NullXString) const
{
@ -230,21 +232,79 @@ extern const XStringWArray NullXStringWArray;
//};
template<class XStringArrayClass, typename CharType1, typename CharType2, enable_if(is_char(CharType1) && is_char(CharType2))>
XStringArrayClass Split(const CharType1* S, const CharType2* Separator)
{
XStringArrayClass xsArray;
size_t separatorLength = length_of_utf_string(Separator);
if ( separatorLength == 0 ) {
typename XStringArrayClass::XStringClass* xstr;
xstr = new typename XStringArrayClass::XStringClass;
xstr->takeValueFrom(S);
xsArray.AddReference(xstr, true);
return xsArray;
}
const CharType1* s = S;
char32_t char32 = 1;
do
{
while ( XStringAbstract__ncompare(s, Separator, separatorLength, false) == 0 ) {
// I have to implement a move_forward_one_char in unicode_conversions, as we don't care about char32
for ( size_t i = 0 ; i < separatorLength ; i++ ) s = get_char32_from_string(s, &char32);
}
const CharType1* t = s;
size_t nb = 0;
while ( char32 && XStringAbstract__ncompare(t, Separator, separatorLength, false) != 0 ) {
nb++;
t = get_char32_from_string(t, &char32);
}
typename XStringArrayClass::XStringClass* xstr;
xstr = new typename XStringArrayClass::XStringClass;
xstr->strncpy(s, nb);
xsArray.AddReference(xstr, true);
// s = get_char32_from_string(t, &char32);
s = t;
// Consume the separator we found
for ( size_t i = 0 ; i < separatorLength ; i++ ) s = get_char32_from_string(s, &char32);
} while ( char32 );
return xsArray;
//
//
// // TODO : Allocating temporary strings could be avoided by using low level function from unicode_conversions
// typename XStringArrayClass::XStringClass SS;
// SS.takeValueFrom(S);
// typename XStringArrayClass::XStringClass XSeparator;
// SS.takeValueFrom(Separator);
// return Split<XStringArrayClass>(SS, XSeparator);
};
template<class XStringArrayClass, class XStringClass1, class XStringClass2, enable_if(!is_char(XStringClass1) && !is_char_ptr(XStringClass1) && !is_char(XStringClass2))>
XStringArrayClass Split(const XStringClass1& S, const XStringClass2& Separator)
{
XStringArrayClass Ss;
size_t idxB, idxE;
idxB = 0;
idxE = S.indexOf(Separator, idxB);
while ( idxE != MAX_XSIZE ) {
Ss.Add(S.subString(idxB, idxE-idxB));
idxB = idxE + Separator.length();
idxE = S.indexOf(Separator, idxB);
}
if ( idxB < S.length() ) Ss.Add(S.subString(idxB, S.length()-idxB));
return Ss;
return Split<XStringArrayClass>(S.s(), Separator.s());
//
// XStringArrayClass Ss;
// size_t idxB, idxE;
//
// if ( Separator.length() == 0 ) {
// Ss.Add(S);
// return Ss;
// }
// idxB = 0;
// idxE = S.indexOf(Separator, idxB);
// while ( idxE != MAX_XSIZE ) {
// Ss.Add(S.subString(idxB, idxE-idxB));
// idxB = idxE + Separator.length();
// idxE = S.indexOf(Separator, idxB);
// }
// if ( idxB < S.length() ) Ss.Add(S.subString(idxB, S.length()-idxB));
// return Ss;
};
@ -254,16 +314,4 @@ XStringArrayClass Split(const XStringClass1& S)
return Split<XStringArrayClass>(S, ", "_XS);
};
template<class XStringArrayClass, typename CharType1, typename CharType2, enable_if(is_char(CharType1) && is_char(CharType2))>
XStringArrayClass Split(const CharType1* S, const CharType2* Separator)
{
// TODO : Allocating temporary strings could be avoided by using low level function from unicode_conversions
typename XStringArrayClass::XStringClass SS;
SS.takeValueFrom(S);
typename XStringArrayClass::XStringClass XSeparator;
SS.takeValueFrom(Separator);
return Split<XStringArrayClass>(SS, XSeparator);
};
#endif

View File

@ -14,10 +14,9 @@ int XStringArray_tests()
array1.Add(L"1"_XSW);
if ( array1.isEmpty() ) return 2;
if ( array1[0] != "1"_XS ) return 21;
array1.Add(L"2"_XSW);
if ( array1[0] != L"1"_XSW ) return 3;
if ( array1[1] != L"2"_XSW ) return 4;
if ( array1[1] != "2"_XS ) return 21;
if ( !array1.contains(L"2"_XSW) ) return 5;
@ -30,6 +29,26 @@ int XStringArray_tests()
if ( !(array1 == array1bis) ) return 10;
if ( array1 != array1bis ) return 11;
}
// Split
{
XStringArray array = Split<XStringArray>(" word1 word2 word3 ", " ");
if ( array[0] != "word1"_XS ) return 31;
if ( array[1] != "word2"_XS ) return 32;
if ( array[2] != "word3"_XS ) return 33;
}
{
XStringArray array = Split<XStringArray>("word1, word2, word3", ", ");
if ( array[0] != "word1"_XS ) return 31;
if ( array[1] != "word2"_XS ) return 32;
if ( array[2] != "word3"_XS ) return 33;
}
{
XStringArray array = Split<XStringArray>(" word1 word2 word3 "_XS, " "_XS);
if ( array[0] != "word1"_XS ) return 31;
if ( array[1] != "word2"_XS ) return 32;
if ( array[2] != "word3"_XS ) return 33;
}
// Test concat and Split
{
@ -45,10 +64,6 @@ int XStringArray_tests()
if ( array1 != array2bis ) return 20;
XStringArray array3bis = Split<XStringArray>(c);
if ( array1 != array3bis ) return 20;
char* c2;// = "a, b, c";
XStringArray array4 = Split<XStringArray>(c2, " ");
}
XStringWArray array2;

View File

@ -31,11 +31,11 @@ bool all_tests()
bool all_ok = true;
int ret;
ret = XString_tests();
if ( ret != 0 ) {
printf("XString16_tests() failed at test %d\n", ret);
all_ok = false;
}
// ret = XString_tests();
// if ( ret != 0 ) {
// printf("XString16_tests() failed at test %d\n", ret);
// all_ok = false;
// }
//return ret;
// ret = XUINTN_tests();
// if ( ret != 0 ) {
@ -66,11 +66,6 @@ bool all_tests()
printf("strlen_tests() failed at test %d\n", ret);
all_ok = false;
}
ret = BootOptions_tests();
if ( ret != 0 ) {
printf("BootOptions_tests() failed at test %d\n", ret);
all_ok = false;
}
ret = strcmp_tests();
if ( ret != 0 ) {
printf("strcmp_tests() failed at test %d\n", ret);
@ -91,9 +86,19 @@ bool all_tests()
printf("XObjArray_tests() failed at test %d\n", ret);
all_ok = false;
}
// ret = XString_tests();
// if ( ret != 0 ) {
// printf("XString_tests() failed at test %d\n", ret);
// all_ok = false;
// }
ret = XStringArray_tests();
if ( ret != 0 ) {
printf("XStringWArray_tests() failed at test %d\n", ret);
printf("XStringArray_tests() failed at test %d\n", ret);
all_ok = false;
}
ret = BootOptions_tests();
if ( ret != 0 ) {
printf("BootOptions_tests() failed at test %d\n", ret);
all_ok = false;
}
// ret = XUINTN_tests();