rutie/binding/
encoding.rs

1use crate::{
2    rubysys::{encoding, string, vm},
3    types::{c_char, c_int, size_t, EncodingIndex, EncodingType, Value, ValueType},
4    util,
5};
6use std::ffi::CString;
7
8pub fn default_external() -> Value {
9    unsafe { encoding::rb_enc_default_external() }
10}
11
12pub fn default_internal() -> Value {
13    unsafe { encoding::rb_enc_default_internal() }
14}
15
16pub fn force_encoding(s: Value, enc: Value) -> Value {
17    unsafe { encoding::rb_enc_associate(s, encoding::rb_to_encoding(enc)) }
18}
19
20pub fn coderange_clear(obj: Value) {
21    unsafe { encoding::coderange_clear(obj) }
22}
23
24// best str1/str2 encoding or nil if incompatible
25pub fn compatible_encoding(str1: Value, str2: Value) -> Value {
26    unsafe { encoding::rb_enc_from_encoding(encoding::rb_enc_compatible(str1, str2)) }
27}
28
29pub fn is_compatible_encoding(str1: Value, str2: Value) -> bool {
30    compatible_encoding(str1, str2).ty() != ValueType::Nil
31}
32
33pub fn from_encoding_index(idx: EncodingIndex) -> Value {
34    unsafe { encoding::rb_enc_from_encoding(encoding::rb_enc_from_index(idx)) }
35}
36
37pub fn usascii_encoding() -> Value {
38    unsafe { from_encoding_index(encoding::rb_usascii_encindex()) }
39}
40
41pub fn utf8_encoding() -> Value {
42    unsafe { from_encoding_index(encoding::rb_utf8_encindex()) }
43}
44
45pub fn enc_get_index(s: Value) -> EncodingIndex {
46    let idx = unsafe { encoding::rb_enc_get_index(s) };
47
48    idx
49}
50
51pub fn find_encoding_index(name: &str) -> EncodingIndex {
52    let cstr = CString::new(name).unwrap();
53    let idx = unsafe { encoding::rb_enc_find_index(cstr.as_ptr()) };
54
55    idx
56}
57
58pub fn encode(str: Value, to: Value, ecflags: c_int, ecopts: Value) -> Value {
59    unsafe { encoding::rb_str_encode(str, to, ecflags, ecopts) }
60}
61
62pub fn econv_prepare_opts(opthash: Value, opts: *const Value) -> c_int {
63    unsafe { encoding::rb_econv_prepare_opts(opthash, opts) }
64}
65
66// ptr - pointer for current point in string starting from the beginning
67// end - pointer for the end of the string
68// len_p - a mutable integer pointer for Ruby to give us how much we need to add on to `ptr`
69// enc - the encoding the codepoints will be based on
70pub fn next_codepoint(
71    ptr: *const c_char,
72    end: *const c_char,
73    len_p: *mut c_int,
74    enc: Value,
75) -> usize {
76    unsafe { encoding::rb_enc_codepoint_len(ptr, end, len_p, encoding::rb_to_encoding(enc)) }
77}