grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v5 07/16] include/grub/charset.h: Add grub_utf16_{strlen, strnlen


From: Vitaly Kuzmichev
Subject: [PATCH v5 07/16] include/grub/charset.h: Add grub_utf16_{strlen, strnlen}()
Date: Tue, 22 Aug 2023 23:39:15 +0200

Add two functions to calculate UTF-16 string length. The address to
UTF-16 string could be unaligned, so use grub_get_unaligned16() to
iterate over characters.

Signed-off-by: Vitaly Kuzmichev <vitaly.kuzmichev@rtsoft.de>
---
 include/grub/charset.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/grub/charset.h b/include/grub/charset.h
index 635d6df9e..099f23fbe 100644
--- a/include/grub/charset.h
+++ b/include/grub/charset.h
@@ -278,6 +278,32 @@ grub_utf16_to_utf8 (grub_uint8_t *dest, const 
grub_uint16_t *src,
   return dest;
 }
 
+/* Returns the length of the UTF16 string, excluding \0.  */
+static inline grub_size_t
+grub_utf16_strlen (const void *s)
+{
+  const grub_uint8_t *p = s;
+  grub_size_t len = 0;
+
+  while (grub_get_unaligned16 (p + 2 * len))
+    len++;
+
+  return len;
+}
+
+/* Returns the length of the UTF16 string of the fixed size, excluding \0.  */
+static inline grub_size_t
+grub_utf16_strnlen (const void *s, grub_size_t maxlen)
+{
+  const grub_uint8_t *p = s;
+  grub_size_t len = 0;
+
+  while (grub_get_unaligned16 (p + 2 * len) && len < maxlen)
+    len++;
+
+  return len;
+}
+
 #define GRUB_MAX_UTF8_PER_LATIN1 2
 
 /* Convert Latin1 to UTF-8.  */
-- 
2.34.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]