Revert "utf8.[ch]: use char32_t and char16_t instead of int, int32_t, int16_t"
This commit is contained in:
parent
6b61ae72fa
commit
dcd1262673
3
TODO
3
TODO
|
@ -120,6 +120,9 @@ Features:
|
||||||
|
|
||||||
* nspawn: as soon as networkd has a bus interface, hook up --network-interface=, --network-bridge= with networkd, to trigger netdev creation should an interface be missing
|
* nspawn: as soon as networkd has a bus interface, hook up --network-interface=, --network-bridge= with networkd, to trigger netdev creation should an interface be missing
|
||||||
|
|
||||||
|
* rework C11 utf8.[ch] to use char32_t instead of uint32_t when referring
|
||||||
|
to unicode chars, to make things more expressive.
|
||||||
|
|
||||||
* "machinectl migrate" or similar to copy a container from or to a
|
* "machinectl migrate" or similar to copy a container from or to a
|
||||||
difference host, via ssh
|
difference host, via ssh
|
||||||
|
|
||||||
|
|
|
@ -226,7 +226,7 @@ int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode
|
||||||
|
|
||||||
int a[8];
|
int a[8];
|
||||||
unsigned i;
|
unsigned i;
|
||||||
char32_t c;
|
uint32_t c;
|
||||||
|
|
||||||
if (length != (size_t) -1 && length < 9)
|
if (length != (size_t) -1 && length < 9)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -237,8 +237,8 @@ int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode
|
||||||
return a[i];
|
return a[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
c = (a[0] << 28U) | (a[1] << 24U) | (a[2] << 20U) | (a[3] << 16U) |
|
c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
|
||||||
(a[4] << 12U) | (a[5] << 8U) | (a[6] << 4U) | (a[7] << 0U);
|
((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
|
||||||
|
|
||||||
/* Don't allow 0 chars */
|
/* Don't allow 0 chars */
|
||||||
if (c == 0)
|
if (c == 0)
|
||||||
|
@ -272,7 +272,7 @@ int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode
|
||||||
case '7': {
|
case '7': {
|
||||||
/* octal encoding */
|
/* octal encoding */
|
||||||
int a, b, c;
|
int a, b, c;
|
||||||
char32_t m;
|
uint32_t m;
|
||||||
|
|
||||||
if (length != (size_t) -1 && length < 3)
|
if (length != (size_t) -1 && length < 3)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -294,7 +294,7 @@ int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
/* Don't allow bytes above 255 */
|
/* Don't allow bytes above 255 */
|
||||||
m = (a << 6U) | (b << 3U) | (char32_t) c;
|
m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
|
||||||
if (m > 255)
|
if (m > 255)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
|
@ -331,7 +331,7 @@ int cunescape_length_with_prefix(const char *s, size_t length, const char *prefi
|
||||||
|
|
||||||
for (f = s, t = r + pl; f < s + length; f++) {
|
for (f = s, t = r + pl; f < s + length; f++) {
|
||||||
size_t remaining;
|
size_t remaining;
|
||||||
char32_t u;
|
uint32_t u;
|
||||||
char c;
|
char c;
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
|
|
|
@ -99,7 +99,7 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & EXTRACT_CUNESCAPE) {
|
if (flags & EXTRACT_CUNESCAPE) {
|
||||||
char32_t u;
|
uint32_t u;
|
||||||
|
|
||||||
r = cunescape_one(*p, (size_t) -1, &c, &u);
|
r = cunescape_one(*p, (size_t) -1, &c, &u);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
|
|
|
@ -319,7 +319,7 @@ static int json_parse_string(const char **p, char **ret) {
|
||||||
else if (*c == 't')
|
else if (*c == 't')
|
||||||
ch = '\t';
|
ch = '\t';
|
||||||
else if (*c == 'u') {
|
else if (*c == 'u') {
|
||||||
char16_t x;
|
uint16_t x;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
r = unhex_ucs2(c + 1, &x);
|
r = unhex_ucs2(c + 1, &x);
|
||||||
|
@ -332,7 +332,7 @@ static int json_parse_string(const char **p, char **ret) {
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (!utf16_is_surrogate(x))
|
if (!utf16_is_surrogate(x))
|
||||||
n += utf8_encode_unichar(s + n, (char32_t) x);
|
n += utf8_encode_unichar(s + n, x);
|
||||||
else if (utf16_is_trailing_surrogate(x))
|
else if (utf16_is_trailing_surrogate(x))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -413,7 +413,7 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
|
||||||
|
|
||||||
k = 0;
|
k = 0;
|
||||||
for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
|
for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
|
||||||
char32_t c;
|
int c;
|
||||||
|
|
||||||
c = utf8_encoded_to_unichar(i);
|
c = utf8_encoded_to_unichar(i);
|
||||||
if (c < 0)
|
if (c < 0)
|
||||||
|
@ -425,7 +425,7 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
|
||||||
x ++;
|
x ++;
|
||||||
|
|
||||||
for (j = s + old_length; k < new_length && j > i; ) {
|
for (j = s + old_length; k < new_length && j > i; ) {
|
||||||
char32_t c;
|
int c;
|
||||||
|
|
||||||
j = utf8_prev_char(j);
|
j = utf8_prev_char(j);
|
||||||
c = utf8_encoded_to_unichar(j);
|
c = utf8_encoded_to_unichar(j);
|
||||||
|
|
|
@ -54,7 +54,7 @@
|
||||||
#include "utf8.h"
|
#include "utf8.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
bool unichar_is_valid(char32_t ch) {
|
bool unichar_is_valid(uint32_t ch) {
|
||||||
|
|
||||||
if (ch >= 0x110000) /* End of unicode space */
|
if (ch >= 0x110000) /* End of unicode space */
|
||||||
return false;
|
return false;
|
||||||
|
@ -68,7 +68,7 @@ bool unichar_is_valid(char32_t ch) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool unichar_is_control(char32_t ch) {
|
static bool unichar_is_control(uint32_t ch) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
0 to ' '-1 is the C0 range.
|
0 to ' '-1 is the C0 range.
|
||||||
|
@ -104,9 +104,8 @@ static int utf8_encoded_expected_len(const char *str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* decode one unicode char */
|
/* decode one unicode char */
|
||||||
char32_t utf8_encoded_to_unichar(const char *str) {
|
int utf8_encoded_to_unichar(const char *str) {
|
||||||
char32_t unichar;
|
int unichar, len, i;
|
||||||
int len, i;
|
|
||||||
|
|
||||||
assert(str);
|
assert(str);
|
||||||
|
|
||||||
|
@ -114,31 +113,31 @@ char32_t utf8_encoded_to_unichar(const char *str) {
|
||||||
|
|
||||||
switch (len) {
|
switch (len) {
|
||||||
case 1:
|
case 1:
|
||||||
return (char32_t)str[0];
|
return (int)str[0];
|
||||||
case 2:
|
case 2:
|
||||||
unichar = str[0] & 0x1f;
|
unichar = str[0] & 0x1f;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
unichar = (char32_t)str[0] & 0x0f;
|
unichar = (int)str[0] & 0x0f;
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
unichar = (char32_t)str[0] & 0x07;
|
unichar = (int)str[0] & 0x07;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
unichar = (char32_t)str[0] & 0x03;
|
unichar = (int)str[0] & 0x03;
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
unichar = (char32_t)str[0] & 0x01;
|
unichar = (int)str[0] & 0x01;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 1; i < len; i++) {
|
for (i = 1; i < len; i++) {
|
||||||
if (((char32_t)str[i] & 0xc0) != 0x80)
|
if (((int)str[i] & 0xc0) != 0x80)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
unichar <<= 6;
|
unichar <<= 6;
|
||||||
unichar |= (char32_t)str[i] & 0x3f;
|
unichar |= (int)str[i] & 0x3f;
|
||||||
}
|
}
|
||||||
|
|
||||||
return unichar;
|
return unichar;
|
||||||
|
@ -150,8 +149,7 @@ bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
|
||||||
assert(str);
|
assert(str);
|
||||||
|
|
||||||
for (p = str; length;) {
|
for (p = str; length;) {
|
||||||
int encoded_len;
|
int encoded_len, val;
|
||||||
char32_t val;
|
|
||||||
|
|
||||||
encoded_len = utf8_encoded_valid_unichar(p);
|
encoded_len = utf8_encoded_valid_unichar(p);
|
||||||
if (encoded_len < 0 ||
|
if (encoded_len < 0 ||
|
||||||
|
@ -279,7 +277,7 @@ char *ascii_is_valid(const char *str) {
|
||||||
* Returns: The length in bytes that the UTF-8 representation does or would
|
* Returns: The length in bytes that the UTF-8 representation does or would
|
||||||
* occupy.
|
* occupy.
|
||||||
*/
|
*/
|
||||||
size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
|
size_t utf8_encode_unichar(char *out_utf8, uint32_t g) {
|
||||||
|
|
||||||
if (g < (1 << 7)) {
|
if (g < (1 << 7)) {
|
||||||
if (out_utf8)
|
if (out_utf8)
|
||||||
|
@ -323,7 +321,7 @@ char *utf16_to_utf8(const void *s, size_t length) {
|
||||||
t = r;
|
t = r;
|
||||||
|
|
||||||
while (f < (const uint8_t*) s + length) {
|
while (f < (const uint8_t*) s + length) {
|
||||||
char16_t w1, w2;
|
uint16_t w1, w2;
|
||||||
|
|
||||||
/* see RFC 2781 section 2.2 */
|
/* see RFC 2781 section 2.2 */
|
||||||
|
|
||||||
|
@ -331,7 +329,7 @@ char *utf16_to_utf8(const void *s, size_t length) {
|
||||||
f += 2;
|
f += 2;
|
||||||
|
|
||||||
if (!utf16_is_surrogate(w1)) {
|
if (!utf16_is_surrogate(w1)) {
|
||||||
t += utf8_encode_unichar(t, (char32_t) w1);
|
t += utf8_encode_unichar(t, w1);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -375,8 +373,7 @@ static int utf8_unichar_to_encoded_len(int unichar) {
|
||||||
|
|
||||||
/* validate one encoded unicode char and return its length */
|
/* validate one encoded unicode char and return its length */
|
||||||
int utf8_encoded_valid_unichar(const char *str) {
|
int utf8_encoded_valid_unichar(const char *str) {
|
||||||
int len, i;
|
int len, unichar, i;
|
||||||
char32_t unichar;
|
|
||||||
|
|
||||||
assert(str);
|
assert(str);
|
||||||
|
|
||||||
|
|
|
@ -22,13 +22,12 @@
|
||||||
***/
|
***/
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <uchar.h>
|
|
||||||
|
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
|
|
||||||
#define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
|
#define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
|
||||||
|
|
||||||
bool unichar_is_valid(char32_t c);
|
bool unichar_is_valid(uint32_t c);
|
||||||
|
|
||||||
const char *utf8_is_valid(const char *s) _pure_;
|
const char *utf8_is_valid(const char *s) _pure_;
|
||||||
char *ascii_is_valid(const char *s) _pure_;
|
char *ascii_is_valid(const char *s) _pure_;
|
||||||
|
@ -39,20 +38,20 @@ bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pu
|
||||||
char *utf8_escape_invalid(const char *s);
|
char *utf8_escape_invalid(const char *s);
|
||||||
char *utf8_escape_non_printable(const char *str);
|
char *utf8_escape_non_printable(const char *str);
|
||||||
|
|
||||||
size_t utf8_encode_unichar(char *out_utf8, char32_t g);
|
size_t utf8_encode_unichar(char *out_utf8, uint32_t g);
|
||||||
char *utf16_to_utf8(const void *s, size_t length);
|
char *utf16_to_utf8(const void *s, size_t length);
|
||||||
|
|
||||||
int utf8_encoded_valid_unichar(const char *str);
|
int utf8_encoded_valid_unichar(const char *str);
|
||||||
char32_t utf8_encoded_to_unichar(const char *str);
|
int utf8_encoded_to_unichar(const char *str);
|
||||||
|
|
||||||
static inline bool utf16_is_surrogate(char16_t c) {
|
static inline bool utf16_is_surrogate(uint16_t c) {
|
||||||
return (0xd800 <= c && c <= 0xdfff);
|
return (0xd800 <= c && c <= 0xdfff);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool utf16_is_trailing_surrogate(char16_t c) {
|
static inline bool utf16_is_trailing_surrogate(uint16_t c) {
|
||||||
return (0xdc00 <= c && c <= 0xdfff);
|
return (0xdc00 <= c && c <= 0xdfff);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline char32_t utf16_surrogate_pair_to_unichar(char16_t lead, char16_t trail) {
|
static inline uint32_t utf16_surrogate_pair_to_unichar(uint16_t lead, uint16_t trail) {
|
||||||
return ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000;
|
return ((lead - 0xd800) << 10) + (trail - 0xdc00) + 0x10000;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue