]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePrintLib/PrintLibInternal.c
remove unnecessary comments introduced by tools from MdePkg. The regular express...
[mirror_edk2.git] / MdePkg / Library / BasePrintLib / PrintLibInternal.c
1 /** @file
2 Print Library internal worker functions.
3
4 Copyright (c) 2006 - 2008, 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 **/
14
15
16
17
18 #include "PrintLibInternal.h"
19
20 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
21
22
23 /**
24 Internal function that places the character into the Buffer.
25
26 Internal function that places ASCII or Unicode character into the Buffer.
27
28 @param Buffer Buffer to place the Unicode or ASCII string.
29 @param EndBuffer The end of the input Buffer. No characters will be
30 placed after that.
31 @param Length Count of character to be placed into Buffer.
32 @param Character Character to be placed into Buffer.
33 @param Increment Character increment in Buffer.
34
35 @return Buffer Buffer filled with the input Character.
36
37 **/
38 CHAR8 *
39 BasePrintLibFillBuffer (
40 OUT CHAR8 *Buffer,
41 IN CHAR8 *EndBuffer,
42 IN INTN Length,
43 IN UINTN Character,
44 IN INTN Increment
45 )
46 {
47 INTN Index;
48
49 for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
50 *Buffer = (CHAR8) Character;
51 *(Buffer + 1) = (CHAR8) (Character >> 8);
52 Buffer += Increment;
53 }
54 return Buffer;
55 }
56
57 /**
58 Internal function that convert a decimal number to a string in Buffer.
59
60 Print worker function that convert a decimal number to a string in Buffer.
61
62 @param Buffer Location to place the Unicode or ASCII string of Value.
63 @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.
64 @param Radix Radix of the value
65
66 @return Number of characters printed.
67
68 **/
69 UINTN
70 BasePrintLibValueToString (
71 IN OUT CHAR8 *Buffer,
72 IN INT64 Value,
73 IN UINTN Radix
74 )
75 {
76 UINTN Digits;
77 UINT32 Remainder;
78
79 //
80 // Loop to convert one digit at a time in reverse order
81 //
82 *(Buffer++) = 0;
83 Digits = 0;
84 do {
85 Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);
86 *(Buffer++) = mHexStr[Remainder];
87 Digits++;
88 } while (Value != 0);
89
90 //
91 // the length of Buffer string converted from Value
92 //
93 return Digits;
94 }
95
96 /**
97 Internal function that converts a decimal value to a Null-terminated string.
98
99 Converts the decimal number specified by Value to a Null-terminated
100 string specified by Buffer containing at most Width characters.
101 If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
102 The number of characters in Buffer is returned not including the Null-terminator.
103 If the conversion contains more than Width characters, then only the first
104 Width characters are returned, and the total number of characters
105 required to perform the conversion is returned.
106 Additional conversion parameters are specified in Flags.
107 The Flags bit LEFT_JUSTIFY is always ignored.
108 All conversions are left justified in Buffer.
109 If Width is 0, PREFIX_ZERO is ignored in Flags.
110 If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas
111 are inserted every 3rd digit starting from the right.
112 If HEX_RADIX is set in Flags, then the output buffer will be formatted in hexadecimal format.
113 If Value is < 0 and HEX_RADIX is not set in Flags, then the fist character in Buffer is a '-'.
114 If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored,
115 then Buffer is padded with '0' characters so the combination of the optional '-'
116 sign character, '0' characters, digit characters for Value, and the Null-terminator
117 add up to Width characters.
118 If both COMMA_TYPE and HEX_RADIX are set in Flags, then ASSERT().
119
120 If Buffer is NULL, then ASSERT().
121 If unsupported bits are set in Flags, then ASSERT().
122 If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()
123
124 @param Buffer Pointer to the output buffer for the produced Null-terminated
125 string.
126 @param Flags The bitmask of flags that specify left justification, zero pad,
127 and commas.
128 @param Value The 64-bit signed value to convert to a string.
129 @param Width The maximum number of characters to place in Buffer, not including
130 the Null-terminator.
131 @param Increment Character increment in Buffer.
132
133 @return The number of characters in Buffer not including the Null-terminator.
134
135 **/
136 UINTN
137 BasePrintLibConvertValueToString (
138 IN OUT CHAR8 *Buffer,
139 IN UINTN Flags,
140 IN INT64 Value,
141 IN UINTN Width,
142 IN UINTN Increment
143 )
144 {
145 CHAR8 *OriginalBuffer;
146 CHAR8 *EndBuffer;
147 CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
148 UINTN Count;
149 UINTN Digits;
150 UINTN Index;
151 UINTN Radix;
152
153 //
154 // Make sure Buffer is not NULL and Width < MAXIMUM
155 //
156 ASSERT (Buffer != NULL);
157 ASSERT (Width < MAXIMUM_VALUE_CHARACTERS);
158 //
159 // Make sure Flags can only contain supported bits.
160 //
161 ASSERT ((Flags & ~(LEFT_JUSTIFY | COMMA_TYPE | PREFIX_ZERO | RADIX_HEX)) == 0);
162
163 //
164 // If both COMMA_TYPE and HEX_RADIX are set, then ASSERT ()
165 //
166 ASSERT (((Flags & COMMA_TYPE) != 0 && (Flags & RADIX_HEX) != 0) == FALSE);
167
168 OriginalBuffer = Buffer;
169
170 //
171 // Width is 0 or COMMA_TYPE is set, PREFIX_ZERO is ignored.
172 //
173 if (Width == 0 || (Flags & COMMA_TYPE) != 0) {
174 Flags &= (~PREFIX_ZERO);
175 }
176 //
177 // If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.
178 //
179 if (Width == 0) {
180 Width = MAXIMUM_VALUE_CHARACTERS - 1;
181 }
182 //
183 // Set the tag for the end of the input Buffer.
184 //
185 EndBuffer = Buffer + Width * Increment;
186
187 //
188 // Convert decimal negative
189 //
190 if ((Value < 0) && ((Flags & RADIX_HEX) == 0)) {
191 Value = -Value;
192 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
193 Width--;
194 }
195
196 //
197 // Count the length of the value string.
198 //
199 Radix = ((Flags & RADIX_HEX) == 0)? 10 : 16;
200 Count = BasePrintLibValueToString (ValueBuffer, Value, Radix);
201
202 //
203 // Append Zero
204 //
205 if ((Flags & PREFIX_ZERO) != 0) {
206 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
207 }
208
209 //
210 // Print Comma type for every 3 characters
211 //
212 Digits = Count % 3;
213 if (Digits != 0) {
214 Digits = 3 - Digits;
215 }
216 for (Index = 0; Index < Count; Index++) {
217 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);
218 if ((Flags & COMMA_TYPE) != 0) {
219 Digits++;
220 if (Digits == 3) {
221 Digits = 0;
222 if ((Index + 1) < Count) {
223 Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
224 }
225 }
226 }
227 }
228
229 //
230 // Print Null-terminator
231 //
232 BasePrintLibFillBuffer (Buffer, EndBuffer + Increment, 1, 0, Increment);
233
234 return ((Buffer - OriginalBuffer) / Increment);
235 }
236