[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
- Re: [PATCH v5 01/16] partmap/gpt: Add grub_gpt_partition_get_uuid(), (continued)
- [PATCH v5 03/16] commands/probe: Simplify 'probe --part-uuid' command, Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 11/16] commands/probe: Add command option to display partition PARTLABEL, Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 12/16] docs/grub.texi: Update documentation for 'probe' and 'search' commands, Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 04/16] include/grub/misc.h: Fix edge case in grub_uuidcasecmp(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 05/16] commands/search: Add support to search by partition PARTUUID, Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 10/16] commands/search: Add support to search by partition PARTLABEL, Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 13/16] fs/f2fs: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 07/16] include/grub/charset.h: Add grub_utf16_{strlen, strnlen}(),
Vitaly Kuzmichev <=
- [PATCH v5 06/16] include/grub/charset.h: Enhance grub_utf16_to_utf8(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 14/16] fs/fat: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 08/16] include/grub/charset.h: Add grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22
- [PATCH v5 15/16] fs/iso9660: Simplify to use grub_utf16_to_utf8_alloc(), Vitaly Kuzmichev, 2023/08/22