Skip to content

[libc] Add support for C23 binary notation in sprintf #80820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/src/stdio/printf_core/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ int convert(Writer *writer, const FormatSection &to_conv) {
case 's':
return convert_string(writer, to_conv);
case 'd':
case 'b':
case 'B':
return convert_decimal_binary(writer, to_conv);
case 'i':
case 'u':
case 'o':
Expand Down
3 changes: 3 additions & 0 deletions libc/src/stdio/printf_core/converter_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
// defines convert_int
#include "src/stdio/printf_core/int_converter.h"

// defines convert_decimal_binary
#include "src/stdio/printf_core/decimal_binary_converter.h"

#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
// defines convert_float_decimal
// defines convert_float_dec_exp
Expand Down
8 changes: 8 additions & 0 deletions libc/src/stdio/printf_core/decimal_binary_converter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//===-- Decimal Binary Converter for printf -----------------------------*- C++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ using HexFmt = IntegerToString<uintmax_t, radix::Hex>;
using HexFmtUppercase = IntegerToString<uintmax_t, radix::Hex::Uppercase>;
using OctFmt = IntegerToString<uintmax_t, radix::Oct>;
using DecFmt = IntegerToString<uintmax_t>;
using BinFmt = IntegerToString<Bin>

LIBC_INLINE constexpr size_t num_buf_size() {
LIBC_INLINE constexpr size_t num_buf_size() {
constexpr auto max = [](size_t a, size_t b) -> size_t {
return (a < b) ? b : a;
};
Expand Down
8 changes: 8 additions & 0 deletions libc/src/stdio/printf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ template <typename ArgProvider> class Parser {
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, int, conv_index);
break;
case ('d'):
case ('b'):
case ('B'):
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, char *, conv_index);
break;
case ('i'):
case ('o'):
case ('x'):
Expand Down Expand Up @@ -479,6 +483,10 @@ template <typename ArgProvider> class Parser {
conv_size = type_desc_from_type<int>();
break;
case ('d'):
case ('b'):
case ('B'):
conv_size = type_desc_from_type<void *>();
break;
case ('i'):
case ('o'):
case ('x'):
Expand Down