forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf8.c
60 lines (56 loc) · 1.05 KB
/
utf8.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include "citrine.h"
/**
* UTF8Size
*
* measures the size of character
*/
int ctr_utf8size(char c) {
if ((c & CTR_UTF8_BYTE3) == CTR_UTF8_BYTE3) return 4;
if ((c & CTR_UTF8_BYTE2) == CTR_UTF8_BYTE2) return 3;
if ((c & CTR_UTF8_BYTE1) == CTR_UTF8_BYTE1) return 2;
return 1;
}
/**
* GetUTF8Length
*
* measures the length of an utf8 string in utf8 chars
*/
ctr_size ctr_getutf8len(char* strval, ctr_size max) {
ctr_size i;
ctr_size j = 0;
ctr_size s = 0;
for(i = 0; i < max; i++) {
s = ctr_utf8size(strval[i]);
j += (s - 1);
}
return (i-j);
}
/**
* GetBytesForUTF8String
*/
ctr_size getBytesUtf8(char* strval, long startByte, ctr_size lenUChar) {
long i = 0;
long bytes = 0;
int s = 0;
int x = 0;
long index = 0;
char c;
while(x < lenUChar) {
index = startByte + i;
c = strval[index];
s = ctr_utf8size(c);
bytes = bytes + s;
i = i + s;
x ++;
}
return bytes;
}