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