use core::ffi::c_int;

use super::locale::locale_t;

mod ctype_b;

#[no_mangle]
unsafe extern "C" fn isalnum(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_alphanumeric()) as _
}

#[no_mangle]
unsafe extern "C" fn isalpha(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_alphabetic()) as _
}

#[no_mangle]
unsafe extern "C" fn isascii(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii()) as _
}

#[no_mangle]
pub unsafe extern "C" fn isblank(ch: c_int) -> c_int {
    (ch == b' ' as c_int || ch == b'\t' as c_int) as _
}

#[no_mangle]
unsafe extern "C" fn iscntrl(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_control()) as _
}

#[no_mangle]
unsafe extern "C" fn isdigit(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_digit()) as _
}

#[no_mangle]
unsafe extern "C" fn isgraph(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_graphic()) as _
}

#[no_mangle]
unsafe extern "C" fn islower(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_lowercase()) as _
}

#[no_mangle]
unsafe extern "C" fn isprint(ch: c_int) -> c_int {
    (ch < 0xFF && ((ch as u8).is_ascii_graphic() || (ch as u8).is_ascii_whitespace())) as _
}

#[no_mangle]
unsafe extern "C" fn ispunct(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_punctuation()) as _
}

#[no_mangle]
unsafe extern "C" fn isspace(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_whitespace()) as _
}

#[no_mangle]
unsafe extern "C" fn isupper(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_uppercase()) as _
}

#[no_mangle]
unsafe extern "C" fn isxdigit(ch: c_int) -> c_int {
    (ch < 0xFF && (ch as u8).is_ascii_hexdigit()) as _
}

#[no_mangle]
unsafe extern "C" fn toascii(ch: c_int) -> c_int {
    ch & 0x7F
}

#[no_mangle]
unsafe extern "C" fn tolower(ch: c_int) -> c_int {
    if ch < 0xFF {
        (ch as u8).to_ascii_lowercase() as _
    } else {
        ch
    }
}

#[no_mangle]
unsafe extern "C" fn toupper(ch: c_int) -> c_int {
    if ch < 0xFF {
        (ch as u8).to_ascii_uppercase() as _
    } else {
        ch
    }
}

#[no_mangle]
unsafe extern "C" fn isalnum_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isalpha_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isblank_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn iscntrl_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isdigit_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isgraph_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn islower_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isprint_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn ispunct_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isspace_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isupper_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn isxdigit_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn tolower_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}

#[no_mangle]
unsafe extern "C" fn toupper_l(_ch: c_int, _locale: locale_t) -> c_int {
    unimplemented!()
}
