Skip to content

Commit d950df9

Browse files
committed
utils: utf8: change utf8_count_chars return to int
Change utf8_count_chars return type to int and drop thesys/types.h, this way the function does not depend on posix types. Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
1 parent 1a7e0b5 commit d950df9

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

include/zephyr/sys/util_utf8.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define ZEPHYR_INCLUDE_SYS_UTIL_UFT8_H_
1616

1717
#include <stddef.h>
18-
#include <sys/types.h>
1918

2019
#ifdef __cplusplus
2120
extern "C" {
@@ -81,7 +80,7 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
8180
* @return Number of UTF-8 characters in @p s on success or (negative) error code
8281
* otherwise.
8382
*/
84-
ssize_t utf8_count_chars(const char *s);
83+
int utf8_count_chars(const char *s);
8584

8685
#ifdef __cplusplus
8786
}

lib/utils/utf8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ char *utf8_lcpy(char *dst, const char *src, size_t n)
8484
return dst;
8585
}
8686

87-
ssize_t utf8_count_chars(const char *s)
87+
int utf8_count_chars(const char *s)
8888
{
8989
ssize_t count = 0;
9090
const char *p = s; /* getting a pointer to increment */

tests/unit/util/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,24 +1008,24 @@ ZTEST(util, test_utf8_lcpy_null_termination)
10081008
ZTEST(util, test_utf8_count_chars_ASCII)
10091009
{
10101010
const char *test_str = "I have 15 char.";
1011-
ssize_t count = utf8_count_chars(test_str);
1011+
int count = utf8_count_chars(test_str);
10121012

10131013
zassert_equal(count, 15, "Failed to count ASCII");
10141014
}
10151015

10161016
ZTEST(util, test_utf8_count_chars_non_ASCII)
10171017
{
10181018
const char *test_str = "Hello دنیا!🌍";
1019-
ssize_t count = utf8_count_chars(test_str);
1019+
int count = utf8_count_chars(test_str);
10201020

10211021
zassert_equal(count, 12, "Failed to count non-ASCII");
10221022
}
10231023

10241024
ZTEST(util, test_utf8_count_chars_invalid_utf)
10251025
{
10261026
const char test_str[] = { (char)0x80, 0x00 };
1027-
ssize_t count = utf8_count_chars(test_str);
1028-
ssize_t expected_result = -EINVAL;
1027+
int count = utf8_count_chars(test_str);
1028+
int expected_result = -EINVAL;
10291029

10301030
zassert_equal(count, expected_result, "Failed to detect invalid UTF");
10311031
}

0 commit comments

Comments
 (0)