diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index d6d1349..918e431 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -609,17 +609,37 @@ MOO_EXPORT moo_oow_t moo_count_bcstrl ( /* ------------------------------------------------------------------------- */ -#define MOO_BYTE_TO_BCSTR_RADIXMASK (0xFF) -#define MOO_BYTE_TO_BCSTR_LOWERCASE (1 << 8) +#define MOO_BYTE_TO_OOCSTR_RADIXMASK (0xFF) +#define MOO_BYTE_TO_OOCSTR_LOWERCASE (1 << 8) + +#define MOO_BYTE_TO_UCSTR_RADIXMASK MOO_BYTE_TO_OOCSTR_RADIXMASK +#define MOO_BYTE_TO_UCSTR_LOWERCASE MOO_BYTE_TO_OOCSTR_LOWERCASE + +#define MOO_BYTE_TO_BCSTR_RADIXMASK MOO_BYTE_TO_OOCSTR_RADIXMASK +#define MOO_BYTE_TO_BCSTR_LOWERCASE MOO_BYTE_TO_OOCSTR_LOWERCASE + +moo_oow_t moo_byte_to_ucstr ( + moo_oob_t byte, + moo_uch_t* buf, + moo_oow_t size, + int flagged_radix, + moo_uch_t fill +); moo_oow_t moo_byte_to_bcstr ( - moo_uint8_t byte, + moo_oob_t byte, moo_bch_t* buf, moo_oow_t size, int flagged_radix, moo_bch_t fill ); +#if defined(MOO_OOCH_IS_UCH) +# define moo_byte_to_oocstr moo_byte_to_ucstr +#else +# define moo_byte_to_oocstr moo_byte_to_bcstr +#endif + /* ------------------------------------------------------------------------- */ MOO_EXPORT int moo_get_ucwidth ( diff --git a/moo/lib/utl.c b/moo/lib/utl.c index 7e278ee..5760776 100644 --- a/moo/lib/utl.c +++ b/moo/lib/utl.c @@ -490,9 +490,43 @@ moo_oow_t moo_rotate_uchars (moo_uch_t* str, moo_oow_t len, int dir, moo_oow_t n /* ----------------------------------------------------------------------- */ -moo_oow_t moo_byte_to_bcstr (moo_uint8_t byte, moo_bch_t* buf, moo_oow_t size, int flagged_radix, moo_bch_t fill) +moo_oow_t moo_byte_to_ucstr (moo_oob_t byte, moo_uch_t* buf, moo_oow_t size, int flagged_radix, moo_uch_t fill) { - moo_bch_t tmp[(MOO_SIZEOF(moo_uint8_t) * MOO_BITS_PER_BYTE)]; + moo_uch_t tmp[(MOO_SIZEOF(moo_oob_t) * MOO_BITS_PER_BYTE)]; + moo_uch_t* p = tmp, * bp = buf, * be = buf + size - 1; + int radix; + moo_uch_t radix_char; + + radix = (flagged_radix & MOO_BYTE_TO_UCSTR_RADIXMASK); + radix_char = (flagged_radix & MOO_BYTE_TO_UCSTR_LOWERCASE)? 'a': 'A'; + if (radix < 2 || radix > 36 || size <= 0) return 0; + + do + { + moo_oob_t digit = byte % radix; + if (digit < 10) *p++ = digit + '0'; + else *p++ = digit + radix_char - 10; + byte /= radix; + } + while (byte > 0); + + if (fill != '\0') + { + while (size - 1 > p - tmp) + { + *bp++ = fill; + size--; + } + } + + while (p > tmp && bp < be) *bp++ = *--p; + *bp = '\0'; + return bp - buf; +} + +moo_oow_t moo_byte_to_bcstr (moo_oob_t byte, moo_bch_t* buf, moo_oow_t size, int flagged_radix, moo_bch_t fill) +{ + moo_bch_t tmp[(MOO_SIZEOF(moo_oob_t) * MOO_BITS_PER_BYTE)]; moo_bch_t* p = tmp, * bp = buf, * be = buf + size - 1; int radix; moo_bch_t radix_char; @@ -503,7 +537,7 @@ moo_oow_t moo_byte_to_bcstr (moo_uint8_t byte, moo_bch_t* buf, moo_oow_t size, i do { - moo_uint8_t digit = byte % radix; + moo_oob_t digit = byte % radix; if (digit < 10) *p++ = digit + '0'; else *p++ = digit + radix_char - 10; byte /= radix;