diff --git a/libc/src/string/strcmp.h b/libc/src/string/strcmp.h new file mode 100644 --- /dev/null +++ b/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 diff --git a/libc/src/string/strcmp.cpp b/libc/src/string/strcmp.cpp new file mode 100644 --- /dev/null +++ b/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 diff --git a/libc/test/src/string/strcmp_test.cpp b/libc/test/src/string/strcmp_test.cpp new file mode 100644 --- /dev/null +++ b/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