-
Notifications
You must be signed in to change notification settings - Fork 8.4k
util: move utf8 utils to a separate header #95355
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright The Zephyr Project Contributors | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @file | ||
| * @brief UTF-8 utilities | ||
| * | ||
| * Misc UTF-8 utilities. | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_ | ||
| #define ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_ | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * @addtogroup sys-util | ||
| * @{ | ||
| */ | ||
|
|
||
| /** | ||
| * @brief Properly truncate a NULL-terminated UTF-8 string | ||
| * | ||
| * Take a NULL-terminated UTF-8 string and ensure that if the string has been | ||
| * truncated (by setting the NULL terminator) earlier by other means, that | ||
| * the string ends with a properly formatted UTF-8 character (1-4 bytes). | ||
| * | ||
| * Example: | ||
| * | ||
| * @code{.c} | ||
| * char test_str[] = "€€€"; | ||
| * char trunc_utf8[8]; | ||
| * | ||
| * printf("Original : %s\n", test_str); // €€€ | ||
| * strncpy(trunc_utf8, test_str, sizeof(trunc_utf8)); | ||
| * trunc_utf8[sizeof(trunc_utf8) - 1] = '\0'; | ||
| * printf("Bad : %s\n", trunc_utf8); // €€� | ||
| * utf8_trunc(trunc_utf8); | ||
| * printf("Truncated: %s\n", trunc_utf8); // €€ | ||
| * @endcode | ||
| * | ||
| * @param utf8_str NULL-terminated string | ||
| * | ||
| * @return Pointer to the @p utf8_str | ||
| */ | ||
| char *utf8_trunc(char *utf8_str); | ||
|
|
||
| /** | ||
| * @brief Copies a UTF-8 encoded string from @p src to @p dst | ||
| * | ||
| * The resulting @p dst will always be NULL terminated if @p n is larger than 0, | ||
| * and the @p dst string will always be properly UTF-8 truncated. | ||
| * | ||
| * @param dst The destination of the UTF-8 string. | ||
| * @param src The source string | ||
| * @param n The size of the @p dst buffer. Maximum number of characters copied | ||
| * is @p n - 1. If 0 nothing will be done, and the @p dst will not be | ||
| * NULL terminated. | ||
| * | ||
| * @return Pointer to the @p dst | ||
| */ | ||
| char *utf8_lcpy(char *dst, const char *src, size_t n); | ||
|
|
||
| /** | ||
| * @brief Counts the characters in a UTF-8 encoded string @p s | ||
| * | ||
| * Counts the number of UTF-8 characters (code points) in a null-terminated string. | ||
| * This function steps through each UTF-8 sequence by checking leading byte patterns. | ||
| * It does not fully validate UTF-8 correctness, only counts characters. | ||
| * | ||
| * @param s The input string | ||
| * | ||
| * @return Number of UTF-8 characters in @p s on success or (negative) error code | ||
| * otherwise. | ||
| */ | ||
| int utf8_count_chars(const char *s); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* | ||
| * Copyright The Zephyr Project Contributors | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /* _GNU_SOURCE causes extra headers to be included and can cause dependency | ||
| * loops | ||
| */ | ||
| #define _GNU_SOURCE | ||
|
|
||
| #include <sys/types.h> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.