Skip to content

Commit a3f0834

Browse files
committed
util: move utf8 utils to a separate header
This moves the declaration of the utf8 utils defined in lib/utils/utf8.c in their own header. Main reason to do this is that the current setup requried adding an include for sys/types.h in util.h, which can result in a build falure due to a circular header depdenecy when using: CONFIG_POSIX_API=y CONFIG_NEWLIB_LIBC=y _GNU_SOURCE the loop and error are: - include/sys/types.h:50: <- this is a NEWLIB one - include/zephyr/posix/sys/select.h:9: - include/zephyr/posix/posix_types.h:30: - include/zephyr/kernel.h:17: - include/zephyr/kernel_includes.h:25: - include/zephyr/sys/atomic.h:18: include/zephyr/sys/util.h:705:1: error: unknown type name 'ssize_t' Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
1 parent 7837f5e commit a3f0834

File tree

12 files changed

+110
-60
lines changed

12 files changed

+110
-60
lines changed

doc/releases/migration-guide-4.3.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Build System
2626
Kernel
2727
******
2828

29+
Base Libraries
30+
**************
31+
32+
* UTF-8 utils declarations (:c:func:`utf8_trunc`, :c:func:`utf8_lcpy`) have
33+
been moved from ``util.h`` to a separate
34+
:zephyr_file:`include/zephyr/sys/util_utf8.h` file.
35+
2936
Boards
3037
******
3138

include/zephyr/sys/util.h

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#include <stddef.h>
3030
#include <stdint.h>
3131
#include <string.h>
32-
#include <sys/types.h>
33-
3432

3533
/** @brief Number of bits that make up a type */
3634
#define NUM_BITS(t) (sizeof(t) * BITS_PER_BYTE)
@@ -647,63 +645,6 @@ static inline int64_t sign_extend_64(uint64_t value, uint8_t index)
647645
return (int64_t)(value << shift) >> shift;
648646
}
649647

650-
/**
651-
* @brief Properly truncate a NULL-terminated UTF-8 string
652-
*
653-
* Take a NULL-terminated UTF-8 string and ensure that if the string has been
654-
* truncated (by setting the NULL terminator) earlier by other means, that
655-
* the string ends with a properly formatted UTF-8 character (1-4 bytes).
656-
*
657-
* Example:
658-
*
659-
* @code{.c}
660-
* char test_str[] = "€€€";
661-
* char trunc_utf8[8];
662-
*
663-
* printf("Original : %s\n", test_str); // €€€
664-
* strncpy(trunc_utf8, test_str, sizeof(trunc_utf8));
665-
* trunc_utf8[sizeof(trunc_utf8) - 1] = '\0';
666-
* printf("Bad : %s\n", trunc_utf8); // €€�
667-
* utf8_trunc(trunc_utf8);
668-
* printf("Truncated: %s\n", trunc_utf8); // €€
669-
* @endcode
670-
*
671-
* @param utf8_str NULL-terminated string
672-
*
673-
* @return Pointer to the @p utf8_str
674-
*/
675-
char *utf8_trunc(char *utf8_str);
676-
677-
/**
678-
* @brief Copies a UTF-8 encoded string from @p src to @p dst
679-
*
680-
* The resulting @p dst will always be NULL terminated if @p n is larger than 0,
681-
* and the @p dst string will always be properly UTF-8 truncated.
682-
*
683-
* @param dst The destination of the UTF-8 string.
684-
* @param src The source string
685-
* @param n The size of the @p dst buffer. Maximum number of characters copied
686-
* is @p n - 1. If 0 nothing will be done, and the @p dst will not be
687-
* NULL terminated.
688-
*
689-
* @return Pointer to the @p dst
690-
*/
691-
char *utf8_lcpy(char *dst, const char *src, size_t n);
692-
693-
/**
694-
* @brief Counts the characters in a UTF-8 encoded string @p s
695-
*
696-
* Counts the number of UTF-8 characters (code points) in a null-terminated string.
697-
* This function steps through each UTF-8 sequence by checking leading byte patterns.
698-
* It does not fully validate UTF-8 correctness, only counts characters.
699-
*
700-
* @param s The input string
701-
*
702-
* @return Number of UTF-8 characters in @p s on success or (negative) error code
703-
* otherwise.
704-
*/
705-
ssize_t utf8_count_chars(const char *s);
706-
707648
#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
708649
#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
709650
#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))

include/zephyr/sys/util_utf8.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief UTF-8 utilities
10+
*
11+
* Misc UTF-8 utilities.
12+
*/
13+
14+
#ifndef ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_
15+
#define ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_
16+
17+
#include <stddef.h>
18+
#include <sys/types.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/**
25+
* @addtogroup sys-util
26+
* @{
27+
*/
28+
29+
/**
30+
* @brief Properly truncate a NULL-terminated UTF-8 string
31+
*
32+
* Take a NULL-terminated UTF-8 string and ensure that if the string has been
33+
* truncated (by setting the NULL terminator) earlier by other means, that
34+
* the string ends with a properly formatted UTF-8 character (1-4 bytes).
35+
*
36+
* Example:
37+
*
38+
* @code{.c}
39+
* char test_str[] = "€€€";
40+
* char trunc_utf8[8];
41+
*
42+
* printf("Original : %s\n", test_str); // €€€
43+
* strncpy(trunc_utf8, test_str, sizeof(trunc_utf8));
44+
* trunc_utf8[sizeof(trunc_utf8) - 1] = '\0';
45+
* printf("Bad : %s\n", trunc_utf8); // €€�
46+
* utf8_trunc(trunc_utf8);
47+
* printf("Truncated: %s\n", trunc_utf8); // €€
48+
* @endcode
49+
*
50+
* @param utf8_str NULL-terminated string
51+
*
52+
* @return Pointer to the @p utf8_str
53+
*/
54+
char *utf8_trunc(char *utf8_str);
55+
56+
/**
57+
* @brief Copies a UTF-8 encoded string from @p src to @p dst
58+
*
59+
* The resulting @p dst will always be NULL terminated if @p n is larger than 0,
60+
* and the @p dst string will always be properly UTF-8 truncated.
61+
*
62+
* @param dst The destination of the UTF-8 string.
63+
* @param src The source string
64+
* @param n The size of the @p dst buffer. Maximum number of characters copied
65+
* is @p n - 1. If 0 nothing will be done, and the @p dst will not be
66+
* NULL terminated.
67+
*
68+
* @return Pointer to the @p dst
69+
*/
70+
char *utf8_lcpy(char *dst, const char *src, size_t n);
71+
72+
/**
73+
* @brief Counts the characters in a UTF-8 encoded string @p s
74+
*
75+
* Counts the number of UTF-8 characters (code points) in a null-terminated string.
76+
* This function steps through each UTF-8 sequence by checking leading byte patterns.
77+
* It does not fully validate UTF-8 correctness, only counts characters.
78+
*
79+
* @param s The input string
80+
*
81+
* @return Number of UTF-8 characters in @p s on success or (negative) error code
82+
* otherwise.
83+
*/
84+
ssize_t utf8_count_chars(const char *s);
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
/**
91+
* @}
92+
*/
93+
94+
#endif /* ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_ */

lib/utils/utf8.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <zephyr/sys/__assert.h>
1010
#include <errno.h>
1111
#include <sys/types.h>
12+
#include <zephyr/sys/util_utf8.h>
1213

1314
#define ASCII_CHAR 0x7F
1415
#define SEQUENCE_FIRST_MASK 0xC0

subsys/bluetooth/audio/aics.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <zephyr/sys/check.h>
2828
#include <zephyr/sys/util.h>
2929
#include <zephyr/sys/util_macro.h>
30+
#include <zephyr/sys/util_utf8.h>
3031
#include <zephyr/sys_clock.h>
3132

3233
#include "aics_internal.h"

subsys/bluetooth/audio/has.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <zephyr/sys/slist.h>
3232
#include <zephyr/sys/util.h>
3333
#include <zephyr/sys/util_macro.h>
34+
#include <zephyr/sys/util_utf8.h>
3435
#include <zephyr/sys_clock.h>
3536
#include <zephyr/toolchain.h>
3637

subsys/bluetooth/audio/has_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <zephyr/sys/check.h>
2424
#include <zephyr/sys/util.h>
2525
#include <zephyr/sys/util_macro.h>
26+
#include <zephyr/sys/util_utf8.h>
2627

2728
#include "has_internal.h"
2829

subsys/bluetooth/audio/shell/bap_broadcast_assistant.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <zephyr/sys/byteorder.h>
3131
#include <zephyr/sys/util.h>
3232
#include <zephyr/sys/util_macro.h>
33+
#include <zephyr/sys/util_utf8.h>
3334
#include <zephyr/types.h>
3435

3536
#include "common/bt_shell_private.h"

subsys/bluetooth/audio/tbs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <zephyr/sys/check.h>
3434
#include <zephyr/sys/util.h>
3535
#include <zephyr/sys/util_macro.h>
36+
#include <zephyr/sys/util_utf8.h>
3637
#include <zephyr/types.h>
3738

3839
#include "audio_internal.h"

subsys/bluetooth/audio/tbs_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <zephyr/sys/slist.h>
2929
#include <zephyr/sys/util.h>
3030
#include <zephyr/sys/util_macro.h>
31+
#include <zephyr/sys/util_utf8.h>
3132
#include <zephyr/toolchain.h>
3233
#include <zephyr/types.h>
3334
#include <zephyr/sys/check.h>

0 commit comments

Comments
 (0)