// C11 uchar.h — char16_t / char32_t plus minimal conversion helpers.
//
// The W65816 runtime treats text as Latin-1 (8-bit) throughout, so
// the 16-bit and 32-bit char types are degenerate one-byte mappings
// (high bytes always zero).  Conversion functions are provided for
// surface-compatibility; they do not handle multi-byte UTF-8 input.

#ifndef _UCHAR_H
#define _UCHAR_H

#include <stdint.h>
#include <stddef.h>

typedef uint16_t char16_t;
typedef uint32_t char32_t;

// mbstate_t is the multi-byte conversion state.  Empty struct — our
// 1:1 byte mapping is stateless.
typedef struct { int unused; } mbstate_t;

// mbrtoc16 / c16rtomb — multibyte <-> char16_t.  In our Latin-1
// model these are byte-for-byte copies.
static inline size_t mbrtoc16(char16_t *out, const char *s, size_t n, mbstate_t *ps) {
    (void)ps;
    if (!s || n == 0) return (size_t)-2;
    unsigned char c = (unsigned char)*s;
    if (out) *out = (char16_t)c;
    return (c == 0) ? 0 : 1;
}

static inline size_t c16rtomb(char *s, char16_t c, mbstate_t *ps) {
    (void)ps;
    if (!s) return 1;
    *s = (char)(c & 0xFF);
    return 1;
}

static inline size_t mbrtoc32(char32_t *out, const char *s, size_t n, mbstate_t *ps) {
    (void)ps;
    if (!s || n == 0) return (size_t)-2;
    unsigned char c = (unsigned char)*s;
    if (out) *out = (char32_t)c;
    return (c == 0) ? 0 : 1;
}

static inline size_t c32rtomb(char *s, char32_t c, mbstate_t *ps) {
    (void)ps;
    if (!s) return 1;
    *s = (char)(c & 0xFF);
    return 1;
}

#endif
