Skip to content

[libc] Add support for 'string.h' locale variants #105719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strchrnul
libc.src.string.strcmp
libc.src.string.strcoll
libc.src.string.strcoll_l
libc.src.string.strcpy
libc.src.string.strcspn
libc.src.string.strdup
Expand All @@ -79,6 +80,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.string.strtok
libc.src.string.strtok_r
libc.src.string.strxfrm
libc.src.string.strxfrm_l

# stdbit.h entrypoints
libc.src.stdbit.stdc_bit_ceil_uc
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 @@ -809,6 +809,10 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdlib.strtoul_l
libc.src.stdlib.strtoull_l

# string.h entrypoints
libc.src.string.strcoll_l
libc.src.string.strxfrm_l

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

Expand Down
1 change: 1 addition & 0 deletions libc/include/string.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "__llvm-libc-common.h"

#include "llvm-libc-types/locale_t.h"
#include "llvm-libc-macros/null-macro.h"

%%public_api()
Expand Down
17 changes: 17 additions & 0 deletions libc/newhdrgen/yaml/string.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ functions:
arguments:
- type: const char *
- type: const char *
- name: strcoll_l
standards:
- stdc
Copy link
Contributor

@s-barannikov s-barannikov May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions are POSIX (2008), not stdc.

return_type: int
arguments:
- type: const char *
- type: const char *
- type: locale_t
- name: strcpy
standards:
- stdc
Expand Down Expand Up @@ -300,3 +308,12 @@ functions:
- type: char *__restrict
- type: const char *__restrict
- type: size_t
- name: strxfrm_l
standards:
- stdc
return_type: size_t
arguments:
- type: char *__restrict
- type: const char *__restrict
- type: size_t
- type: locale_t
13 changes: 13 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>]
>,
FunctionSpec<
"strcoll_l",
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<LocaleT>]
>,
FunctionSpec<
"strncmp",
RetValSpec<IntType>,
Expand All @@ -366,6 +371,14 @@ def StdC : StandardSpec<"stdc"> {
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<SizeTType>]
>,
FunctionSpec<
"strxfrm_l",
RetValSpec<SizeTType>,
[ArgSpec<CharRestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<SizeTType>,
ArgSpec<LocaleT>]
>,
FunctionSpec<
"strchr",
RetValSpec<CharPtr>,
Expand Down
19 changes: 19 additions & 0 deletions libc/src/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ add_entrypoint_object(
strcoll.h
)

add_entrypoint_object(
strcoll_l
SRCS
strcoll_l.cpp
HDRS
strcoll_l.h
)

add_entrypoint_object(
strcpy
SRCS
Expand Down Expand Up @@ -441,6 +449,17 @@ add_entrypoint_object(
.memory_utils.inline_memcpy
)

add_entrypoint_object(
strxfrm_l
SRCS
strxfrm_l.cpp
HDRS
strxfrm_l.h
DEPENDS
.string_utils
.memory_utils.inline_memcpy
)

add_entrypoint_object(
memset_explicit
SRCS
Expand Down
24 changes: 24 additions & 0 deletions libc/src/string/strcoll_l.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation of strcoll_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/string/strcoll_l.h"

#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

// TODO: Add support for locales.
LLVM_LIBC_FUNCTION(int, strcoll_l,
(const char *left, const char *right, locale_t)) {
for (; *left && *left == *right; ++left, ++right)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add an assert to check that the locale passed is the default C locale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expose asserts in entrypoints? I'm unsure if people would prefer it be ignored or terminate their program.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, this is fine for now then. We should add something to the docs explaining that this is the behavior though.

;
return static_cast<int>(*left) - static_cast<int>(*right);
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/string/strcoll_l.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for strcoll_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_STRING_STRCOLL_L_H
#define LLVM_LIBC_SRC_STRING_STRCOLL_L_H

#include "include/llvm-libc-types/locale_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

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

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRING_STRCOLL_L_H
28 changes: 28 additions & 0 deletions libc/src/string/strxfrm_l.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Implementation of strxfrm_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/string/strxfrm_l.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/string_utils.h"

#include "src/__support/common.h"

namespace LIBC_NAMESPACE_DECL {

// TODO: Add support for locales.
LLVM_LIBC_FUNCTION(size_t, strxfrm_l,
(char *__restrict dest, const char *__restrict src, size_t n,
locale_t)) {
size_t len = internal::string_length(src);
if (n > len)
inline_memcpy(dest, src, len + 1);
return len;
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/string/strxfrm_l.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation header for strxfrm_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_STRING_STRXFRM_L_H
#define LLVM_LIBC_SRC_STRING_STRXFRM_L_H

#include "include/llvm-libc-types/locale_t.h"
#include "src/__support/macros/config.h"
#include <stddef.h> // For size_t

namespace LIBC_NAMESPACE_DECL {

size_t strxfrm_l(char *__restrict dest, const char *__restrict src, size_t n,
locale_t locale);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STRING_STRXFRM_L_H
Loading