[libc-commits] [libc] f418f88 - [libc] add locale free strcoll
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Fri Oct 28 11:13:13 PDT 2022
Author: Michael Jones
Date: 2022-10-28T11:13:07-07:00
New Revision: f418f88824905c372cb9252b3f675012a3fc799d
URL: https://github.com/llvm/llvm-project/commit/f418f88824905c372cb9252b3f675012a3fc799d
DIFF: https://github.com/llvm/llvm-project/commit/f418f88824905c372cb9252b3f675012a3fc799d.diff
LOG: [libc] add locale free strcoll
The strcoll function is intended to compare strings based on their
ordering in the current locale. Since the locale facilities have not yet
been added, a simple implementation that is the same as strcmp has been
added as a placeholder.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D136802
Added:
libc/src/string/strcoll.cpp
libc/src/string/strcoll.h
libc/test/src/string/strcoll_test.cpp
Modified:
libc/config/linux/x86_64/entrypoints.txt
libc/src/string/CMakeLists.txt
libc/test/src/string/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 18594e500c936..40c6c33ab05c9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -42,6 +42,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strcat
libc.src.string.strchr
libc.src.string.strcmp
+ libc.src.string.strcoll
libc.src.string.strcpy
libc.src.string.strcspn
libc.src.string.strdup
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index ab579afa4cfd9..624afb4d470a6 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -96,6 +96,14 @@ add_entrypoint_object(
strcmp.h
)
+add_entrypoint_object(
+ strcoll
+ SRCS
+ strcoll.cpp
+ HDRS
+ strcoll.h
+)
+
add_entrypoint_object(
strcpy
SRCS
diff --git a/libc/src/string/strcoll.cpp b/libc/src/string/strcoll.cpp
new file mode 100644
index 0000000000000..d60a7a5d5915a
--- /dev/null
+++ b/libc/src/string/strcoll.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of strcoll -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strcoll.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// TODO: Add support for locales.
+LLVM_LIBC_FUNCTION(int, strcoll, (const char *left, const char *right)) {
+ for (; *left && *left == *right; ++left, ++right)
+ ;
+ return static_cast<int>(*left) - static_cast<int>(*right);
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/string/strcoll.h b/libc/src/string/strcoll.h
new file mode 100644
index 0000000000000..5949ede61bd19
--- /dev/null
+++ b/libc/src/string/strcoll.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for strcoll -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRING_STRCOLL_H
+#define LLVM_LIBC_SRC_STRING_STRCOLL_H
+
+namespace __llvm_libc {
+
+int strcoll(const char *left, const char *right);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRCOLL_H
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index bbd990935aa5d..b0b352913cc94 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -92,6 +92,16 @@ add_libc_unittest(
libc.src.string.strcmp
)
+add_libc_unittest(
+ strcoll_test
+ SUITE
+ libc_string_unittests
+ SRCS
+ strcoll_test.cpp
+ DEPENDS
+ libc.src.string.strcoll
+)
+
add_libc_unittest(
strcpy_test
SUITE
diff --git a/libc/test/src/string/strcoll_test.cpp b/libc/test/src/string/strcoll_test.cpp
new file mode 100644
index 0000000000000..cb99f0a509b25
--- /dev/null
+++ b/libc/test/src/string/strcoll_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for strcoll ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strcoll.h"
+#include "utils/UnitTest/Test.h"
+
+// TODO: Add more comprehensive tests once locale support is added.
+
+TEST(LlvmLibcStrcollTest, SimpleTest) {
+ const char *s1 = "abc";
+ const char *s2 = "abc";
+ const char *s3 = "def";
+ int result = __llvm_libc::strcoll(s1, s2);
+ ASSERT_EQ(result, 0);
+
+ // Verify operands reversed.
+ result = __llvm_libc::strcoll(s2, s1);
+ ASSERT_EQ(result, 0);
+
+ result = __llvm_libc::strcoll(s1, s3);
+ ASSERT_LT(result, 0);
+
+ result = __llvm_libc::strcoll(s3, s1);
+ ASSERT_GT(result, 0);
+}
More information about the libc-commits
mailing list