From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] lib: wchar: reimplement in terms of Linux UCS2 helpers
Date: Thu, 11 Dec 2025 21:45:52 +0100 [thread overview]
Message-ID: <20251211204555.2694174-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251211204555.2694174-1-a.fatoum@pengutronix.de>
Let's make use of the Linux functions for those that have direct
counterparts.
Signed-off-by: Ahmad Fatoum <a.fatoum@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@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@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
next prev parent reply other threads:[~2025-12-11 20:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 20:45 [PATCH 1/2] lib: import Linux UCS2 library functions Ahmad Fatoum
2025-12-11 20:45 ` Ahmad Fatoum [this message]
2025-12-15 7:10 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251211204555.2694174-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox