aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/Manual/en/BH_String.pod16
-rw-r--r--doc/Manual/ru/BH_String.pod16
-rw-r--r--include/BH/String.h8
-rw-r--r--src/String/String.c29
-rw-r--r--test/tests/TestString.c112
5 files changed, 181 insertions, 0 deletions
diff --git a/doc/Manual/en/BH_String.pod b/doc/Manual/en/BH_String.pod
index c7b027e..a88d796 100644
--- a/doc/Manual/en/BH_String.pod
+++ b/doc/Manual/en/BH_String.pod
@@ -384,6 +384,22 @@ If successful, it returns the converted number or 0.
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
L<BH>
diff --git a/doc/Manual/ru/BH_String.pod b/doc/Manual/ru/BH_String.pod
index 4e665d5..2c78942 100644
--- a/doc/Manual/ru/BH_String.pod
+++ b/doc/Manual/ru/BH_String.pod
@@ -375,6 +375,22 @@ I<string> (с ограничением по длинне I<size>).
Создает копию строки 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 СМ. ТАКЖЕ
L<BH>
diff --git a/include/BH/String.h b/include/BH/String.h
index 4ef0c33..f9ef2bc 100644
--- a/include/BH/String.h
+++ b/include/BH/String.h
@@ -116,4 +116,12 @@ uint64_t BH_StringToInt64u(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 */
diff --git a/src/String/String.c b/src/String/String.c
index 74f40e2..3bc0337 100644
--- a/src/String/String.c
+++ b/src/String/String.c
@@ -1,4 +1,5 @@
#include <BH/String.h>
+#include <BH/Unicode.h>
#include <string.h>
#include <stdlib.h>
@@ -17,3 +18,31 @@ char *BH_StringDup(const char *string)
memcpy(result, string, length);
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;
+}
diff --git a/test/tests/TestString.c b/test/tests/TestString.c
new file mode 100644
index 0000000..270b7eb
--- /dev/null
+++ b/test/tests/TestString.c
@@ -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();
+}