-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode.c
More file actions
78 lines (70 loc) · 2.06 KB
/
Copy pathunicode.c
File metadata and controls
78 lines (70 loc) · 2.06 KB
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
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unicode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stoupin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/24 11:55:10 by stoupin #+# #+# */
/* Updated: 2017/04/24 12:23:09 by stoupin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void split_uint(int i, char q[4])
{
int j;
j = 0;
while (j < 4)
{
q[j] = i & 0x3f;
i >>= 6;
j++;
}
}
int buf_put_unicode_char(t_buf *buffer, int uc)
{
int len;
char q[4];
split_uint(uc, q);
len = 0;
if (uc <= 0x007f)
len = buf_putchar(buffer, q[0] + q[1] * 0x40);
else if (uc >= 0x0080 && uc <= 0x07ff)
len = buf_putchar(buffer, 0xc0 + q[1])
+ buf_putchar(buffer, 0x80 + q[0]);
else if (uc >= 0x0800 && uc <= 0xffff)
len = buf_putchar(buffer, 0xe0 + q[2])
+ buf_putchar(buffer, 0x80 + q[1])
+ buf_putchar(buffer, 0x80 + q[0]);
else if (uc >= 0x10000 && uc <= 0x10ffff)
len = buf_putchar(buffer, 0xf0 + q[3])
+ buf_putchar(buffer, 0x80 + q[2])
+ buf_putchar(buffer, 0x80 + q[1])
+ buf_putchar(buffer, 0x80 + q[0]);
return (len);
}
int ft_charlen_u(int uc)
{
if (uc <= 0x007f)
return (1);
else if (uc >= 0x0080 && uc <= 0x07ff)
return (2);
else if (uc >= 0x0800 && uc <= 0xffff)
return (3);
else if (uc >= 0x10000 && uc <= 0x10ffff)
return (4);
return (1);
}
int ft_strlen_u(int *uc)
{
int len;
int i;
len = 0;
i = 0;
while (uc[i] != 0)
{
len += ft_charlen_u(uc[i]);
i++;
}
return (len);
}