MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / formatNumber

Function formatNumber

Sources/Shared/Base/Format.cpp:227–315  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

225
226 template<typename T>
227 constexpr std::int32_t formatNumber(const Containers::MutableStringView& buffer, T value, FormatContext& context) {
228 std::int32_t precision = context.Precision;
229 if (precision == -1) {
230 precision = 1;
231 }
232
233 switch (context.Type) {
234 case FormatType::Character: {
235 if (1 <= buffer.size()) {
236 char* begin = buffer.data();
237 *begin = static_cast<char>(value);
238 }
239 return 1;
240 }
241 case FormatType::Unspecified:
242 case FormatType::Decimal: {
243 auto absValue = static_cast<uint32_or_64_t<T>>(value);
244 bool negative = isNegative(value);
245 if (negative) absValue = ~absValue + 1;
246
247 std::int32_t digitCount = countDigits(absValue);
248 std::int32_t size = (negative ? 1 : 0) + (digitCount < precision ? precision : digitCount);
249
250 if (size <= buffer.size()) {
251 char* begin = buffer.data();
252 if (negative) {
253 *begin++ = '-';
254 }
255 if (digitCount < precision) {
256 for (std::int32_t i = 0; i < precision - digitCount; i++) {
257 *begin++ = '0';
258 }
259 }
260 formatDecimal(begin, absValue, digitCount);
261 }
262
263 return size;
264 }
265 case FormatType::Octal: {
266 auto absValue = static_cast<uint32_or_64_t<T>>(value);
267
268 std::int32_t digitCount = countDigits<3>(absValue);
269 std::int32_t size = (digitCount < precision ? precision : digitCount + 1);
270
271 if (size <= buffer.size()) {
272 char* begin = buffer.data();
273 if (digitCount < precision) {
274 for (std::int32_t i = 0; i < precision - digitCount; i++) {
275 *begin++ = '0';
276 }
277 } else {
278 *begin++ = '0'; // '0' prefix for octal numbers
279 }
280 formatBase2e<3>(begin, absValue, digitCount, false);
281 }
282
283 return size;
284 }

Callers 1

formatMethod · 0.85

Calls 5

isNegativeFunction · 0.85
countDigitsFunction · 0.85
formatDecimalFunction · 0.85
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected