-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathucore_ext.c
168 lines (158 loc) · 4.37 KB
/
ucore_ext.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "icu_common.h"
extern VALUE rb_cUString;
extern VALUE icu_ustr_new_set(const UChar * str, long len, long capa);
/**
* call-seq:
* ary.to_u => anUString
*
* Creates UString from array of fixnums, representing Unicode codepoints.
* (inversion of UString#codepoints)
*
* a = "поддержка".to_u.codepoints # => [1087, 1086, 1076, 1076, 1077, 1088, 1078, 1082, 1072]
* a.to_u # => "поддержка"
*
*/
VALUE icu_ustr_from_array(obj)
VALUE obj;
{
int i, n;
VALUE *p;
VALUE ret, temp;
UChar32 * src , *pos, chr;
UChar * buf;
int32_t len, capa;
UErrorCode status = U_ZERO_ERROR;
n = RARRAY_LEN(obj);
p = RARRAY_PTR(obj);
src = ALLOC_N(UChar32, n);
pos = src;
for ( i = 0; i < n; i++){
temp = p[i];
if(TYPE(temp) != T_FIXNUM) {
free(src);
rb_raise(rb_eTypeError, "Can't convert from %s", rb_class2name(CLASS_OF(temp)));
}
chr = (UChar32) FIX2INT(temp);
// invalid codepoints are converted to U+FFFD
if( ! (U_IS_UNICODE_CHAR(chr)) ) {
chr = 0xFFFD;
}
*pos = chr;
pos ++;
}
capa = n+1;
buf = ALLOC_N(UChar, capa);
u_strFromUTF32(buf, capa, &len, src, n, &status);
if( U_BUFFER_OVERFLOW_ERROR == status ){
capa = len+1;
REALLOC_N(buf, UChar, capa);
status = U_ZERO_ERROR;
u_strFromUTF32(buf, capa, &len, src, n, &status);
}
if (U_FAILURE(status) ) {
free(src);
free(buf);
rb_raise(rb_eRuntimeError, u_errorName(status));
}
if( capa <= len ){
++capa;
REALLOC_N(buf, UChar, capa);
}
ret = icu_ustr_new_set(buf, len, capa);
free(src);
return ret;
}
/**
* call-seq:
* str.to_u(encoding = 'utf8') => String
*
* Converts String value in given encoding to UString.
* When no encoding is given, utf8 is assumed. If string is not valid UTF8,
* and no encoding is given, exception is raised.
*
* When explicit encoding is given, converter will replace incorrect codepoints
* with <U+FFFD> - replacement character.
*/
VALUE
icu_from_rstr(argc, argv, str)
int argc;
VALUE *argv,
str;
{
VALUE enc;
char *encoding = 0; /* default */
UErrorCode error = 0;
int32_t capa, len;
VALUE s;
UChar * buf;
UConverter * conv;
if (rb_scan_args(argc, argv, "01", &enc) == 1) {
Check_Type(enc, T_STRING);
encoding = RSTRING_PTR(enc);
}
capa = RSTRING_LEN(str) + 1;
buf = ALLOC_N(UChar, capa);
if(! encoding || !strncmp(encoding, "utf8", 4) ) {
/* from UTF8 */
u_strFromUTF8(buf, capa-1, &len, RSTRING_PTR(str), RSTRING_LEN(str), &error);
if( U_FAILURE(error)) {
free(buf);
rb_raise(rb_eArgError, u_errorName(error));
}
s = icu_ustr_new_set(buf, len, capa);
} else {
conv = ucnv_open(encoding, &error);
if (U_FAILURE(error)) {
ucnv_close(conv);
rb_raise(rb_eArgError, u_errorName(error));
}
len = ucnv_toUChars(conv, buf, capa-1, RSTRING_PTR(str),
RSTRING_LEN(str), &error);
if (U_BUFFER_OVERFLOW_ERROR == error) {
capa = len+1;
REALLOC_N(buf, UChar, capa);
error = 0;
len = ucnv_toUChars(conv, buf, capa-1, RSTRING_PTR(str),
RSTRING_LEN(str), &error);
if (U_FAILURE(error)) {
free(buf);
rb_raise(rb_eArgError, u_errorName(error));
}
}
s = icu_ustr_new_set(buf, len, capa);
ucnv_close(conv);
}
return s;
}
/**
* call-seq:
* u(str, enc = 'utf8') => UString
*
* Global function to convert from String to UString
*/
VALUE
icu_f_rb_str(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE enc;
VALUE str;
if (rb_scan_args(argc, argv, "11", &str, &enc) == 2) {
Check_Type(enc, T_STRING);
Check_Type(str, T_STRING);
return icu_from_rstr(1, &enc, str);
} else {
Check_Type(str, T_STRING);
return icu_from_rstr(0, NULL, str);
}
}
void initialize_ucore_ext(void)
{
/* conversion from String to UString */
rb_define_method(rb_cString, "to_u", icu_from_rstr, -1);
rb_define_alias(rb_cString, "u", "to_u");
rb_define_global_function("u", icu_f_rb_str, -1);
/* conversion from Array to UString */
rb_define_method(rb_cArray, "to_u", icu_ustr_from_array, 0);
}