Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@ if(LLVM_LIBC_FULL_BUILD)
# sched.h entrypoints
libc.src.sched.__sched_getcpucount

# strings.h entrypoints
libc.src.strings.strcasecmp_l
libc.src.strings.strncasecmp_l

# setjmp.h entrypoints
libc.src.setjmp.longjmp
libc.src.setjmp.setjmp
Expand Down
4 changes: 4 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,10 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.string.strcoll_l
libc.src.string.strxfrm_l

# strings.h entrypoints
libc.src.strings.strcasecmp_l
libc.src.strings.strncasecmp_l

# assert.h entrypoints
libc.src.assert.__assert_fail

Expand Down
18 changes: 18 additions & 0 deletions libc/include/strings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ header_template: strings.h.def
macros: []
types:
- type_name: size_t
- type_name: locale_t
enums: []
objects: []
functions:
Expand Down Expand Up @@ -68,6 +69,14 @@ functions:
arguments:
- type: const char *
- type: const char *
- name: strcasecmp_l
standards:
- BSDExtensions
return_type: int
arguments:
- type: const char *
- type: const char *
- type: locale_t
- name: strncasecmp
standards:
- BSDExtensions
Expand All @@ -76,3 +85,12 @@ functions:
- type: const char *
- type: const char *
- type: size_t
- name: strncasecmp_l
standards:
- BSDExtensions
return_type: int
arguments:
- type: const char *
- type: const char *
- type: size_t
- type: locale_t
24 changes: 24 additions & 0 deletions libc/src/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ add_entrypoint_object(
libc.src.string.memory_utils.inline_strcmp
)

add_entrypoint_object(
strcasecmp_l
SRCS
strcasecmp_l.cpp
HDRS
strcasecmp_l.h
DEPENDS
libc.hdr.types.locale_t
libc.src.__support.ctype_utils
libc.src.string.memory_utils.inline_strcmp
)

add_entrypoint_object(
strncasecmp
SRCS
Expand All @@ -125,3 +137,15 @@ add_entrypoint_object(
libc.src.__support.ctype_utils
libc.src.string.memory_utils.inline_strcmp
)

add_entrypoint_object(
strncasecmp_l
SRCS
strncasecmp_l.cpp
HDRS
strncasecmp_l.h
DEPENDS
libc.hdr.types.locale_t
libc.src.__support.ctype_utils
libc.src.string.memory_utils.inline_strcmp
)
27 changes: 27 additions & 0 deletions libc/src/strings/strcasecmp_l.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation of strcasecmp_l ------------------------------------===//
//
// 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/strings/strcasecmp_l.h"

#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_strcmp.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, strcasecmp_l,
(const char *left, const char *right, locale_t)) {
auto case_cmp = [](char a, char b) {
return LIBC_NAMESPACE::internal::tolower(a) -
LIBC_NAMESPACE::internal::tolower(b);
};
return inline_strcmp(left, right, case_cmp);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/strings/strcasecmp_l.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for strcasecmp_l ------------------*- 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_STRINGS_STRCASECMP_L_H
#define LLVM_LIBC_SRC_STRINGS_STRCASECMP_L_H

#include "hdr/types/locale_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int strcasecmp_l(const char *left, const char *right, locale_t locale);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRINGS_STRCASECMP_L_H
27 changes: 27 additions & 0 deletions libc/src/strings/strncasecmp_l.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation of strncasecmp_l -----------------------------------===//
//
// 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/strings/strncasecmp_l.h"

#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_strcmp.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, strncasecmp_l,
(const char *left, const char *right, size_t n, locale_t)) {
auto case_cmp = [](char a, char b) {
return LIBC_NAMESPACE::internal::tolower(a) -
LIBC_NAMESPACE::internal::tolower(b);
};
return inline_strncmp(left, right, n, case_cmp);
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/strings/strncasecmp_l.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation header for strcasecmp_l ------------------*- 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_STRINGS_STRNCASECMP_L_H
#define LLVM_LIBC_SRC_STRINGS_STRNCASECMP_L_H

#include "hdr/types/locale_t.h"
#include "src/__support/macros/config.h"
#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {

int strncasecmp_l(const char *left, const char *right, size_t n,
locale_t locale);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRINGS_STRNCASECMP_L_H
26 changes: 26 additions & 0 deletions libc/test/src/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ add_libc_test(
libc.src.strings.strcasecmp
)

add_libc_test(
strcasecmp_l_test
SUITE
libc-strings-tests
SRCS
strcasecmp_l_test.cpp
DEPENDS
libc.hdr.locale_macros
libc.src.locale.freelocale
libc.src.locale.newlocale
libc.src.strings.strcasecmp_l
)

add_libc_test(
strncasecmp_test
SUITE
Expand All @@ -84,5 +97,18 @@ add_libc_test(
libc.src.strings.strncasecmp
)

add_libc_test(
strncasecmp_l_test
SUITE
libc-strings-tests
SRCS
strncasecmp_l_test.cpp
DEPENDS
libc.hdr.locale_macros
libc.src.locale.freelocale
libc.src.locale.newlocale
libc.src.strings.strncasecmp_l
)

add_libc_multi_impl_test(bcmp libc-strings-tests SRCS bcmp_test.cpp)
add_libc_multi_impl_test(bzero libc-strings-tests SRCS bzero_test.cpp)
21 changes: 21 additions & 0 deletions libc/test/src/strings/strcasecmp_l_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for strcasecmp_l ----------------------------------------===//
//
// 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 "hdr/locale_macros.h"
#include "src/locale/freelocale.h"
#include "src/locale/newlocale.h"
#include "src/strings/strcasecmp_l.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStrCaseCmpLTest, Case) {
locale_t locale = LIBC_NAMESPACE::newlocale(LC_ALL, "C", nullptr);
ASSERT_EQ(LIBC_NAMESPACE::strcasecmp_l("hello", "HELLO", locale), 0);
ASSERT_LT(LIBC_NAMESPACE::strcasecmp_l("hello1", "hello2", locale), 0);
ASSERT_GT(LIBC_NAMESPACE::strcasecmp_l("hello2", "hello1", locale), 0);
LIBC_NAMESPACE::freelocale(locale);
}
22 changes: 22 additions & 0 deletions libc/test/src/strings/strncasecmp_l_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Unittests for strncasecmp_l ---------------------------------------===//
//
// 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 "hdr/locale_macros.h"
#include "src/locale/freelocale.h"
#include "src/locale/newlocale.h"
#include "src/strings/strncasecmp_l.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStrNCaseCmpLTest, Case) {
locale_t locale = LIBC_NAMESPACE::newlocale(LC_ALL, "C", nullptr);
ASSERT_EQ(LIBC_NAMESPACE::strncasecmp_l("hello", "HELLO", 3, locale), 0);
ASSERT_EQ(LIBC_NAMESPACE::strncasecmp_l("abcXX", "ABCYY", 3, locale), 0);
ASSERT_LT(LIBC_NAMESPACE::strncasecmp_l("hello1", "hello2", 6, locale), 0);
ASSERT_GT(LIBC_NAMESPACE::strncasecmp_l("hello2", "hello1", 6, locale), 0);
LIBC_NAMESPACE::freelocale(locale);
}
Loading