[all-commits] [llvm/llvm-project] 77ad77: [libc++][format] Improve string formatters

mordante via All-commits all-commits at lists.llvm.org
Tue Jun 21 22:40:51 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 77ad77c0710f1496f1caeeee14a2565f1ea81b06
  Author: Mark de Wever <koraq at xs4all.nl>
  Date:   2022-06-22 (Wed, 22 Jun 2022)

  Changed paths:
    M libcxx/include/CMakeLists.txt
    A libcxx/include/__format/formatter_output.h
    M libcxx/include/__format/formatter_string.h
    M libcxx/include/__format/parser_std_format_spec.h
    M libcxx/include/format
    M libcxx/include/module.modulemap.in
    M libcxx/test/libcxx/private_headers.verify.cpp
    R libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp

  Log Message:
  [libc++][format] Improve string formatters

This changes the implementation of the formatter. Instead of inheriting
from a specialized parser all formatters will use the same generic
parser. This reduces the binary size.

The new parser contains some additional fields only used in the chrono
formatting. Since this doesn't change the size of the parser the fields
are in the generic parser. The parser is designed to fit in 128-bit,
making it cheap to pass by value.

The new format function is a const member function. This isn't required
by the Standard yet, but it will be after LWG-3636 is accepted.
Additionally P2286 adds a formattable concept which requires the member
function to be const qualified in C++23. This paper is likely to be
accepted in the 2022 July plenary.

Depends on D121530

NOTE parts of the code now contains duplicates for the current and new parser.
The intention is to remove the duplication in followup patches. A general
overview of the final code is available in D124620. That review however lacks a
bit of polish.

Most of the new code is based on the same algorithms used in the current code.

The final version of this code reduces the binary size by 17 KB for this example

int main() {
    std::string_view sv{"hello world"};
    std::format("{}{}|{}{}{}{}{}{}|{}{}{}{}{}{}|{}{}{}|{}{}|{}", true, '*',
                (signed char)(42), (short)(42), (int)(42), (long)(42), (long long)(42), (__int128_t)(42),
                (unsigned char)(42), (unsigned short)(42), (unsigned int)(42), (unsigned long)(42),
                (unsigned long long)(42), (__uint128_t)(42),
                (float)(42), (double)(42), (long double)(42),
                "hello world", sv,
    std::wstring_view sv{L"hello world"};
    std::format(L"{}{}|{}{}{}{}{}{}|{}{}{}{}{}{}|{}{}{}|{}{}|{}", true, L'*',
                (signed char)(42), (short)(42), (int)(42), (long)(42), (long long)(42), (__int128_t)(42),
                (unsigned char)(42), (unsigned short)(42), (unsigned int)(42), (unsigned long)(42),
                (unsigned long long)(42), (__uint128_t)(42),
                (float)(42), (double)(42), (long double)(42),
                L"hello world", sv,


Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D125606

More information about the All-commits mailing list