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

Function formatWith

Sources/Shared/Base/Format.cpp:421–533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

419
420 template<class Writer, class FormattedWriter, class Formatter>
421 void formatWith(const Writer writer, const FormattedWriter formattedWriter, Containers::StringView format, Containers::ArrayView<Formatter> formatters) {
422 bool inPlaceholder = false;
423 std::size_t placeholderOffset = 0;
424 std::size_t formatterToGo = 0;
425 std::int32_t placeholderIndex = -1;
426 FormatContext context{-1, FormatType::Unspecified};
427 for (std::size_t formatOffset = 0; formatOffset != format.size(); ) {
428 // Placeholder begin (or escaped {)
429 if (format[formatOffset] == '{') {
430 if (formatOffset + 1 < format.size() && format[formatOffset + 1] == '{') {
431 writer(format.slice(formatOffset, formatOffset + 1));
432 formatOffset += 2;
433 continue;
434 }
435
436 DEATH_DEBUG_ASSERT(!inPlaceholder);
437 inPlaceholder = true;
438 placeholderOffset = formatOffset;
439 placeholderIndex = -1;
440 context.Precision = -1;
441 context.Type = FormatType::Unspecified;
442
443 formatOffset++;
444 continue;
445 }
446
447 // Placeholder end (or escaped })
448 if (format[formatOffset] == '}') {
449 if (!inPlaceholder && formatOffset + 1 < format.size() && format[formatOffset + 1] == '}') {
450 writer(format.slice(formatOffset, formatOffset + 1));
451 formatOffset += 2;
452 continue;
453 }
454
455 DEATH_ASSERT(inPlaceholder, "Mismatched }", );
456 inPlaceholder = false;
457
458 // If the placeholder was numbered, use that number, otherwise just use the formatter that's next
459 if (placeholderIndex != -1) formatterToGo = placeholderIndex;
460
461 if (formatterToGo < formatters.size()) {
462 // Formatter index is in bounds, write
463 formattedWriter(formatters[formatterToGo], context);
464 } else {
465 // Otherwise just verbatim copy the placeholder (including })
466 writer(format.slice(placeholderOffset, formatOffset + 1));
467 }
468
469 // Next time we see an unnumbered placeholder, take the next formatter
470 formatterToGo++;
471
472 formatOffset++;
473 continue;
474 }
475
476 // Placeholder contents
477 if (inPlaceholder) {
478 // Placeholder index

Callers 1

formatFormattersFunction · 0.85

Calls 4

DEATH_ASSERTFunction · 0.85
parseNumberFunction · 0.85
sizeMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected