chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / basic / utf8.c
index 124effd6dfcd6694c055c45e66cce9162eca806a..d18dbc35280c827d5cc8e9ca24a9de34db3003e3 100644 (file)
@@ -1,23 +1,6 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
   Copyright 2008-2011 Kay Sievers
-  Copyright 2012 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd 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
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 /* Parts of this file are based on the GLIB utf8 validation functions. The
 #include <string.h>
 
 #include "alloc-util.h"
+//#include "gunicode.h"
 #include "hexdecoct.h"
 #include "macro.h"
 #include "utf8.h"
 
-bool unichar_is_valid(uint32_t ch) {
+bool unichar_is_valid(char32_t ch) {
 
         if (ch >= 0x110000) /* End of unicode space */
                 return false;
@@ -67,7 +51,7 @@ bool unichar_is_valid(uint32_t ch) {
         return true;
 }
 
-static bool unichar_is_control(uint32_t ch) {
+static bool unichar_is_control(char32_t ch) {
 
         /*
           0 to ' '-1 is the C0 range.
@@ -75,7 +59,7 @@ static bool unichar_is_control(uint32_t ch) {
           '\t' is in C0 range, but more or less harmless and commonly used.
         */
 
-        return (ch < ' ' && ch != '\t' && ch != '\n') ||
+        return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
                 (0x7F <= ch && ch <= 0x9F);
 }
 
@@ -103,8 +87,9 @@ static int utf8_encoded_expected_len(const char *str) {
 }
 
 /* decode one unicode char */
-int utf8_encoded_to_unichar(const char *str) {
-        int unichar, len, i;
+int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
+        char32_t unichar;
+        int len, i;
 
         assert(str);
 
@@ -112,34 +97,37 @@ int utf8_encoded_to_unichar(const char *str) {
 
         switch (len) {
         case 1:
-                return (int)str[0];
+                *ret_unichar = (char32_t)str[0];
+                return 0;
         case 2:
                 unichar = str[0] & 0x1f;
                 break;
         case 3:
-                unichar = (int)str[0] & 0x0f;
+                unichar = (char32_t)str[0] & 0x0f;
                 break;
         case 4:
-                unichar = (int)str[0] & 0x07;
+                unichar = (char32_t)str[0] & 0x07;
                 break;
         case 5:
-                unichar = (int)str[0] & 0x03;
+                unichar = (char32_t)str[0] & 0x03;
                 break;
         case 6:
-                unichar = (int)str[0] & 0x01;
+                unichar = (char32_t)str[0] & 0x01;
                 break;
         default:
                 return -EINVAL;
         }
 
         for (i = 1; i < len; i++) {
-                if (((int)str[i] & 0xc0) != 0x80)
+                if (((char32_t)str[i] & 0xc0) != 0x80)
                         return -EINVAL;
                 unichar <<= 6;
-                unichar |= (int)str[i] & 0x3f;
+                unichar |= (char32_t)str[i] & 0x3f;
         }
 
-        return unichar;
+        *ret_unichar = unichar;
+
+        return 0;
 }
 
 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
@@ -148,15 +136,16 @@ bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
         assert(str);
 
         for (p = str; length;) {
-                int encoded_len, val;
+                int encoded_len, r;
+                char32_t val;
 
                 encoded_len = utf8_encoded_valid_unichar(p);
                 if (encoded_len < 0 ||
                     (size_t) encoded_len > length)
                         return false;
 
-                val = utf8_encoded_to_unichar(p);
-                if (val < 0 ||
+                r = utf8_encoded_to_unichar(p, &val);
+                if (r < 0 ||
                     unichar_is_control(val) ||
                     (!newline && val == '\n'))
                         return false;
@@ -238,7 +227,7 @@ char *utf8_escape_non_printable(const char *str) {
                                         *(s++) = hexchar((int) *str);
 
                                         str += 1;
-                                        len --;
+                                        len--;
                                 }
                         }
                 } else {
@@ -255,6 +244,9 @@ char *utf8_escape_non_printable(const char *str) {
 char *ascii_is_valid(const char *str) {
         const char *p;
 
+        /* Check whether the string consists of valid ASCII bytes,
+         * i.e values between 0 and 127, inclusive. */
+
         assert(str);
 
         for (p = str; *p; p++)
@@ -264,6 +256,21 @@ char *ascii_is_valid(const char *str) {
         return (char*) str;
 }
 
+char *ascii_is_valid_n(const char *str, size_t len) {
+        size_t i;
+
+        /* Very similar to ascii_is_valid(), but checks exactly len
+         * bytes and rejects any NULs in that range. */
+
+        assert(str);
+
+        for (i = 0; i < len; i++)
+                if ((unsigned char) str[i] >= 128 || str[i] == 0)
+                        return NULL;
+
+        return (char*) str;
+}
+
 /**
  * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
  * @out_utf8: output buffer of at least 4 bytes or NULL
@@ -276,7 +283,7 @@ char *ascii_is_valid(const char *str) {
  * Returns: The length in bytes that the UTF-8 representation does or would
  *          occupy.
  */
-size_t utf8_encode_unichar(char *out_utf8, uint32_t g) {
+size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
 
         if (g < (1 << 7)) {
                 if (out_utf8)
@@ -320,7 +327,7 @@ char *utf16_to_utf8(const void *s, size_t length) {
         t = r;
 
         while (f < (const uint8_t*) s + length) {
-                uint16_t w1, w2;
+                char16_t w1, w2;
 
                 /* see RFC 2781 section 2.2 */
 
@@ -354,7 +361,7 @@ char *utf16_to_utf8(const void *s, size_t length) {
 }
 
 /* expected size used to encode one unicode char */
-static int utf8_unichar_to_encoded_len(int unichar) {
+static int utf8_unichar_to_encoded_len(char32_t unichar) {
 
         if (unichar < 0x80)
                 return 1;
@@ -372,7 +379,8 @@ static int utf8_unichar_to_encoded_len(int unichar) {
 
 /* validate one encoded unicode char and return its length */
 int utf8_encoded_valid_unichar(const char *str) {
-        int len, unichar, i;
+        int len, i, r;
+        char32_t unichar;
 
         assert(str);
 
@@ -389,7 +397,9 @@ int utf8_encoded_valid_unichar(const char *str) {
                 if ((str[i] & 0x80) != 0x80)
                         return -EINVAL;
 
-        unichar = utf8_encoded_to_unichar(str);
+        r = utf8_encoded_to_unichar(str, &unichar);
+        if (r < 0)
+                return r;
 
         /* check if encoded length matches encoded value */
         if (utf8_unichar_to_encoded_len(unichar) != len)
@@ -401,3 +411,42 @@ int utf8_encoded_valid_unichar(const char *str) {
 
         return len;
 }
+
+size_t utf8_n_codepoints(const char *str) {
+        size_t n = 0;
+
+        /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
+
+        while (*str != 0) {
+                int k;
+
+                k = utf8_encoded_valid_unichar(str);
+                if (k < 0)
+                        return (size_t) -1;
+
+                str += k;
+                n++;
+        }
+
+        return n;
+}
+
+size_t utf8_console_width(const char *str) {
+        size_t n = 0;
+
+        /* Returns the approximate width a string will take on screen when printed on a character cell
+         * terminal/console. */
+
+        while (*str != 0) {
+                char32_t c;
+
+                if (utf8_encoded_to_unichar(str, &c) < 0)
+                        return (size_t) -1;
+
+                str = utf8_next_char(str);
+
+                n += unichar_iswide(c) ? 2 : 1;
+        }
+
+        return n;
+}