-
Notifications
You must be signed in to change notification settings - Fork 10.9k
/
write_int_converter.h
69 lines (59 loc) · 2.16 KB
/
write_int_converter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//===-- Write integer 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
#include <inttypes.h>
#include <stddef.h>
namespace LIBC_NAMESPACE {
namespace printf_core {
LIBC_INLINE int convert_write_int(Writer *writer,
const FormatSection &to_conv) {
#ifndef LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
// This is an additional check added by LLVM-libc.
if (to_conv.conv_val_ptr == nullptr)
return NULLPTR_WRITE_ERROR;
#endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
int written = writer->get_chars_written();
switch (to_conv.length_modifier) {
case LengthModifier::none:
*reinterpret_cast<int *>(to_conv.conv_val_ptr) = written;
break;
case LengthModifier::l:
*reinterpret_cast<long *>(to_conv.conv_val_ptr) = written;
break;
case LengthModifier::ll:
case LengthModifier::L:
*reinterpret_cast<long long *>(to_conv.conv_val_ptr) = written;
break;
case LengthModifier::h:
*reinterpret_cast<short *>(to_conv.conv_val_ptr) =
static_cast<short>(written);
break;
case LengthModifier::hh:
*reinterpret_cast<signed char *>(to_conv.conv_val_ptr) =
static_cast<signed char>(written);
break;
case LengthModifier::z:
*reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written;
break;
case LengthModifier::t:
*reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written;
break;
case LengthModifier::j:
case LengthModifier::w:
case LengthModifier::wf:
*reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written;
break;
}
return WRITE_OK;
}
} // namespace printf_core
} // namespace LIBC_NAMESPACE
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H