intl/icu/source/common/ustrfmt.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ustrfmt.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +*   Copyright (C) 2001-2006, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +*/
    1.10 +
    1.11 +#include "cstring.h"
    1.12 +#include "ustrfmt.h"
    1.13 +
    1.14 +
    1.15 +/***
    1.16 + * Fills in a UChar* string with the radix-based representation of a
    1.17 + * uint32_t number padded with zeroes to minwidth.  The result
    1.18 + * will be null terminated if there is room.
    1.19 + *
    1.20 + * @param buffer UChar buffer to receive result
    1.21 + * @param capacity capacity of buffer
    1.22 + * @param i the unsigned number to be formatted
    1.23 + * @param radix the radix from 2..36
    1.24 + * @param minwidth the minimum width.  If the result is narrower than
    1.25 + *        this, '0's will be added on the left.  Must be <=
    1.26 + *        capacity.
    1.27 + * @return the length of the result, not including any terminating
    1.28 + *        null
    1.29 + */
    1.30 +U_CAPI int32_t U_EXPORT2
    1.31 +uprv_itou (UChar * buffer, int32_t capacity,
    1.32 +           uint32_t i, uint32_t radix, int32_t minwidth)
    1.33 +{
    1.34 +    int32_t length = 0;
    1.35 +    int digit;
    1.36 +    int32_t j;
    1.37 +    UChar temp;
    1.38 +
    1.39 +    do{
    1.40 +        digit = (int)(i % radix);
    1.41 +        buffer[length++]=(UChar)(digit<=9?(0x0030+digit):(0x0030+digit+7));
    1.42 +        i=i/radix;
    1.43 +    } while(i && length<capacity);
    1.44 +
    1.45 +    while (length < minwidth){
    1.46 +        buffer[length++] = (UChar) 0x0030;/*zero padding */
    1.47 +    }
    1.48 +    /* null terminate the buffer */
    1.49 +    if(length<capacity){
    1.50 +        buffer[length] = (UChar) 0x0000;
    1.51 +    }
    1.52 +
    1.53 +    /* Reverses the string */
    1.54 +    for (j = 0; j < (length / 2); j++){
    1.55 +        temp = buffer[(length-1) - j];
    1.56 +        buffer[(length-1) - j] = buffer[j];
    1.57 +        buffer[j] = temp;
    1.58 +    }
    1.59 +    return length;
    1.60 +}

mercurial