Skip to content

Commit 2449a30

Browse files
committed
[libc] Add support for C23 binary notation in sprintf
Resolves Issue #80727
1 parent 38476b0 commit 2449a30

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

libc/src/stdio/printf_core/converter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ int convert(Writer *writer, const FormatSection &to_conv) {
5353
case 's':
5454
return convert_string(writer, to_conv);
5555
case 'd':
56+
case 'b':
57+
case 'B':
58+
return convert_decimal_binary(writer, to_conv);
5659
case 'i':
5760
case 'u':
5861
case 'o':

libc/src/stdio/printf_core/converter_atlas.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
// defines convert_int
2323
#include "src/stdio/printf_core/int_converter.h"
2424

25+
// defines convert_decimal_binary
26+
#include "src/stdio/printf_core/decimal_binary_converter.h"
27+
2528
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
2629
// defines convert_float_decimal
2730
// defines convert_float_dec_exp
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//===-- Decimal Binary Converter for printf -----------------------------*- C++
2+
//-*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//

libc/src/stdio/printf_core/int_converter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ using HexFmt = IntegerToString<uintmax_t, radix::Hex>;
3333
using HexFmtUppercase = IntegerToString<uintmax_t, radix::Hex::Uppercase>;
3434
using OctFmt = IntegerToString<uintmax_t, radix::Oct>;
3535
using DecFmt = IntegerToString<uintmax_t>;
36+
using BinFmt = IntegerToString<Bin>
3637

37-
LIBC_INLINE constexpr size_t num_buf_size() {
38+
LIBC_INLINE constexpr size_t num_buf_size() {
3839
constexpr auto max = [](size_t a, size_t b) -> size_t {
3940
return (a < b) ? b : a;
4041
};

libc/src/stdio/printf_core/parser.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ template <typename ArgProvider> class Parser {
154154
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, int, conv_index);
155155
break;
156156
case ('d'):
157+
case ('b'):
158+
case ('B'):
159+
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, char *, conv_index);
160+
break;
157161
case ('i'):
158162
case ('o'):
159163
case ('x'):
@@ -479,6 +483,10 @@ template <typename ArgProvider> class Parser {
479483
conv_size = type_desc_from_type<int>();
480484
break;
481485
case ('d'):
486+
case ('b'):
487+
case ('B'):
488+
conv_size = type_desc_from_type<void *>();
489+
break;
482490
case ('i'):
483491
case ('o'):
484492
case ('x'):

0 commit comments

Comments
 (0)