1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**********************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Copyright (C) 2001-2006, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru**********************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cstring.h"
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ustrfmt.h"
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/***
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Fills in a UChar* string with the radix-based representation of a
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * uint32_t number padded with zeroes to minwidth.  The result
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * will be null terminated if there is room.
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param buffer UChar buffer to receive result
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param capacity capacity of buffer
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param i the unsigned number to be formatted
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param radix the radix from 2..36
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param minwidth the minimum width.  If the result is narrower than
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *        this, '0's will be added on the left.  Must be <=
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *        capacity.
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @return the length of the result, not including any terminating
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *        null
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_itou (UChar * buffer, int32_t capacity,
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru           uint32_t i, uint32_t radix, int32_t minwidth)
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t length = 0;
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int digit;
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t j;
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UChar temp;
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    do{
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        digit = (int)(i % radix);
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++]=(UChar)(digit<=9?(0x0030+digit):(0x0030+digit+7));
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        i=i/radix;
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } while(i && length<capacity);
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while (length < minwidth){
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length++] = (UChar) 0x0030;/*zero padding */
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* null terminate the buffer */
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(length<capacity){
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[length] = (UChar) 0x0000;
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* Reverses the string */
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (j = 0; j < (length / 2); j++){
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        temp = buffer[(length-1) - j];
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[(length-1) - j] = buffer[j];
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        buffer[j] = temp;
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
58