[NFC][libc] Use span instead of ArrayRef

This commit is contained in:
Guillaume Chatelet 2022-08-22 08:09:23 +00:00
parent f3acb54c1b
commit e7b250b8a6
13 changed files with 36 additions and 40 deletions

View File

@ -37,6 +37,7 @@ add_header_library(
HDRS HDRS
integer_to_string.h integer_to_string.h
DEPENDS DEPENDS
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view libc.src.__support.CPP.string_view
libc.src.__support.CPP.type_traits libc.src.__support.CPP.type_traits
) )

View File

@ -57,6 +57,7 @@ add_header_library(
HDRS HDRS
span.h span.h
DEPENDS DEPENDS
.array
.type_traits .type_traits
) )
@ -71,7 +72,7 @@ add_header_library(
HDRS HDRS
stringstream.h stringstream.h
DEPENDS DEPENDS
.array_ref .span
.string_view .string_view
libc.src.__support.integer_to_string libc.src.__support.integer_to_string
) )

View File

@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H #define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
#include "ArrayRef.h"
#include "StringView.h" #include "StringView.h"
#include "span.h"
#include "type_traits.h" #include "type_traits.h"
#include "src/__support/integer_to_string.h" #include "src/__support/integer_to_string.h"
@ -22,7 +22,7 @@ namespace cpp {
// without any dynamic memory allocation. There is no requirement to mimic the // without any dynamic memory allocation. There is no requirement to mimic the
// C++ standard library class std::stringstream. // C++ standard library class std::stringstream.
class StringStream { class StringStream {
MutableArrayRef<char> data; span<char> data;
size_t write_ptr = 0; // The current write pointer size_t write_ptr = 0; // The current write pointer
bool err = false; // If an error occurs while writing bool err = false; // If an error occurs while writing
@ -41,7 +41,7 @@ public:
static constexpr char ENDS = '\0'; static constexpr char ENDS = '\0';
// Create a string stream which will write into |buf|. // Create a string stream which will write into |buf|.
constexpr StringStream(const MutableArrayRef<char> &buf) : data(buf) {} constexpr StringStream(const span<char> &buf) : data(buf) {}
// Return a StringView to the current characters in the stream. If a // Return a StringView to the current characters in the stream. If a
// null terminator was not explicitly written, then the return value // null terminator was not explicitly written, then the return value

View File

@ -23,7 +23,7 @@ add_object_library(
HDRS HDRS
dir.h dir.h
DEPENDS DEPENDS
libc.src.__support.CPP.array_ref libc.src.__support.CPP.span
libc.src.__support.threads.mutex libc.src.__support.threads.mutex
) )

View File

@ -29,8 +29,7 @@ Dir *Dir::open(const char *path) {
struct ::dirent *Dir::read() { struct ::dirent *Dir::read() {
MutexLock lock(&mutex); MutexLock lock(&mutex);
if (readptr >= fillsize) { if (readptr >= fillsize) {
fillsize = platform_fetch_dirents( fillsize = platform_fetch_dirents(fd, buffer);
fd, cpp::MutableArrayRef<uint8_t>(buffer, BUFSIZE));
if (fillsize == 0) if (fillsize == 0)
return nullptr; return nullptr;
readptr = 0; readptr = 0;

View File

@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H #ifndef LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H
#define LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H #define LLVM_LIBC_SRC_SUPPORT_FILE_DIR_H
#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/span.h"
#include "src/__support/threads/mutex.h" #include "src/__support/threads/mutex.h"
#include <dirent.h> #include <dirent.h>
@ -28,7 +28,7 @@ bool platform_closedir(int fd);
// Platform specific function which will fetch dirents in to buffer. // Platform specific function which will fetch dirents in to buffer.
// Returns the number of bytes written into buffer // Returns the number of bytes written into buffer
size_t platform_fetch_dirents(int fd, cpp::MutableArrayRef<uint8_t> buffer); size_t platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer);
// This class is designed to allow implementation of the POSIX dirent.h API. // This class is designed to allow implementation of the POSIX dirent.h API.
// By itself, it is platform independent but calls platform specific // By itself, it is platform independent but calls platform specific

View File

@ -34,7 +34,7 @@ int platform_opendir(const char *name) {
return fd; return fd;
} }
size_t platform_fetch_dirents(int fd, cpp::MutableArrayRef<uint8_t> buffer) { size_t platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
long size = long size =
__llvm_libc::syscall(SYS_getdents, fd, buffer.data(), buffer.size()); __llvm_libc::syscall(SYS_getdents, fd, buffer.data(), buffer.size());
if (size < 0) { if (size < 0) {

View File

@ -9,9 +9,9 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H #ifndef LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H
#define LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H #define LLVM_LIBC_SRC_SUPPORT_INTEGER_TO_STRING_H
#include "src/__support/CPP/ArrayRef.h"
#include "src/__support/CPP/StringView.h" #include "src/__support/CPP/StringView.h"
#include "src/__support/CPP/optional.h" #include "src/__support/CPP/optional.h"
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/type_traits.h" #include "src/__support/CPP/type_traits.h"
namespace __llvm_libc { namespace __llvm_libc {
@ -43,7 +43,7 @@ namespace __llvm_libc {
// auto str = IntegerToString::convert<30>(a, b30buf); // auto str = IntegerToString::convert<30>(a, b30buf);
class IntegerToString { class IntegerToString {
static cpp::StringView convert_uintmax(uintmax_t uval, static cpp::StringView convert_uintmax(uintmax_t uval,
cpp::MutableArrayRef<char> &buffer, cpp::span<char> &buffer,
bool lowercase, bool lowercase,
const uint8_t conv_base) { const uint8_t conv_base) {
const char a = lowercase ? 'a' : 'A'; const char a = lowercase ? 'a' : 'A';
@ -65,8 +65,7 @@ class IntegerToString {
return cpp::StringView(buffer.data() + buffer.size() - len, len); return cpp::StringView(buffer.data() + buffer.size() - len, len);
} }
static cpp::StringView convert_intmax(intmax_t val, static cpp::StringView convert_intmax(intmax_t val, cpp::span<char> &buffer,
cpp::MutableArrayRef<char> &buffer,
bool lowercase, bool lowercase,
const uint8_t conv_base) { const uint8_t conv_base) {
if (val >= 0) if (val >= 0)
@ -137,8 +136,8 @@ public:
template <uint8_t BASE, typename T, template <uint8_t BASE, typename T,
cpp::enable_if_t<2 <= BASE && BASE <= 36 && cpp::is_integral_v<T>, cpp::enable_if_t<2 <= BASE && BASE <= 36 && cpp::is_integral_v<T>,
int> = 0> int> = 0>
static cpp::optional<cpp::StringView> static cpp::optional<cpp::StringView> convert(T val, cpp::span<char> buffer,
convert(T val, cpp::MutableArrayRef<char> buffer, bool lowercase = true) { bool lowercase = true) {
if (buffer.size() < bufsize<BASE, T>()) if (buffer.size() < bufsize<BASE, T>())
return cpp::optional<cpp::StringView>(); return cpp::optional<cpp::StringView>();
if (cpp::is_signed_v<T>) if (cpp::is_signed_v<T>)
@ -148,26 +147,23 @@ public:
} }
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0> template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::StringView> dec(T val, static cpp::optional<cpp::StringView> dec(T val, cpp::span<char> buffer) {
cpp::MutableArrayRef<char> buffer) {
return convert<10>(val, buffer); return convert<10>(val, buffer);
} }
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0> template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::StringView> static cpp::optional<cpp::StringView> hex(T val, cpp::span<char> buffer,
hex(T val, cpp::MutableArrayRef<char> buffer, bool lowercase = true) { bool lowercase = true) {
return convert<16>(val, buffer, lowercase); return convert<16>(val, buffer, lowercase);
} }
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0> template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::StringView> oct(T val, static cpp::optional<cpp::StringView> oct(T val, cpp::span<char> buffer) {
cpp::MutableArrayRef<char> buffer) {
return convert<8>(val, buffer); return convert<8>(val, buffer);
} }
template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0> template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
static cpp::optional<cpp::StringView> bin(T val, static cpp::optional<cpp::StringView> bin(T val, cpp::span<char> buffer) {
cpp::MutableArrayRef<char> buffer) {
return convert<2>(val, buffer); return convert<2>(val, buffer);
} }
}; };

View File

@ -59,10 +59,11 @@ add_object_library(
DEPENDS DEPENDS
.writer .writer
.core_structs .core_structs
libc.src.__support.integer_to_string
libc.src.__support.CPP.limits libc.src.__support.CPP.limits
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view libc.src.__support.CPP.string_view
libc.src.__support.FPUtil.fputil libc.src.__support.FPUtil.fputil
libc.src.__support.integer_to_string
) )

View File

@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/span.h"
#include "src/__support/CPP/StringView.h" #include "src/__support/CPP/StringView.h"
#include "src/__support/integer_to_string.h" #include "src/__support/integer_to_string.h"
#include "src/stdio/printf_core/converter_utils.h" #include "src/stdio/printf_core/converter_utils.h"
@ -28,7 +28,7 @@ constexpr char inline to_lower(char a) { return a | 32; }
constexpr bool inline is_lower(char a) { return (a & 32) > 0; } constexpr bool inline is_lower(char a) { return (a & 32) > 0; }
cpp::optional<cpp::StringView> inline num_to_strview( cpp::optional<cpp::StringView> inline num_to_strview(
uintmax_t num, cpp::MutableArrayRef<char> bufref, char conv_name) { uintmax_t num, cpp::span<char> bufref, char conv_name) {
if (to_lower(conv_name) == 'x') { if (to_lower(conv_name) == 'x') {
return IntegerToString::hex(num, bufref, is_lower(conv_name)); return IntegerToString::hex(num, bufref, is_lower(conv_name));
} else if (conv_name == 'o') { } else if (conv_name == 'o') {

View File

@ -78,7 +78,7 @@ add_libc_unittest(
SRCS SRCS
stringstream_test.cpp stringstream_test.cpp
DEPENDS DEPENDS
libc.src.__support.CPP.array_ref libc.src.__support.CPP.span
libc.src.__support.CPP.stringstream libc.src.__support.CPP.stringstream
) )

View File

@ -6,23 +6,22 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/span.h"
#include "src/__support/CPP/stringstream.h" #include "src/__support/CPP/stringstream.h"
#include "utils/UnitTest/Test.h" #include "utils/UnitTest/Test.h"
using __llvm_libc::cpp::MutableArrayRef; using __llvm_libc::cpp::span;
using __llvm_libc::cpp::StringStream; using __llvm_libc::cpp::StringStream;
TEST(LlvmLibcStringStreamTest, Simple) { TEST(LlvmLibcStringStreamTest, Simple) {
char buf[256]; char buf[256];
MutableArrayRef<char> bufref(reinterpret_cast<void *>(buf), 256);
StringStream ss1(bufref); StringStream ss1(buf);
ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS; ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
ASSERT_FALSE(ss1.overflow()); ASSERT_FALSE(ss1.overflow());
ASSERT_STREQ(ss1.str().data(), "Hello, Stream - 123"); ASSERT_STREQ(ss1.str().data(), "Hello, Stream - 123");
StringStream ss2(bufref); StringStream ss2(buf);
ss2 << 'a' << 'b' << 'c' << StringStream::ENDS; ss2 << 'a' << 'b' << 'c' << StringStream::ENDS;
ASSERT_FALSE(ss2.overflow()); ASSERT_FALSE(ss2.overflow());
ASSERT_STREQ(ss2.str().data(), "abc"); ASSERT_STREQ(ss2.str().data(), "abc");
@ -31,14 +30,13 @@ TEST(LlvmLibcStringStreamTest, Simple) {
TEST(LlvmLibcStringStreamTest, Overflow) { TEST(LlvmLibcStringStreamTest, Overflow) {
constexpr size_t BUFSIZE = 8; constexpr size_t BUFSIZE = 8;
char buf[BUFSIZE]; char buf[BUFSIZE];
MutableArrayRef<char> bufref(reinterpret_cast<void *>(buf), BUFSIZE);
StringStream ss1(bufref); StringStream ss1(buf);
ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS; ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
ASSERT_TRUE(ss1.overflow()); ASSERT_TRUE(ss1.overflow());
ASSERT_EQ(ss1.str().size(), BUFSIZE); ASSERT_EQ(ss1.str().size(), BUFSIZE);
StringStream ss2(bufref); StringStream ss2(buf);
ss2 << "7777777"; ss2 << "7777777";
ASSERT_FALSE(ss2.overflow()); ASSERT_FALSE(ss2.overflow());
ASSERT_EQ(ss2.str().size(), size_t(7)); ASSERT_EQ(ss2.str().size(), size_t(7));

View File

@ -6,16 +6,16 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/span.h"
#include "src/string/stpncpy.h" #include "src/string/stpncpy.h"
#include "utils/UnitTest/Test.h" #include "utils/UnitTest/Test.h"
#include <stddef.h> // For size_t. #include <stddef.h> // For size_t.
class LlvmLibcStpncpyTest : public __llvm_libc::testing::Test { class LlvmLibcStpncpyTest : public __llvm_libc::testing::Test {
public: public:
void check_stpncpy(__llvm_libc::cpp::MutableArrayRef<char> dst, void check_stpncpy(__llvm_libc::cpp::span<char> dst,
const __llvm_libc::cpp::ArrayRef<char> src, size_t n, const __llvm_libc::cpp::span<const char> src, size_t n,
const __llvm_libc::cpp::ArrayRef<char> expected, const __llvm_libc::cpp::span<const char> expected,
size_t expectedCopied) { size_t expectedCopied) {
// Making sure we don't overflow buffer. // Making sure we don't overflow buffer.
ASSERT_GE(dst.size(), n); ASSERT_GE(dst.size(), n);