[PATCH 2/2] lib: wchar: reimplement in terms of Linux UCS2 helpers

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Dec 11 12:45:52 PST 2025


Let's make use of the Linux functions for those that have direct
counterparts.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/wchar.h | 11 ++++-----
 lib/wchar.c     | 62 ++-----------------------------------------------
 2 files changed, 7 insertions(+), 66 deletions(-)

diff --git a/include/wchar.h b/include/wchar.h
index 02818815e183..5377aa27c08a 100644
--- a/include/wchar.h
+++ b/include/wchar.h
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/stddef.h>
+#include <linux/ucs2_string.h>
 
 wchar_t *strdup_wchar(const wchar_t *src);
 
@@ -15,16 +16,14 @@ wchar_t *strdup_char_to_wchar(const char *src);
 
 char *strdup_wchar_to_char(const wchar_t *src);
 
-size_t wcslen(const wchar_t *s);
-
-size_t wcsnlen(const wchar_t *s, size_t maxlen);
+#define wcsnlen		ucs2_strnlen
+#define wcslen		ucs2_strlen
+#define wcsncmp		ucs2_strncmp
+#define wcscmp(s1, s2)	wcsncmp((s1), (s2), ~0UL)
 
 #define MB_CUR_MAX 4
 
 int mbtowc(wchar_t *pwc, const char *s, size_t n);
 int wctomb(char *s, wchar_t wc);
 
-int wcscmp (const wchar_t *s1, const wchar_t *s2);
-int wcsncmp (const wchar_t *s1, const wchar_t *s2, size_t n);
-
 #endif /* __WCHAR_H */
diff --git a/lib/wchar.c b/lib/wchar.c
index 96db8116286a..05961ce1bd52 100644
--- a/lib/wchar.c
+++ b/lib/wchar.c
@@ -1,42 +1,10 @@
-/*
- * wchar.c - wide character support
- *
- * Copyright (c) 2014 Sascha Hauer <s.hauer at pengutronix.de>, Pengutronix
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- */
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: 2014 Sascha Hauer <s.hauer at pengutronix.de>, Pengutronix
 
 #include <wchar.h>
 #include <malloc.h>
 #include <string.h>
 
-size_t wcslen(const wchar_t *s)
-{
-	size_t len = 0;
-
-	while (*s++)
-		len++;
-
-	return len;
-}
-
-size_t wcsnlen(const wchar_t * s, size_t count)
-{
-	const wchar_t *sc;
-
-	for (sc = s; count-- && *sc != L'\0'; ++sc)
-		/* nothing */;
-	return sc - s;
-}
-
 wchar_t *strdup_wchar(const wchar_t *src)
 {
 	int len;
@@ -124,29 +92,3 @@ char *strdup_wchar_to_char(const wchar_t *src)
 
 	return dst;
 }
-
-int wcscmp(const wchar_t *s1, const wchar_t *s2)
-{
-	while (*s1 == *s2++) {
-		if (*s1++ == 0)
-			return 0;
-	}
-
-	return *s1 - *--s2;
-}
-
-int wcsncmp (const wchar_t *s1, const wchar_t *s2, size_t n)
-{
-	if (n == 0)
-		return 0;
-
-	do {
-		if (*s1 != *s2++)
-			return *s1 - *--s2;
-
-		if (*s1++ == 0)
-			break;
-	} while (--n != 0);
-
-	return 0;
-}
-- 
2.47.3




More information about the barebox mailing list