VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: strformatnum.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - String Formatter, Single Numbers.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_STRING
42#include <iprt/string.h>
43#include "internal/iprt.h"
44
45#include <iprt/errcore.h>
46#include "internal/string.h"
47
48
49RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
50 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
51{
52 fFlags &= ~RTSTR_F_BIT_MASK;
53 fFlags |= RTSTR_F_8BIT;
54
55 ssize_t cchRet;
56 if (cbBuf >= 64)
57 cchRet = RTStrFormatNumber(pszBuf, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
58 else
59 {
60 char szTmp[64];
61 cchRet = RTStrFormatNumber(szTmp, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
62 if ((size_t)cchRet < cbBuf)
63 memcpy(pszBuf, szTmp, cchRet + 1);
64 else
65 {
66 if (cbBuf)
67 {
68 memcpy(pszBuf, szTmp, cbBuf - 1);
69 pszBuf[cbBuf - 1] = '\0';
70 }
71 cchRet = VERR_BUFFER_OVERFLOW;
72 }
73 }
74 return cchRet;
75}
76
77
78RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
79 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
80{
81 fFlags &= ~RTSTR_F_BIT_MASK;
82 fFlags |= RTSTR_F_16BIT;
83
84 ssize_t cchRet;
85 if (cbBuf >= 64)
86 cchRet = RTStrFormatNumber(pszBuf, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
87 else
88 {
89 char szTmp[64];
90 cchRet = RTStrFormatNumber(szTmp, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
91 if ((size_t)cchRet < cbBuf)
92 memcpy(pszBuf, szTmp, cchRet + 1);
93 else
94 {
95 if (cbBuf)
96 {
97 memcpy(pszBuf, szTmp, cbBuf - 1);
98 pszBuf[cbBuf - 1] = '\0';
99 }
100 cchRet = VERR_BUFFER_OVERFLOW;
101 }
102 }
103 return cchRet;
104}
105
106
107RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
108 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
109{
110 fFlags &= ~RTSTR_F_BIT_MASK;
111 fFlags |= RTSTR_F_32BIT;
112
113 ssize_t cchRet;
114 if (cbBuf >= 64)
115 cchRet = RTStrFormatNumber(pszBuf, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
116 else
117 {
118 char szTmp[64];
119 cchRet = RTStrFormatNumber(szTmp, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
120 if ((size_t)cchRet < cbBuf)
121 memcpy(pszBuf, szTmp, cchRet + 1);
122 else
123 {
124 if (cbBuf)
125 {
126 memcpy(pszBuf, szTmp, cbBuf - 1);
127 pszBuf[cbBuf - 1] = '\0';
128 }
129 cchRet = VERR_BUFFER_OVERFLOW;
130 }
131 }
132 return cchRet;
133}
134
135
136RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
137 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
138{
139 fFlags &= ~RTSTR_F_BIT_MASK;
140 fFlags |= RTSTR_F_64BIT;
141
142 ssize_t cchRet;
143 if (cbBuf >= 64)
144 cchRet = RTStrFormatNumber(pszBuf, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
145 else
146 {
147 char szTmp[64];
148 cchRet = RTStrFormatNumber(szTmp, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
149 if ((size_t)cchRet < cbBuf)
150 memcpy(pszBuf, szTmp, cchRet + 1);
151 else
152 {
153 if (cbBuf)
154 {
155 memcpy(pszBuf, szTmp, cbBuf - 1);
156 pszBuf[cbBuf - 1] = '\0';
157 }
158 cchRet = VERR_BUFFER_OVERFLOW;
159 }
160 }
161 return cchRet;
162}
163
164
165RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128, unsigned int uiBase,
166 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
167{
168 NOREF(cchWidth); NOREF(cchPrecision);
169 if (uiBase != 16)
170 fFlags |= RTSTR_F_SPECIAL;
171 fFlags &= ~RTSTR_F_BIT_MASK;
172
173 char szTmp[64+32+32+32];
174 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
175 size_t cchResult = RTStrFormatNumber(pszTmp, pu128->QWords.qw1, 16, 0, 0, fFlags | RTSTR_F_64BIT);
176 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu128->QWords.qw0, 16, 8, 0,
177 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
178 if (pszTmp == pszBuf)
179 return cchResult;
180 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
181 if (RT_SUCCESS(rc))
182 return cchResult;
183 return rc;
184}
185
186
187RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256, unsigned int uiBase,
188 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
189{
190 NOREF(cchWidth); NOREF(cchPrecision);
191 if (uiBase != 16)
192 fFlags |= RTSTR_F_SPECIAL;
193 fFlags &= ~RTSTR_F_BIT_MASK;
194
195 char szTmp[64+32+32+32];
196 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
197 size_t cchResult = RTStrFormatNumber(pszTmp, pu256->QWords.qw3, 16, 0, 0, fFlags | RTSTR_F_64BIT);
198 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw2, 16, 8, 0,
199 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
200 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw1, 16, 8, 0,
201 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
202 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu256->QWords.qw0, 16, 8, 0,
203 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
204 if (pszTmp == pszBuf)
205 return cchResult;
206 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
207 if (RT_SUCCESS(rc))
208 return cchResult;
209 return rc;
210}
211
212
213RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512, unsigned int uiBase,
214 signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
215{
216 NOREF(cchWidth); NOREF(cchPrecision);
217 if (uiBase != 16)
218 fFlags |= RTSTR_F_SPECIAL;
219 fFlags &= ~RTSTR_F_BIT_MASK;
220
221 char szTmp[64+32+32+32 + 32+32+32+32];
222 char *pszTmp = cbBuf >= sizeof(szTmp) ? pszBuf : szTmp;
223 size_t cchResult = RTStrFormatNumber(pszTmp, pu512->QWords.qw7, 16, 0, 0, fFlags | RTSTR_F_64BIT);
224 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw6, 16, 8, 0,
225 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
226 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw5, 16, 8, 0,
227 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
228 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw4, 16, 8, 0,
229 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
230 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw3, 16, 8, 0,
231 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
232 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw2, 16, 8, 0,
233 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
234 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw1, 16, 8, 0,
235 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
236 cchResult += RTStrFormatNumber(&pszTmp[cchResult], pu512->QWords.qw0, 16, 8, 0,
237 (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
238 if (pszTmp == pszBuf)
239 return cchResult;
240 int rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
241 if (RT_SUCCESS(rc))
242 return cchResult;
243 return rc;
244}
245
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use