added moo_byte_to_ucstr()

This commit is contained in:
hyunghwan.chung 2019-11-20 09:32:44 +00:00
parent 3a27f74492
commit e136427d63
2 changed files with 60 additions and 6 deletions

View File

@ -609,17 +609,37 @@ MOO_EXPORT moo_oow_t moo_count_bcstrl (
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
#define MOO_BYTE_TO_BCSTR_RADIXMASK (0xFF) #define MOO_BYTE_TO_OOCSTR_RADIXMASK (0xFF)
#define MOO_BYTE_TO_BCSTR_LOWERCASE (1 << 8) #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_oow_t moo_byte_to_bcstr (
moo_uint8_t byte, moo_oob_t byte,
moo_bch_t* buf, moo_bch_t* buf,
moo_oow_t size, moo_oow_t size,
int flagged_radix, int flagged_radix,
moo_bch_t fill 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 ( MOO_EXPORT int moo_get_ucwidth (

View File

@ -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; moo_bch_t* p = tmp, * bp = buf, * be = buf + size - 1;
int radix; int radix;
moo_bch_t radix_char; 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 do
{ {
moo_uint8_t digit = byte % radix; moo_oob_t digit = byte % radix;
if (digit < 10) *p++ = digit + '0'; if (digit < 10) *p++ = digit + '0';
else *p++ = digit + radix_char - 10; else *p++ = digit + radix_char - 10;
byte /= radix; byte /= radix;