-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibutfconv.c
77 lines (61 loc) · 1.16 KB
/
libutfconv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright (C) 2023 Cyril Hrubis <[email protected]>
*/
#include <string.h>
#include <stdlib.h>
#include "libutfconv.h"
static const char *enc_names[LU_ENC_CNT] = {
"none",
"cp1250",
"cp1251",
"cp1252",
"cp1253",
"cp1254",
"cp1255",
"cp1256",
"cp1257",
"cp1258",
};
enum lu_enc lu_name_to_enc(const char *enc_name)
{
unsigned int i;
for (i = 0; i < LU_ENC_CNT; i++) {
if (!strcasecmp(enc_name, enc_names[i]))
return i;
}
return LU_ENC_ERR;
}
const char *lu_enc_to_name(enum lu_enc enc)
{
if (enc >= LU_ENC_CNT)
return "invalid";
return enc_names[enc];
}
size_t lu_to_utf8_size(const char *str, enum lu_enc enc)
{
size_t len = 1;
while (*str) {
len += lu_to_utf8_bytes(*str, enc);
str++;
}
return len;
}
void lu_to_utf8_cpy(const char *src, char *dest, enum lu_enc enc)
{
while (*src) {
uint32_t utf = lu_to_utf(*src, enc);
dest += lu_to_utf8(utf, dest);
src++;
}
}
char *lu_to_utf8_alloc(const char *src, enum lu_enc enc)
{
char *ret = malloc(lu_to_utf8_size(src, enc));
if (!ret)
return NULL;
while (*src)
ret += lu_to_utf8(lu_to_utf(*(src++), enc), ret);
*ret = 0;
return ret;
}