Revert "[NFC][libc] Use span instead of ArrayRef"

This reverts commit e7b250b8a6.
This commit is contained in:
Guillaume Chatelet 2022-08-22 08:14:22 +00:00
parent e7b250b8a6
commit 70ca9a6600
13 changed files with 40 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -59,11 +59,10 @@ add_object_library(
DEPENDS
.writer
.core_structs
libc.src.__support.integer_to_string
libc.src.__support.CPP.limits
libc.src.__support.CPP.span
libc.src.__support.CPP.string_view
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
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_INT_CONVERTER_H
#include "src/__support/CPP/span.h"
#include "src/__support/CPP/ArrayRef.h"
#include "src/__support/CPP/StringView.h"
#include "src/__support/integer_to_string.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; }
cpp::optional<cpp::StringView> inline num_to_strview(
uintmax_t num, cpp::span<char> bufref, char conv_name) {
uintmax_t num, cpp::MutableArrayRef<char> bufref, char conv_name) {
if (to_lower(conv_name) == 'x') {
return IntegerToString::hex(num, bufref, is_lower(conv_name));
} else if (conv_name == 'o') {

View File

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

View File

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

View File

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