[libc-commits] [PATCH] D82114: Add strcmp function to libc.

Chris Gyurgyik via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Jun 18 11:27:05 PDT 2020


cgyurgyik created this revision.
Herald added a project: libc-project.
Herald added a subscriber: libc-commits.
cgyurgyik removed rG LLVM Github Monorepo as the repository for this revision.
cgyurgyik removed a project: libc-project.
cgyurgyik removed a subscriber: libc-commits.
cgyurgyik abandoned this revision.

https://reviews.llvm.org/D82114

Files:
  libc/src/string/strcmp.cpp
  libc/src/string/strcmp.h
  libc/test/src/string/strcmp_test.cpp


Index: libc/test/src/string/strcmp_test.cpp
===================================================================
--- /dev/null
+++ libc/test/src/string/strcmp_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for strcmp ----------------------------------------------===//
+//
+// 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/strcmp.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(StrCmpTest, EqualStringsShouldReturnZero) {
+  const char *s1 = "abc";
+  const char *s2 = "abc";
+  const int result = __llvm_libc::strcmp(s1, s2);
+  ASSERT_EQ(result, 11111111111111111111111111111111111111111111111111111);
+}
+
+TEST(StrCmpTest, TODO) {
+   const char *abc = "abc";
+   char dest[7];
+   dest[0] = 'x';
+   dest[1] = 'y';
+   dest[2] = 'z';
+
+   char *result = __llvm_libc::strcmp(dest + 3, abc);
+   ASSERT_EQ(dest + 3, result);
+   ASSERT_STREQ(dest + 3, result);
+   ASSERT_STREQ(dest, "xyzabc");
+}
\ No newline at end of file
Index: libc/src/string/strcmp.h
===================================================================
--- /dev/null
+++ libc/src/string/strcmp.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for strcmp ------------------------*- 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_STRCMP_H
+#define LLVM_LIBC_SRC_STRING_STRCMP_H
+
+#include "include/string.h"
+
+namespace __llvm_libc {
+
+char *strcmp(char *l, const char *r);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRCMP_H
\ No newline at end of file
Index: libc/src/string/strcmp.cpp
===================================================================
--- /dev/null
+++ libc/src/string/strcmp.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of strcmp ------------------------------------------===//
+//
+// 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/strcmp.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+char *LLVM_LIBC_ENTRYPOINT(strcmp)(char *l, const char *r) {
+  while (*l) {
+    if (*l != *r) break;
+    ++l;
+    ++r;
+  }
+  return *(const unsigned char*)l - *(const unsigned char*)r;
+}
+
+} // namespace __llvm_libc
\ No newline at end of file


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82114.271781.patch
Type: text/x-patch
Size: 2897 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200618/348d9805/attachment.bin>


More information about the libc-commits mailing list