VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strformatnum.cpp@ 72426

Last change on this file since 72426 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/* $Id: strformatnum.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - String Formatter, Single Numbers.
4 */
5
6/*
7 * Copyright (C) 2010-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_STRING
32#include <iprt/string.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include "internal/string.h"
37
38
39RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
40 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
41{
42 fFlags &= ~RTSTR_F_BIT_MASK;
43 fFlags |= RTSTR_F_8BIT;
44
45 ssize_t cchRet;
46 if (cbBuf >= 64)
47 cchRet = RTStrFormatNumber(pszBuf, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
48 else
49 {
50 char szTmp[64];
51 cchRet = RTStrFormatNumber(szTmp, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
52 if ((size_t)cchRet < cbBuf)
53 memcpy(pszBuf, szTmp, cchRet + 1);
54 else
55 {
56 if (cbBuf)
57 {
58 memcpy(pszBuf, szTmp, cbBuf - 1);
59 pszBuf[cbBuf - 1] = '\0';
60 }
61 cchRet = VERR_BUFFER_OVERFLOW;
62 }
63 }
64 return cchRet;
65}
66
67
68RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
69 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
70{
71 fFlags &= ~RTSTR_F_BIT_MASK;
72 fFlags |= RTSTR_F_16BIT;
73
74 ssize_t cchRet;
75 if (cbBuf >= 64)
76 cchRet = RTStrFormatNumber(pszBuf, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
77 else
78 {
79 char szTmp[64];
80 cchRet = RTStrFormatNumber(szTmp, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
81 if ((size_t)cchRet <= cbBuf)
82 memcpy(pszBuf, szTmp, cchRet + 1);
83 else
84 {
85 if (cbBuf)
86 {
87 memcpy(pszBuf, szTmp, cbBuf - 1);
88 pszBuf[cbBuf - 1] = '\0';
89 }
90 cchRet = VERR_BUFFER_OVERFLOW;
91 }
92 }
93 return cchRet;
94}
95
96
97RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
98 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
99{
100 fFlags &= ~RTSTR_F_BIT_MASK;
101 fFlags |= RTSTR_F_32BIT;
102
103 ssize_t cchRet;
104 if (cbBuf >= 64)
105 cchRet = RTStrFormatNumber(pszBuf, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
106 else
107 {
108 char szTmp[64];
109 cchRet = RTStrFormatNumber(szTmp, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
110 if ((size_t)cchRet <= cbBuf)
111 memcpy(pszBuf, szTmp, cchRet + 1);
112 else
113 {
114 if (cbBuf)
115 {
116 memcpy(pszBuf, szTmp, cbBuf - 1);
117 pszBuf[cbBuf - 1] = '\0';
118 }
119 cchRet = VERR_BUFFER_OVERFLOW;
120 }
121 }
122 return cchRet;
123}
124
125
126RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
127 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
128{
129 fFlags &= ~RTSTR_F_BIT_MASK;
130 fFlags |= RTSTR_F_64BIT;
131
132 ssize_t cchRet;
133 if (cbBuf >= 64)
134 cchRet = RTStrFormatNumber(pszBuf, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
135 else
136 {
137 char szTmp[64];
138 cchRet = RTStrFormatNumber(szTmp, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
139 if ((size_t)cchRet <= cbBuf)
140 memcpy(pszBuf, szTmp, cchRet + 1);
141 else
142 {
143 if (cbBuf)
144 {
145 memcpy(pszBuf, szTmp, cbBuf - 1);
146 pszBuf[cbBuf - 1] = '\0';
147 }
148 cchRet = VERR_BUFFER_OVERFLOW;
149 }
150 }
151 return cchRet;
152}
153
154
155RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128, unsigned int uiBase,
156 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
157{
158 NOREF(cchWidth); NOREF(cchPrecision);
159 if (uiBase != 16)
160 fFlags |= RTSTR_F_SPECIAL;
161 fFlags &= ~RTSTR_F_BIT_MASK;
162
163 char szTmp[64+32+32+32];
164 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
165 size_t cchResult = RTStrFormatNumber(pszTmp, pu128->QWords.qw1, 16, 0, 0, fFlags | RTSTR_F_64BIT);
166 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu128->QWords.qw0, 16, 8, 0,
167 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
168 if (pszTmp == pszBuf)
169 return cchResult;
170 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
171 if (RT_SUCCESS(rc))
172 return cchResult;
173 return rc;
174}
175
176
177RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256, unsigned int uiBase,
178 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
179{
180 NOREF(cchWidth); NOREF(cchPrecision);
181 if (uiBase != 16)
182 fFlags |= RTSTR_F_SPECIAL;
183 fFlags &= ~RTSTR_F_BIT_MASK;
184
185 char szTmp[64+32+32+32];
186 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
187 size_t cchResult = RTStrFormatNumber(pszTmp, pu256->QWords.qw3, 16, 0, 0, fFlags | RTSTR_F_64BIT);
188 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw2, 16, 8, 0,
189 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
190 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw1, 16, 8, 0,
191 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
192 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw0, 16, 8, 0,
193 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
194 if (pszTmp == pszBuf)
195 return cchResult;
196 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
197 if (RT_SUCCESS(rc))
198 return cchResult;
199 return rc;
200}
201
202
203RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512, unsigned int uiBase,
204 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
205{
206 NOREF(cchWidth); NOREF(cchPrecision);
207 if (uiBase != 16)
208 fFlags |= RTSTR_F_SPECIAL;
209 fFlags &= ~RTSTR_F_BIT_MASK;
210
211 char szTmp[64+32+32+32 + 32+32+32+32];
212 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
213 size_t cchResult = RTStrFormatNumber(pszTmp, pu512->QWords.qw7, 16, 0, 0, fFlags | RTSTR_F_64BIT);
214 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw6, 16, 8, 0,
215 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
216 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw5, 16, 8, 0,
217 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
218 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw4, 16, 8, 0,
219 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
220 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw3, 16, 8, 0,
221 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
222 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw2, 16, 8, 0,
223 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
224 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw1, 16, 8, 0,
225 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
226 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw0, 16, 8, 0,
227 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
228 if (pszTmp == pszBuf)
229 return cchResult;
230 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
231 if (RT_SUCCESS(rc))
232 return cchResult;
233 return rc;
234}
235
236
237RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
238 signed int cchPrecision, uint32_t fFlags)
239{
240 NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
241 char szTmp[160];
242
243 char *pszTmp = szTmp;
244 if (pr80Value->s.fSign)
245 *pszTmp++ = '-';
246 else
247 *pszTmp++ = '+';
248
249 if (pr80Value->s.uExponent == 0)
250 {
251 if ( !pr80Value->sj64.u63Fraction
252 && pr80Value->sj64.fInteger)
253 *pszTmp++ = '0';
254 /* else: Denormal, handled way below. */
255 }
256 else if (pr80Value->sj64.uExponent == UINT16_C(0x7fff))
257 {
258 /** @todo Figure out Pseudo inf/nan... */
259 if (pr80Value->sj64.fInteger)
260 *pszTmp++ = 'P';
261 if (pr80Value->sj64.u63Fraction == 0)
262 {
263 *pszTmp++ = 'I';
264 *pszTmp++ = 'n';
265 *pszTmp++ = 'f';
266 }
267 else
268 {
269 *pszTmp++ = 'N';
270 *pszTmp++ = 'a';
271 *pszTmp++ = 'N';
272 }
273 }
274 if (pszTmp != &szTmp[1])
275 *pszTmp = '\0';
276 else
277 {
278 *pszTmp++ = pr80Value->sj64.fInteger ? '1' : '0';
279 *pszTmp++ = 'm';
280 pszTmp += RTStrFormatNumber(pszTmp, pr80Value->sj64.u63Fraction, 16, 2+16, 0,
281 RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD | RTSTR_F_64BIT);
282
283 *pszTmp++ = 'e';
284 pszTmp += RTStrFormatNumber(pszTmp, (int32_t)pr80Value->sj64.uExponent - 16383, 10, 0, 0,
285 RTSTR_F_ZEROPAD | RTSTR_F_32BIT | RTSTR_F_VALSIGNED);
286 }
287
288 /*
289 * Copy out the result.
290 */
291 ssize_t cchRet = pszTmp - &szTmp[0];
292 if ((size_t)cchRet <= cbBuf)
293 memcpy(pszBuf, szTmp, cchRet + 1);
294 else
295 {
296 if (cbBuf)
297 {
298 memcpy(pszBuf, szTmp, cbBuf - 1);
299 pszBuf[cbBuf - 1] = '\0';
300 }
301 cchRet = VERR_BUFFER_OVERFLOW;
302 }
303 return cchRet;
304}
305
306
307RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
308 signed int cchPrecision, uint32_t fFlags)
309{
310 RTFLOAT80U2 r80ValueU2;
311 RT_ZERO(r80ValueU2);
312 r80ValueU2.s.fSign = pr80Value->s.fSign;
313 r80ValueU2.s.uExponent = pr80Value->s.uExponent;
314 r80ValueU2.s.u64Mantissa = pr80Value->s.u64Mantissa;
315 return RTStrFormatR80u2(pszBuf, cbBuf, &r80ValueU2, cchWidth, cchPrecision, fFlags);
316}
317
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette