]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
• BaseMemoryLib:
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library worker functions.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: PrintLibInternal.c
14
15 **/
16
17 #include "PrintLibInternal.h"
18
19 static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
20
21
22 /**
23 Internal function that places the character into the Buffer.
24
25 Internal function that places ASCII or Unicode character into the Buffer.
26
27 @param Buffer Buffer to place the Unicode or ASCII string.
28 @param Length Count of character to be placed into Buffer.
29 @param Character Character to be placed into Buffer.
30 @param Increment Character increment in Buffer.
31
32 @return Number of characters printed.
33
34 **/
35 CHAR8 *
36 BasePrintLibFillBuffer (
37 CHAR8 *Buffer,
38 INTN Length,
39 UINTN Character,
40 INTN Increment
41 )
42 {
43 INTN Index;
44
45 for (Index = 0; Index < Length; Index++) {
46 *Buffer = (CHAR8) Character;
47 *(Buffer + 1) = (CHAR8) (Character >> 8);
48 Buffer += Increment;
49 }
50 return Buffer;
51 }
52
53 /**
54 Internal function that convert a decimal number to a string in Buffer.
55
56 Print worker function that convert a decimal number to a string in Buffer.
57
58 @param Buffer Location to place the Unicode or ASCII string of Value.
59 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.
60 @param Radix Radix of the value
61
62 @return Number of characters printed.
63
64 **/
65 UINTN
66 EFIAPI
67 BasePrintLibValueToString (
68 IN OUT CHAR8 *Buffer,
69 IN INT64 Value,
70 IN UINTN Radix
71 )
72 {
73 UINTN Digits;
74 UINT32 Remainder;
75
76 //
77 // Loop to convert one digit at a time in reverse order
78 //
79 *(Buffer++) = 0;
80 Digits = 0;
81 do {
82 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
83 *(Buffer++) = mHexStr[Remainder];
84 Digits++;
85 } while (Value != 0);
86 return Digits;
87 }
88
89 /**
90 Internal function that converts a decimal value to a Null-terminated string.
91
92 Converts the decimal number specified by Value to a Null-terminated
93 string specified by Buffer containing at most Width characters.
94 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
95 The total number of characters placed in Buffer is returned.
96 If the conversion contains more than Width characters, then only the first
97 Width characters are returned, and the total number of characters
98 required to perform the conversion is returned.
99 Additional conversion parameters are specified in Flags.
100 The Flags bit LEFT_JUSTIFY is always ignored.
101 All conversions are left justified in Buffer.
102 If Width is 0, PREFIX_ZERO is ignored in Flags.
103 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
104 are inserted every 3rd digit starting from the right.
105 If Value is < 0, then the fist character in Buffer is a '-'.
106 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
107 then Buffer is padded with '0' characters so the combination of the optional '-'
108 sign character, '0' characters, digit characters for Value, and the Null-terminator
109 add up to Width characters.
110
111 If Buffer is NULL, then ASSERT().
112 If unsupported bits are set in Flags, then ASSERT().
113 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
114
115 @param Buffer Pointer to the output buffer for the produced Null-terminated
116 string.
117 @param Flags The bitmask of flags that specify left justification, zero pad,
118 and commas.
119 @param Value The 64-bit signed value to convert to a string.
120 @param Width The maximum number of characters to place in Buffer.
121 @param Increment Character increment in Buffer.
122
123 @return Total number of characters required to perform the conversion.
124
125 **/
126 UINTN
127 BasePrintLibConvertValueToString (
128 IN OUT CHAR8 *Buffer,
129 IN UINTN Flags,
130 IN INT64 Value,
131 IN UINTN Width,
132 IN UINTN Increment
133 )
134 {
135 CHAR8 *OriginalBuffer;
136 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
137 UINTN Count;
138 UINTN Digits;
139 UINTN Index;
140
141 ASSERT (Buffer != NULL);
142 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
143 //
144 // Make sure Flags can only contain supported bits.
145 //
146 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO)) == 0);
147
148 OriginalBuffer = Buffer;
149
150 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
151 Flags &= (~PREFIX_ZERO);
152 }
153
154 if (Width == 0) {
155 Width = MAXIMUM_VALUE_CHARACTERS - 1;
156 }
157
158 if (Value < 0) {
159 Value = -Value;
160 Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);
161 }
162
163 Count = BasePrintLibValueToString (ValueBuffer, Value, 10);
164
165 if ((Flags & PREFIX_ZERO) != 0) {
166 Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);
167 }
168
169 Digits = 3 - (Count % 3);
170 for (Index = 0; Index < Count; Index++) {
171 Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);
172 if ((Flags & COMMA_TYPE) != 0) {
173 Digits++;
174 if (Digits == 3) {
175 Digits = 0;
176 if ((Index + 1) < Count) {
177 Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);
178 }
179 }
180 }
181 }
182
183 Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment);
184
185 return ((Buffer - OriginalBuffer) / Increment);
186 }