Skip to content

Commit 82fa77d

Browse files
committed
[libc]: Implement strfrom* functions and utils
1 parent b43965a commit 82fa77d

File tree

8 files changed

+327
-1
lines changed

8 files changed

+327
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ set(TARGET_LIBC_ENTRYPOINTS
180180
libc.src.stdlib.qsort_r
181181
libc.src.stdlib.rand
182182
libc.src.stdlib.srand
183+
libc.src.stdlib.strfromf
183184
libc.src.stdlib.strtod
184185
libc.src.stdlib.strtof
185186
libc.src.stdlib.strtol

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,8 @@ def StdC : StandardSpec<"stdc"> {
946946
FunctionSpec<"rand", RetValSpec<IntType>, [ArgSpec<VoidType>]>,
947947
FunctionSpec<"srand", RetValSpec<VoidType>, [ArgSpec<UnsignedIntType>]>,
948948

949+
FunctionSpec<"strfromf", RetValSpec<IntType>, [ArgSpec<CharRestrictedPtr>, ArgSpec<SizeTType>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<FloatType>]>,
950+
949951
FunctionSpec<"strtof", RetValSpec<FloatType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,
950952
FunctionSpec<"strtod", RetValSpec<DoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,
951953
FunctionSpec<"strtold", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,

libc/src/stdlib/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ add_entrypoint_object(
5252
libc.config.linux.app_h
5353
)
5454

55+
add_entrypoint_object(
56+
strfromf
57+
SRCS
58+
strfromf.cpp
59+
HDRS
60+
strfromf.h
61+
DEPENDS
62+
.str_from_util
63+
)
64+
65+
add_header_library(
66+
str_from_util
67+
HDRS
68+
str_from_util.h
69+
DEPENDS
70+
libc.src.stdio.printf_core.converter_atlas
71+
libc.src.stdio.printf_core.core_structs
72+
libc.src.stdio.printf_core.writer
73+
libc.src.__support.str_to_integer
74+
)
75+
5576
add_entrypoint_object(
5677
strtof
5778
SRCS

libc/src/stdlib/str_from_util.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===-- Implementation header for strfromx() utilitites -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// According to the C23 standard, any input character sequences except a
10+
// precision specifier and the usual floating point formats, namely
11+
// %{a,A,e,E,f,F,g,G}, are not allowed and any code that does otherwise results
12+
// in undefined behaviour; which in this case is that the buffer string is
13+
// simply populated with the format string. The case of the value of buffer size
14+
// being 0 or the input being NULL should be handled in the calling function
15+
// (strfromf, strfromd, strfroml) itself.
16+
17+
#ifndef LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H
18+
#define LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H
19+
20+
#include "src/__support/str_to_integer.h"
21+
#include "src/stdio/printf_core/converter_atlas.h"
22+
#include "src/stdio/printf_core/core_structs.h"
23+
#include "src/stdio/printf_core/writer.h"
24+
25+
#include <stddef.h>
26+
27+
namespace LIBC_NAMESPACE::internal {
28+
29+
template <typename T> struct type_of {
30+
using type = T;
31+
};
32+
33+
template <> struct type_of<float> {
34+
using type = fputil::FPBits<float>::StorageType;
35+
};
36+
template <> struct type_of<double> {
37+
using type = fputil::FPBits<double>::StorageType;
38+
};
39+
template <> struct type_of<long double> {
40+
using type = fputil::FPBits<long double>::StorageType;
41+
};
42+
43+
template <typename T> using type_of_v = typename type_of<T>::type;
44+
45+
template <typename T>
46+
printf_core::FormatSection parse_format_string(const char *__restrict format,
47+
T fp) {
48+
printf_core::FormatSection section;
49+
size_t cur_pos = 0;
50+
51+
if (format[cur_pos] == '%') {
52+
section.has_conv = true;
53+
++cur_pos;
54+
55+
// handle precision
56+
section.precision = -1;
57+
if (format[cur_pos] == '.') {
58+
++cur_pos;
59+
section.precision = 0;
60+
61+
// The standard does not allow the '*' (asterisk) operator for strfromx()
62+
// functions
63+
if (internal::isdigit(format[cur_pos])) {
64+
auto result = internal::strtointeger<int>(format + cur_pos, 10);
65+
section.precision += result.value;
66+
cur_pos += result.parsed_len;
67+
}
68+
}
69+
70+
section.conv_name = format[cur_pos];
71+
switch (format[cur_pos]) {
72+
case '%':
73+
section.has_conv = true;
74+
break;
75+
case 'a':
76+
case 'A':
77+
case 'e':
78+
case 'E':
79+
case 'f':
80+
case 'F':
81+
case 'g':
82+
case 'G':
83+
section.conv_val_raw = cpp::bit_cast<type_of_v<T>>(fp);
84+
break;
85+
default:
86+
// error out, invalid format specifier
87+
section.has_conv = false;
88+
while (format[cur_pos] != '\0')
89+
++cur_pos;
90+
break;
91+
}
92+
93+
if (format[cur_pos] != '\0')
94+
++cur_pos;
95+
} else {
96+
section.has_conv = false;
97+
// We are looking for exactly one section, so no more '%'
98+
while (format[cur_pos] != '\0')
99+
++cur_pos;
100+
}
101+
102+
section.raw_string = {format, cur_pos};
103+
return section;
104+
}
105+
106+
int convert(const printf_core::FormatSection &section,
107+
printf_core::Writer *writer) {
108+
if (!section.has_conv)
109+
return writer->write(section.raw_string);
110+
111+
switch (section.conv_name) {
112+
case '%':
113+
return writer->write("%");
114+
case 'f':
115+
case 'F':
116+
return convert_float_decimal(writer, section);
117+
case 'e':
118+
case 'E':
119+
return convert_float_dec_exp(writer, section);
120+
case 'a':
121+
case 'A':
122+
return convert_float_hex_exp(writer, section);
123+
case 'g':
124+
case 'G':
125+
return convert_float_dec_auto(writer, section);
126+
default:
127+
return writer->write(section.raw_string);
128+
}
129+
return -1;
130+
}
131+
132+
} // namespace LIBC_NAMESPACE::internal
133+
134+
#endif // LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H

libc/src/stdlib/strfromf.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- Implementation of strfromf ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdlib/strfromf.h"
10+
#include "src/stdlib/str_from_util.h"
11+
12+
#include <stdarg.h>
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, strfromf,
18+
(char *__restrict s, size_t n, const char *__restrict format,
19+
float fp)) {
20+
21+
if (!s)
22+
return 0;
23+
24+
printf_core::FormatSection section =
25+
internal::parse_format_string(format, (double)fp);
26+
printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));
27+
printf_core::Writer writer(&wb);
28+
29+
int result = 0;
30+
if (section.has_conv)
31+
result = internal::convert(section, &writer);
32+
else
33+
result = writer.write(section.raw_string);
34+
35+
if (result < 0)
36+
return result;
37+
38+
if (n > 0)
39+
wb.buff[wb.buff_cur] = '\0';
40+
41+
return writer.get_chars_written();
42+
}
43+
44+
} // namespace LIBC_NAMESPACE

