-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
converter.cpp
99 lines (91 loc) · 2.74 KB
/
converter.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//===-- Format specifier converter implmentation for scanf -----*- 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
//
//===----------------------------------------------------------------------===//
#include "src/stdio/scanf_core/converter.h"
#include "src/__support/ctype_utils.h"
#include "src/stdio/scanf_core/core_structs.h"
#include "src/stdio/scanf_core/reader.h"
#include "src/stdio/scanf_core/int_converter.h"
#include "src/stdio/scanf_core/string_converter.h"
#include <stddef.h>
namespace __llvm_libc {
namespace scanf_core {
int convert(Reader *reader, const FormatSection &to_conv) {
int ret_val = 0;
switch (to_conv.conv_name) {
case '%':
return raw_match(reader, "%");
case 's':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_string(reader, to_conv);
case 'c':
case '[':
return convert_string(reader, to_conv);
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_int(reader, to_conv);
// #ifndef LLVM_LIBC_SCANF_DISABLE_FLOAT
// case 'f':
// case 'F':
// case 'e':
// case 'E':
// case 'a':
// case 'A':
// case 'g':
// case 'G':
// ret_val = raw_match(reader, " ");
// if (ret_val != READ_OK)
// return ret_val;
// return convert_float(reader, to_conv);
// #endif // LLVM_LIBC_SCANF_DISABLE_FLOAT
// #ifndef LLVM_LIBC_SCANF_DISABLE_WRITE_INT
// case 'n':
// return convert_write_int(reader, to_conv);
// #endif // LLVM_LIBC_SCANF_DISABLE_WRITE_INT
// case 'p':
// ret_val = raw_match(reader, " ");
// if (ret_val != READ_OK)
// return ret_val;
// return convert_pointer(reader, to_conv);
default:
return raw_match(reader, to_conv.raw_string);
}
return -1;
}
// raw_string is assumed to have a positive size.
int raw_match(Reader *reader, cpp::string_view raw_string) {
char cur_char = reader->getc();
int ret_val = READ_OK;
for (size_t i = 0; i < raw_string.size(); ++i) {
// Any space character matches any number of space characters.
if (internal::isspace(raw_string[i])) {
while (internal::isspace(cur_char)) {
cur_char = reader->getc();
}
} else {
if (raw_string[i] == cur_char) {
cur_char = reader->getc();
} else {
ret_val = MATCHING_FAILURE;
break;
}
}
}
reader->ungetc(cur_char);
return ret_val;
}
} // namespace scanf_core
} // namespace __llvm_libc