]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: unichar - Add uni_ucs4_to_utf8_len()
authorStephan Bosch <stephan.bosch@open-xchange.com>
Fri, 16 Oct 2020 19:52:02 +0000 (21:52 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Sat, 18 Nov 2023 18:58:04 +0000 (18:58 +0000)
Returns the number of octets needed to encode the provided codepoint in UTF-8.

src/lib/unichar.h

index 88defcd43f5e7688fafb2efd49b8fbf726ec8dfa..d75a6ba34c3c045b26871cfdec02080ad410fe52 100644 (file)
@@ -72,6 +72,19 @@ int uni_utf8_to_ucs4_n(const unsigned char *input, size_t size,
 void uni_ucs4_to_utf8(const unichar_t *input, size_t len, buffer_t *output);
 void uni_ucs4_to_utf8_c(unichar_t chr, buffer_t *output);
 
+/* Return number of octets needed to encode this codepoint in UTF-8. */
+static inline unsigned int uni_ucs4_to_utf8_len(unichar_t chr)
+{
+       i_assert(uni_is_valid_ucs4(chr));
+       if (chr > 0xFFFF)
+               return 4;
+       if (chr > 0x07FF)
+               return 3;
+       if (chr > 0x007f)
+               return 2;
+       return 1;
+}
+
 /* Returns char_bytes (>0) if *chr_r is set, 0 for incomplete trailing character,
    -1 for invalid input. */
 int uni_utf8_get_char(const char *input, unichar_t *chr_r);