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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::rubysys::{
constant::{FL_USER_8, FL_USER_9},
types::{
c_char, c_int, c_long, size_t, CallbackPtr, EncodingIndex, EncodingType, InternalValue,
RBasic, Value,
},
};
use std::mem;
pub const ENC_DUMMY_FLAG: isize = 1 << 24;
pub const ENC_INDEX_MASK: isize = !(!0 << 24);
pub const ENC_CODERANGE_UNKNOWN: isize = 0;
pub const ENC_CODERANGE_7BIT: isize = FL_USER_8;
pub const ENC_CODERANGE_VALID: isize = FL_USER_9;
pub const ENC_CODERANGE_BROKEN: isize = FL_USER_8 | FL_USER_9;
pub const ENC_CODERANGE_MASK: isize =
ENC_CODERANGE_7BIT | ENC_CODERANGE_VALID | ENC_CODERANGE_BROKEN;
#[cfg_attr(rutie_dllimport, link(name = "rutie_ruby"))]
extern "C" {
// VALUE
// rb_enc_associate(VALUE obj, rb_encoding *enc)
pub fn rb_enc_associate(obj: Value, enc: EncodingType) -> Value;
// VALUE
// rb_enc_associate_index(VALUE obj, int idx)
pub fn rb_enc_associate_index(obj: Value, idx: c_int) -> Value;
// rb_encoding*
// rb_enc_compatible(VALUE str1, VALUE str2)
pub fn rb_enc_compatible(str1: Value, str2: Value) -> EncodingType;
// VALUE
// rb_enc_default_external(void)
pub fn rb_enc_default_external() -> Value;
// VALUE
// rb_enc_default_internal(void)
pub fn rb_enc_default_internal() -> Value;
// int
// rb_enc_find_index(const char *name)
pub fn rb_enc_find_index(name: *const c_char) -> EncodingIndex;
// ------------------------------------------------------
// LINKER CANNOT FIND
// // static VALUE
// // rb_enc_from_encoding_index(int idx)
// pub fn rb_enc_from_encoding_index(idx: c_int) -> Value;
// ------------------------------------------------------
// VALUE
// rb_enc_from_encoding(rb_encoding *encoding)
pub fn rb_enc_from_encoding(encoding: EncodingType) -> Value;
// rb_encoding *
// rb_enc_from_index(int index)
pub fn rb_enc_from_index(index: EncodingIndex) -> EncodingType;
// int
// rb_enc_get_index(VALUE obj)
pub fn rb_enc_get_index(obj: Value) -> EncodingIndex;
// void
// rb_enc_set_index(VALUE obj, int idx)
pub fn rb_enc_set_index(obj: Value, encindex: EncodingIndex);
// void
// rb_enc_set_default_external(VALUE encoding)
pub fn rb_enc_set_default_external(encoding: Value);
// void
// rb_enc_set_default_internal(VALUE encoding)
pub fn rb_enc_set_default_internal(encoding: Value);
// int
// rb_filesystem_encindex(void)
pub fn rb_filesystem_encindex() -> EncodingIndex;
// int
// rb_locale_encindex(void)
pub fn rb_locale_encindex() -> EncodingIndex;
// VALUE
// rb_obj_encoding(VALUE obj)
pub fn rb_obj_encoding(obj: Value) -> Value;
// rb_encoding *
// rb_to_encoding(VALUE enc)
pub fn rb_to_encoding(enc: Value) -> EncodingType;
// int
// rb_to_encoding_index(VALUE enc)
pub fn rb_to_encoding_index(obj: Value) -> EncodingIndex;
// int
// rb_usascii_encindex(void)
pub fn rb_usascii_encindex() -> EncodingIndex;
// int
// rb_utf8_encindex(void)
pub fn rb_utf8_encindex() -> EncodingIndex;
// rb_encoding *
// rb_ascii8bit_encoding(void)
pub fn rb_ascii8bit_encoding() -> EncodingType;
// int
// rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
pub fn rb_enc_ascget(
p: *const c_char,
e: *const c_char,
len: *mut c_int,
enc: EncodingType,
) -> c_int;
// rb_encoding *
// rb_enc_check(VALUE str1, VALUE str2)
pub fn rb_enc_check(str1: Value, str2: Value) -> EncodingType;
// int
// rb_enc_codelen(int code, rb_encoding *enc)
pub fn rb_enc_codelen(code: c_int, enc: EncodingType) -> c_int;
// void
// rb_enc_copy(VALUE obj1, VALUE obj2)
pub fn rb_enc_copy(destination: Value, source: Value);
// rb_encoding *
// rb_enc_find(const char *name)
pub fn rb_enc_find(name: *const c_char) -> EncodingType;
// rb_encoding *
// rb_enc_get(VALUE obj)
//
// Null for objects without an encoding.
pub fn rb_enc_get(object: Value) -> EncodingType;
// int
// rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
pub fn rb_enc_precise_mbclen(p: *const c_char, e: *const c_char, enc: EncodingType) -> c_int;
// VALUE
// rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *ptr_enc)
pub fn rb_enc_str_buf_cat(
string: Value,
ptr: *const c_char,
len: c_long,
encoding: EncodingType,
) -> Value;
// int
// rb_enc_to_index(rb_encoding *enc)
pub fn rb_enc_to_index(encoding: EncodingType) -> c_int;
// VALUE
// rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
pub fn rb_enc_uint_chr(code: u32, encoding: EncodingType) -> Value;
// rb_encoding *
// rb_filesystem_encoding(void)
pub fn rb_filesystem_encoding() -> EncodingType;
// rb_encoding *
// rb_locale_encoding(void)
pub fn rb_locale_encoding() -> EncodingType;
// rb_encoding *
// rb_usascii_encoding(void)
pub fn rb_usascii_encoding() -> EncodingType;
// rb_encoding *
// rb_utf8_encoding(void)
pub fn rb_utf8_encoding() -> EncodingType;
// int
// rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
pub fn rb_enc_mbclen(p: *const c_char, e: *const c_char, enc: EncodingType) -> c_int;
// char *
// rb_enc_nth(const char *p, const char *e, long nth, rb_encoding *enc)
pub fn rb_enc_nth(
p: *const c_char,
e: *const c_char,
nth: c_long,
enc: EncodingType,
) -> *const c_char;
// int
// rb_enc_str_coderange(VALUE str)
//
// Computes and caches the coderange; returns one of `ENC_CODERANGE_*`.
pub fn rb_enc_str_coderange(string: Value) -> c_int;
// VALUE
// rb_str_export_to_enc(VALUE str, rb_encoding *enc)
pub fn rb_str_export_to_enc(str: Value, enc: EncodingType) -> Value;
// VALUE
// rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
pub fn rb_str_encode(str: Value, to: Value, ecflags: c_int, ecopts: Value) -> Value;
// int
// rb_econv_prepare_opts(VALUE opthash, VALUE *opts)
pub fn rb_econv_prepare_opts(opthash: Value, opts: *mut Value) -> c_int;
// unsigned int
// rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
pub fn rb_enc_codepoint_len(
ptr: *const c_char,
end: *const c_char,
len_p: *mut c_int,
enc: EncodingType,
) -> size_t;
}
pub unsafe fn coderange_set(obj: Value, code_range: InternalValue) {
let basic: *mut RBasic = obj.value as *mut RBasic;
(*basic).flags = ((*basic).flags & !(ENC_CODERANGE_MASK as InternalValue)) | code_range
}
pub unsafe fn coderange_clear(obj: Value) {
coderange_set(obj, 0)
}