Skip to content

Commit 2da2d14

Browse files
committed
[libc] Add support for 'string.h' locale variants
Summary: This adds the locale variants of the string functions. As previously, these do not use the locale information at all and simply copy the non-locale version which expects the "C" locale.
1 parent a871051 commit 2da2d14

File tree

10 files changed

+152
-0
lines changed

10 files changed

+152
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ set(TARGET_LIBC_ENTRYPOINTS
5858
libc.src.string.strchrnul
5959
libc.src.string.strcmp
6060
libc.src.string.strcoll
61+
libc.src.string.strcoll_l
6162
libc.src.string.strcpy
6263
libc.src.string.strcspn
6364
libc.src.string.strdup
@@ -79,6 +80,7 @@ set(TARGET_LIBC_ENTRYPOINTS
7980
libc.src.string.strtok
8081
libc.src.string.strtok_r
8182
libc.src.string.strxfrm
83+
libc.src.string.strxfrm_l
8284

8385
# stdbit.h entrypoints
8486
libc.src.stdbit.stdc_bit_ceil_uc

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,10 @@ if(LLVM_LIBC_FULL_BUILD)
809809
libc.src.stdlib.strtoul_l
810810
libc.src.stdlib.strtoull_l
811811

812+
# string.h entrypoints
813+
libc.src.string.strcoll_l
814+
libc.src.string.strxfrm_l
815+
812816
# assert.h entrypoints
813817
libc.src.assert.__assert_fail
814818

libc/include/string.h.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "__llvm-libc-common.h"
1313

14+
#include "llvm-libc-types/locale_t.h"
1415
#include "llvm-libc-macros/null-macro.h"
1516

1617
%%public_api()

libc/newhdrgen/yaml/string.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ functions:
144144
arguments:
145145
- type: const char *
146146
- type: const char *
147+
- name: strcoll_l
148+
standards:
149+
- stdc
150+
return_type: int
151+
arguments:
152+
- type: const char *
153+
- type: const char *
154+
- type: locale_t
147155
- name: strcpy
148156
standards:
149157
- stdc
@@ -300,3 +308,12 @@ functions:
300308
- type: char *__restrict
301309
- type: const char *__restrict
302310
- type: size_t
311+
- name: strxfrm_l
312+
standards:
313+
- stdc
314+
return_type: size_t
315+
arguments:
316+
- type: char *__restrict
317+
- type: const char *__restrict
318+
- type: size_t
319+
- type: locale_t

libc/spec/stdc.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ def StdC : StandardSpec<"stdc"> {
354354
RetValSpec<IntType>,
355355
[ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>]
356356
>,
357+
FunctionSpec<
358+
"strcoll_l",
359+
RetValSpec<IntType>,
360+
[ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<LocaleT>]
361+
>,
357362
FunctionSpec<
358363
"strncmp",
359364
RetValSpec<IntType>,
@@ -366,6 +371,14 @@ def StdC : StandardSpec<"stdc"> {
366371
ArgSpec<ConstCharRestrictedPtr>,
367372
ArgSpec<SizeTType>]
368373
>,
374+
FunctionSpec<
375+
"strxfrm_l",
376+
RetValSpec<SizeTType>,
377+
[ArgSpec<CharRestrictedPtr>,
378+
ArgSpec<ConstCharRestrictedPtr>,
379+
ArgSpec<SizeTType>,
380+
ArgSpec<LocaleT>]
381+
>,
369382
FunctionSpec<
370383
"strchr",
371384
RetValSpec<CharPtr>,

libc/src/string/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ add_entrypoint_object(
200200
strcoll.h
201201
)
202202

203+
add_entrypoint_object(
204+
strcoll_l
205+
SRCS
206+
strcoll_l.cpp
207+
HDRS
208+
strcoll_l.h
209+
)
210+
203211
add_entrypoint_object(
204212
strcpy
205213
SRCS
@@ -441,6 +449,17 @@ add_entrypoint_object(
441449
.memory_utils.inline_memcpy
442450
)
443451

452+
add_entrypoint_object(
453+
strxfrm_l
454+
SRCS
455+
strxfrm_l.cpp
456+
HDRS
457+
strxfrm_l.h
458+
DEPENDS
459+
.string_utils
460+
.memory_utils.inline_memcpy
461+
)
462+
444463
add_entrypoint_object(
445464
memset_explicit
446465
SRCS

libc/src/string/strcoll_l.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of strcoll_l ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/string/strcoll_l.h"
10+
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
// TODO: Add support for locales.
17+
LLVM_LIBC_FUNCTION(int, strcoll_l,
18+
(const char *left, const char *right, locale_t)) {
19+
for (; *left && *left == *right; ++left, ++right)
20+
;
21+
return static_cast<int>(*left) - static_cast<int>(*right);
22+
}
23+
24+
} // namespace LIBC_NAMESPACE_DECL

libc/src/string/strcoll_l.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for strcoll_l ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRING_STRCOLL_L_H
10+
#define LLVM_LIBC_SRC_STRING_STRCOLL_L_H
11+
12+
#include "include/llvm-libc-types/locale_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
int strcoll_l(const char *left, const char *right, locale_t locale);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_STRING_STRCOLL_L_H

libc/src/string/strxfrm_l.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of strxfrm_l ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/string/strxfrm_l.h"
10+
#include "src/__support/macros/config.h"
11+
#include "src/string/memory_utils/inline_memcpy.h"
12+
#include "src/string/string_utils.h"
13+
14+
#include "src/__support/common.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
// TODO: Add support for locales.
19+
LLVM_LIBC_FUNCTION(size_t, strxfrm_l,
20+
(char *__restrict dest, const char *__restrict src, size_t n,
21+
locale_t)) {
22+
size_t len = internal::string_length(src);
23+
if (n > len)
24+
inline_memcpy(dest, src, len + 1);
25+
return len;
26+
}
27+
28+
} // namespace LIBC_NAMESPACE_DECL

libc/src/string/strxfrm_l.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for strxfrm_l ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRING_STRXFRM_L_H
10+
#define LLVM_LIBC_SRC_STRING_STRXFRM_L_H
11+
12+
#include "include/llvm-libc-types/locale_t.h"
13+
#include "src/__support/macros/config.h"
14+
#include <stddef.h> // For size_t
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
size_t strxfrm_l(char *__restrict dest, const char *__restrict src, size_t n,
19+
locale_t locale);
20+
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC_STRING_STRXFRM_L_H

0 commit comments

Comments
 (0)