Add string compare functions
This commit is contained in:
@@ -384,6 +384,22 @@ If successful, it returns the converted number or 0.
|
|||||||
Creates duplicate of the I<string> (simular to strdup).
|
Creates duplicate of the I<string> (simular to strdup).
|
||||||
|
|
||||||
|
|
||||||
|
=head2 BH_StringCompare
|
||||||
|
|
||||||
|
int BH_StringCompare(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
Compares two strings.
|
||||||
|
|
||||||
|
|
||||||
|
=head2 BH_StringCompareCaseless
|
||||||
|
|
||||||
|
int BH_StringCompareCaseless(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
Compares two case-insensitive strings.
|
||||||
|
|
||||||
|
|
||||||
=head1 SEE ALSO
|
=head1 SEE ALSO
|
||||||
|
|
||||||
L<BH>
|
L<BH>
|
||||||
|
|||||||
@@ -375,6 +375,22 @@ I<string> (с ограничением по длинне I<size>).
|
|||||||
Создает копию строки I<string> (схоже с strdup).
|
Создает копию строки I<string> (схоже с strdup).
|
||||||
|
|
||||||
|
|
||||||
|
=head2 BH_StringCompare
|
||||||
|
|
||||||
|
int BH_StringCompare(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
Сравнивает две строки.
|
||||||
|
|
||||||
|
|
||||||
|
=head2 bh_stringcompare не имеет значения
|
||||||
|
|
||||||
|
int BH_StringCompareCaseless(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
Сравнивает две строки без учета регистра.
|
||||||
|
|
||||||
|
|
||||||
=head1 СМ. ТАКЖЕ
|
=head1 СМ. ТАКЖЕ
|
||||||
|
|
||||||
L<BH>
|
L<BH>
|
||||||
|
|||||||
@@ -116,4 +116,12 @@ uint64_t BH_StringToInt64u(const char *string,
|
|||||||
char *BH_StringDup(const char *string);
|
char *BH_StringDup(const char *string);
|
||||||
|
|
||||||
|
|
||||||
|
int BH_StringCompare(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
|
||||||
|
int BH_StringCompareCaseless(const char *s1,
|
||||||
|
const char *s2);
|
||||||
|
|
||||||
|
|
||||||
#endif /* BH_STRING_H */
|
#endif /* BH_STRING_H */
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <BH/String.h>
|
#include <BH/String.h>
|
||||||
|
#include <BH/Unicode.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@@ -17,3 +18,31 @@ char *BH_StringDup(const char *string)
|
|||||||
memcpy(result, string, length);
|
memcpy(result, string, length);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int BH_StringCompare(const char *s1,
|
||||||
|
const char *s2)
|
||||||
|
{
|
||||||
|
return strcmp(s1, s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int BH_StringCompareCaseless(const char *s1,
|
||||||
|
const char *s2)
|
||||||
|
{
|
||||||
|
uint32_t c1, c2;
|
||||||
|
|
||||||
|
while (*s1 && *s2)
|
||||||
|
{
|
||||||
|
s1 += BH_UnicodeDecodeUtf8(s1, 4, &c1);
|
||||||
|
s2 += BH_UnicodeDecodeUtf8(s2, 4, &c2);
|
||||||
|
|
||||||
|
c1 = BH_UnicodeLower(c1);
|
||||||
|
c2 = BH_UnicodeLower(c2);
|
||||||
|
|
||||||
|
if (c1 != c2)
|
||||||
|
return (c1 < c2) ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (!*s1) ? (!*s2 ? 0 : -1) : 1;
|
||||||
|
}
|
||||||
|
|||||||
112
test/tests/TestString.c
Normal file
112
test/tests/TestString.c
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
#include <BH/Unit.h>
|
||||||
|
#include <BH/String.h>
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(EqualStrings)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompare("hello", "hello") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("", "") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("a", "a") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("123", "123") == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(LessThan)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompare("apple", "banana") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("aa", "ab") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("a", "b") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("", "a") < 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(GreaterThan)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompare("zebra", "apple") > 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("ab", "aa") > 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("b", "a") > 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("a", "") > 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(CaselessEqual)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("hello", "HELLO") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("Hello", "hELLO") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("TEST", "test") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("", "") == 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("MiXeD", "mIxEd") == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(CaselessLessThan)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("apple", "BANANA") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("alpha", "Beta") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("a", "Z") < 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(CaselessGreaterThan)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("zebra", "APPLE") > 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("Zulu", "alpha") > 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("Z", "a") > 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(EdgeCases)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompare(
|
||||||
|
"this is a very long string for testing purposes",
|
||||||
|
"this is a very long string for testing purposes") == 0);
|
||||||
|
|
||||||
|
BH_VERIFY(BH_StringCompare("abc123", "abc124") < 0);
|
||||||
|
|
||||||
|
BH_VERIFY(BH_StringCompare("test", "testing") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompare("testing", "test") > 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(CaselessEdgeCases)
|
||||||
|
{
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("TEST", "TESTING") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("TESTING", "test") > 0);
|
||||||
|
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("file1.txt", "file10.txt") < 0);
|
||||||
|
BH_VERIFY(BH_StringCompareCaseless("AaAa", "AAAA") == 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
|
||||||
|
BH_UNIT_ADD(EqualStrings);
|
||||||
|
BH_UNIT_ADD(LessThan);
|
||||||
|
BH_UNIT_ADD(GreaterThan);
|
||||||
|
BH_UNIT_ADD(EdgeCases);
|
||||||
|
BH_UNIT_ADD(CaselessEqual);
|
||||||
|
BH_UNIT_ADD(CaselessLessThan);
|
||||||
|
BH_UNIT_ADD(CaselessGreaterThan);
|
||||||
|
BH_UNIT_ADD(CaselessEdgeCases);
|
||||||
|
|
||||||
|
return BH_UnitRun();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user