use crate::rubysys::{
constant::{FL_USER_8, FL_USER_9},
types::{
c_char, c_int, c_long, c_uint, 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;
#[repr(C)]
pub struct RbEconv {
_private: [u8; 0],
}
pub type EconvResult = c_int;
pub const ECONV_RESULT_INVALID_BYTE_SEQUENCE: EconvResult = 0;
pub const ECONV_RESULT_UNDEFINED_CONVERSION: EconvResult = 1;
pub const ECONV_RESULT_DESTINATION_BUFFER_FULL: EconvResult = 2;
pub const ECONV_RESULT_SOURCE_BUFFER_EMPTY: EconvResult = 3;
pub const ECONV_RESULT_FINISHED: EconvResult = 4;
pub const ECONV_RESULT_AFTER_OUTPUT: EconvResult = 5;
pub const ECONV_RESULT_INCOMPLETE_INPUT: EconvResult = 6;
pub const ECONV_ERROR_HANDLER_MASK: c_int = 0x000000ff;
pub const ECONV_INVALID_MASK: c_int = 0x0000000f;
pub const ECONV_INVALID_REPLACE: c_int = 0x00000002;
pub const ECONV_UNDEF_MASK: c_int = 0x000000f0;
pub const ECONV_UNDEF_REPLACE: c_int = 0x00000020;
pub const ECONV_UNDEF_HEX_CHARREF: c_int = 0x00000030;
pub const ECONV_DECORATOR_MASK: c_int = 0x0001ff00;
pub const ECONV_NEWLINE_DECORATOR_MASK: c_int = 0x00007f00;
pub const ECONV_NEWLINE_DECORATOR_READ_MASK: c_int = 0x00000f00;
pub const ECONV_NEWLINE_DECORATOR_WRITE_MASK: c_int = 0x00007000;
pub const ECONV_UNIVERSAL_NEWLINE_DECORATOR: c_int = 0x00000100;
pub const ECONV_CRLF_NEWLINE_DECORATOR: c_int = 0x00001000;
pub const ECONV_CR_NEWLINE_DECORATOR: c_int = 0x00002000;
pub const ECONV_LF_NEWLINE_DECORATOR: c_int = 0x00004000;
pub const ECONV_XML_TEXT_DECORATOR: c_int = 0x00008000;
pub const ECONV_XML_ATTR_CONTENT_DECORATOR: c_int = 0x00010000;
pub const ECONV_STATEFUL_DECORATOR_MASK: c_int = 0x00f00000;
pub const ECONV_XML_ATTR_QUOTE_DECORATOR: c_int = 0x00100000;
#[cfg(windows)]
pub const ECONV_DEFAULT_NEWLINE_DECORATOR: c_int = ECONV_CRLF_NEWLINE_DECORATOR;
#[cfg(not(windows))]
pub const ECONV_DEFAULT_NEWLINE_DECORATOR: c_int = 0;
pub const ECONV_PARTIAL_INPUT: c_int = 0x00020000;
pub const ECONV_AFTER_OUTPUT: c_int = 0x00040000;
#[cfg_attr(rutie_dllimport, link(name = "rutie_ruby"))]
extern "C" {
pub fn rb_enc_associate(obj: Value, enc: EncodingType) -> Value;
pub fn rb_enc_associate_index(obj: Value, idx: c_int) -> Value;
pub fn rb_enc_compatible(str1: Value, str2: Value) -> EncodingType;
pub fn rb_enc_default_external() -> Value;
pub fn rb_enc_default_internal() -> Value;
pub fn rb_enc_find_index(name: *const c_char) -> EncodingIndex;
pub fn rb_enc_from_encoding(encoding: EncodingType) -> Value;
pub fn rb_enc_from_index(index: EncodingIndex) -> EncodingType;
pub fn rb_enc_get_index(obj: Value) -> EncodingIndex;
pub fn rb_enc_set_index(obj: Value, encindex: EncodingIndex);
pub fn rb_enc_set_default_external(encoding: Value);
pub fn rb_enc_set_default_internal(encoding: Value);
pub fn rb_filesystem_encindex() -> EncodingIndex;
pub fn rb_locale_encindex() -> EncodingIndex;
pub fn rb_obj_encoding(obj: Value) -> Value;
pub fn rb_to_encoding(enc: Value) -> EncodingType;
pub fn rb_to_encoding_index(obj: Value) -> EncodingIndex;
pub fn rb_usascii_encindex() -> EncodingIndex;
pub fn rb_utf8_encindex() -> EncodingIndex;
pub fn rb_ascii8bit_encoding() -> EncodingType;
pub fn rb_enc_ascget(
p: *const c_char,
e: *const c_char,
len: *mut c_int,
enc: EncodingType,
) -> c_int;
pub fn rb_enc_check(str1: Value, str2: Value) -> EncodingType;
pub fn rb_enc_codelen(code: c_int, enc: EncodingType) -> c_int;
pub fn rb_enc_copy(destination: Value, source: Value);
pub fn rb_enc_find(name: *const c_char) -> EncodingType;
pub fn rb_enc_get(object: Value) -> EncodingType;
pub fn rb_enc_precise_mbclen(p: *const c_char, e: *const c_char, enc: EncodingType) -> c_int;
pub fn rb_enc_str_buf_cat(
string: Value,
ptr: *const c_char,
len: c_long,
encoding: EncodingType,
) -> Value;
pub fn rb_enc_to_index(encoding: EncodingType) -> c_int;
pub fn rb_enc_uint_chr(code: u32, encoding: EncodingType) -> Value;
pub fn rb_filesystem_encoding() -> EncodingType;
pub fn rb_locale_encoding() -> EncodingType;
pub fn rb_usascii_encoding() -> EncodingType;
pub fn rb_utf8_encoding() -> EncodingType;
pub fn rb_enc_mbclen(p: *const c_char, e: *const c_char, enc: EncodingType) -> c_int;
pub fn rb_enc_nth(
p: *const c_char,
e: *const c_char,
nth: c_long,
enc: EncodingType,
) -> *const c_char;
pub fn rb_enc_str_coderange(string: Value) -> c_int;
pub fn rb_str_export_to_enc(str: Value, enc: EncodingType) -> Value;
pub fn rb_str_encode(str: Value, to: Value, ecflags: c_int, ecopts: Value) -> Value;
pub fn rb_econv_prepare_opts(opthash: Value, opts: *mut Value) -> c_int;
pub fn rb_enc_codepoint_len(
ptr: *const c_char,
end: *const c_char,
len_p: *mut c_int,
enc: EncodingType,
) -> c_uint;
pub fn rb_ascii8bit_encindex() -> EncodingIndex;
pub fn rb_char_to_option_kcode(c: c_int, option: *mut c_int, kcode: *mut c_int) -> c_int;
pub fn rb_default_external_encoding() -> EncodingType;
pub fn rb_default_internal_encoding() -> EncodingType;
pub fn rb_define_dummy_encoding(name: *const c_char) -> EncodingIndex;
pub fn rb_enc_alias(alias: *const c_char, orig: *const c_char) -> c_int;
pub fn rb_enc_capable(obj: Value) -> c_int;
pub fn rb_enc_dummy_p(enc: EncodingType) -> c_int;
pub fn rb_enc_fast_mbclen(p: *const c_char, e: *const c_char, enc: EncodingType) -> c_int;
pub fn rb_enc_unicode_p(enc: EncodingType) -> c_int;
pub fn rb_find_encoding(obj: Value) -> EncodingType;
pub fn rb_locale_charmap(klass: Value) -> Value;
pub fn rb_enc_tolower(c: c_int, enc: EncodingType) -> c_int;
pub fn rb_enc_toupper(c: c_int, enc: EncodingType) -> c_int;
pub fn rb_enc_path_next(
path: *const c_char,
end: *const c_char,
enc: EncodingType,
) -> *mut c_char;
pub fn rb_enc_path_skip_prefix(
path: *const c_char,
end: *const c_char,
enc: EncodingType,
) -> *mut c_char;
pub fn rb_enc_path_last_separator(
path: *const c_char,
end: *const c_char,
enc: EncodingType,
) -> *mut c_char;
pub fn rb_enc_path_end(
path: *const c_char,
end: *const c_char,
enc: EncodingType,
) -> *mut c_char;
pub fn ruby_enc_find_basename(
name: *const c_char,
baselen: *mut c_long,
alllen: *mut c_long,
enc: EncodingType,
) -> *const c_char;
pub fn ruby_enc_find_extname(
name: *const c_char,
len: *mut c_long,
enc: EncodingType,
) -> *const c_char;
pub fn rb_econv_has_convpath_p(
from_encoding: *const c_char,
to_encoding: *const c_char,
) -> c_int;
pub fn rb_econv_prepare_options(opthash: Value, ecopts: *mut Value, ecflags: c_int) -> c_int;
pub fn rb_econv_open(
source_encoding: *const c_char,
destination_encoding: *const c_char,
ecflags: c_int,
) -> *mut RbEconv;
pub fn rb_econv_open_opts(
source_encoding: *const c_char,
destination_encoding: *const c_char,
ecflags: c_int,
ecopts: Value,
) -> *mut RbEconv;
pub fn rb_econv_open_exc(senc: *const c_char, denc: *const c_char, ecflags: c_int) -> Value;
pub fn rb_econv_convert(
ec: *mut RbEconv,
source_buffer_ptr: *mut *const u8,
source_buffer_end: *const u8,
destination_buffer_ptr: *mut *mut u8,
destination_buffer_end: *mut u8,
flags: c_int,
) -> EconvResult;
pub fn rb_econv_close(ec: *mut RbEconv);
pub fn rb_econv_set_replacement(
ec: *mut RbEconv,
str: *const u8,
len: size_t,
encname: *const c_char,
) -> c_int;
pub fn rb_econv_decorate_at_first(ec: *mut RbEconv, decorator_name: *const c_char) -> c_int;
pub fn rb_econv_decorate_at_last(ec: *mut RbEconv, decorator_name: *const c_char) -> c_int;
pub fn rb_econv_insert_output(
ec: *mut RbEconv,
str: *const u8,
len: size_t,
str_encoding: *const c_char,
) -> c_int;
pub fn rb_econv_encoding_to_insert_output(ec: *mut RbEconv) -> *const c_char;
pub fn rb_econv_check_error(ec: *mut RbEconv);
pub fn rb_econv_make_exception(ec: *mut RbEconv) -> Value;
pub fn rb_econv_putbackable(ec: *mut RbEconv) -> c_int;
pub fn rb_econv_putback(ec: *mut RbEconv, p: *mut u8, n: c_int);
pub fn rb_econv_asciicompat_encoding(encname: *const c_char) -> *const c_char;
pub fn rb_econv_str_convert(ec: *mut RbEconv, src: Value, flags: c_int) -> Value;
pub fn rb_econv_substr_convert(
ec: *mut RbEconv,
src: Value,
byteoff: c_long,
bytesize: c_long,
flags: c_int,
) -> Value;
pub fn rb_econv_str_append(ec: *mut RbEconv, src: Value, dst: Value, flags: c_int) -> Value;
pub fn rb_econv_substr_append(
ec: *mut RbEconv,
src: Value,
byteoff: c_long,
bytesize: c_long,
dst: Value,
flags: c_int,
) -> Value;
pub fn rb_econv_append(
ec: *mut RbEconv,
bytesrc: *const c_char,
bytesize: c_long,
dst: Value,
flags: c_int,
) -> Value;
pub fn rb_econv_binmode(ec: *mut RbEconv);
}
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)
}
#[repr(C)]
struct OnigEncodingPrefix {
precise_mbc_enc_len: CallbackPtr,
name: *const c_char,
max_enc_len: c_int,
min_enc_len: c_int,
}
pub unsafe fn enc_mbminlen(enc: EncodingType) -> c_int {
(*(enc as *const OnigEncodingPrefix)).min_enc_len
}
pub unsafe fn enc_mbmaxlen(enc: EncodingType) -> c_int {
(*(enc as *const OnigEncodingPrefix)).max_enc_len
}
pub unsafe fn enc_asciicompat(enc: EncodingType) -> bool {
enc_mbminlen(enc) == 1 && rb_enc_dummy_p(enc) == 0
}