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