utf8: modernize utf16 inline calls a bit
authorLennart Poettering <lennart@poettering.net>
Wed, 18 Jul 2018 10:23:31 +0000 (12:23 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 25 Sep 2018 13:57:47 +0000 (15:57 +0200)
Let's fix an indentation issue.

Let's avoid yoda comparisons.

Let's drop unnecessary ().

Let's make sure we convert 16bit values to 32bit before shifting them by
10bit to the left, to avoid overflows.

Let's avoid comparisons between signed literals and unsigned variables,
in particular if the literals are outside of the minimum range C
requires for "int".

src/basic/utf8.h

index f5e9f8c..63991bf 100644 (file)
@@ -31,15 +31,15 @@ int utf8_encoded_valid_unichar(const char *str);
 int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar);
 
 static inline bool utf16_is_surrogate(char16_t c) {
-        return (0xd800 <= c && c <= 0xdfff);
+        return c >= 0xd800U && c <= 0xdfffU;
 }
 
 static inline bool utf16_is_trailing_surrogate(char16_t c) {
-        return (0xdc00 <= c && c <= 0xdfff);
+        return c >= 0xdc00U && c <= 0xdfffU;
 }
 
 static inline char32_t utf16_surrogate_pair_to_unichar(char16_t lead, char16_t trail) {
-                return ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000;
+        return ((((char32_t) lead - 0xd800U) << 10) + ((char32_t) trail - 0xdc00U) + 0x10000U);
 }
 
 size_t utf8_n_codepoints(const char *str);