libc/src/stdlib/strfromf.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for strfromf ------------------------*- C++--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDLIB_STRFROMF_H
10+
#define LLVM_LIBC_SRC_STDLIB_STRFROMF_H
11+
12+
#include <stddef.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int strfromf(char *__restrict s, size_t n, const char *__restrict format,
17+
float fp);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_STDLIB_STRTOF_H

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ add_libc_test(
168168
.strtol_test_support
169169
)
170170

171+
add_libc_test(
172+
strfromf_test
173+
SUITE
174+
libc-stdlib-tests
175+
SRCS
176+
strfromf_test.cpp
177+
DEPENDS
178+
libc.src.stdlib.strfromf
179+
)
180+
171181
add_libc_test(
172182
abs_test
173183
SUITE
@@ -259,7 +269,6 @@ add_libc_test(
259269
libc.src.stdlib.qsort
260270
)
261271

262-
263272
add_libc_test(
264273
qsort_r_test
265274
SUITE
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===-- Unittests for strfromf --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdlib/strfromf.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
TEST(LlvmLibcStrfromfTest, DecimalFloatFormat) {
13+
char buff[100];
14+
int written;
15+
16+
written = LIBC_NAMESPACE::strfromf(buff, 16, "%f", 1.0);
17+
EXPECT_EQ(written, 8);
18+
ASSERT_STREQ(buff, "1.000000");
19+
20+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%f", 1234567890);
21+
EXPECT_EQ(written, 17);
22+
ASSERT_STREQ(buff, "1234567936.000000");
23+
24+
written = LIBC_NAMESPACE::strfromf(buff, 5, "%f", "1234567890.0");
25+
EXPECT_EQ(written, 17);
26+
ASSERT_STREQ(buff, "1234");
27+
28+
written = LIBC_NAMESPACE::strfromf(buff, 67, "%.3f", 1.0);
29+
EXPECT_EQ(written, 5);
30+
ASSERT_STREQ(buff, "1.000");
31+
32+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%1f", 1234567890.0);
33+
EXPECT_EQ(written, 3);
34+
ASSERT_STREQ(buff, "%1f");
35+
36+
written = LIBC_NAMESPACE::strfromf(nullptr, 0, "%f", 1.0);
37+
EXPECT_EQ(written, 0);
38+
}
39+
40+
TEST(LlvmLibcStrfromfTest, HexExpFloatFormat) {
41+
char buff[100];
42+
int written;
43+
44+
written = LIBC_NAMESPACE::strfromf(buff, 0, "%a", 1234567890.0);
45+
EXPECT_EQ(written, 0);
46+
47+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%a", 1234567890.0);
48+
EXPECT_EQ(written, 14);
49+
ASSERT_STREQ(buff, "0x1.26580cp+30");
50+
51+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%A", 1234567890.0);
52+
EXPECT_EQ(written, 14);
53+
ASSERT_STREQ(buff, "0X1.26580CP+30");
54+
}
55+
56+
TEST(LlvmLibcStrfromfTest, DecimalExpFloatFormat) {
57+
char buff[100];
58+
int written;
59+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9e", 1234567890.0);
60+
EXPECT_EQ(written, 15);
61+
ASSERT_STREQ(buff, "1.234567936e+09");
62+
63+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9E", 1234567890.0);
64+
EXPECT_EQ(written, 15);
65+
ASSERT_STREQ(buff, "1.234567936E+09");
66+
}
67+
68+
TEST(LlvmLibcStrfromfTest, AutoDecimalFloatFormat) {
69+
char buff[100];
70+
int written;
71+
72+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9g", 1234567890.0);
73+
EXPECT_EQ(written, 15);
74+
ASSERT_STREQ(buff, "1.23456794e+09");
75+
76+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9G", 1234567890.0);
77+
EXPECT_EQ(written, 15);
78+
ASSERT_STREQ(buff, "1.23456794E+09");
79+
}
80+
81+
TEST(LlvmLibcStrfromfTest, ImproperFormatString) {
82+
83+
char buff[100];
84+
int retval;
85+
retval = LIBC_NAMESPACE::strfromf(
86+
buff, 37, "A simple string with no conversions.", 1.0);
87+
EXPECT_EQ(retval, 36);
88+
ASSERT_STREQ(buff, "A simple string with no conversions.");
89+
90+
retval = LIBC_NAMESPACE::strfromf(
91+
buff, 37, "%A simple string with one conversion, should overwrite.", 1.0);
92+
EXPECT_EQ(retval, 6);
93+
ASSERT_STREQ(buff, "0X1P+0");
94+
}

0 commit comments

Comments
 (0)