mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Add string view overloads (#1154)
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
This is better ergonomic, as `std::string_view` is lightweight and accept more conversion than `const std::string&`. Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <ftxui/screen/string.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
@@ -125,13 +126,15 @@ class ConstStringListRef {
|
||||
Adapter& operator=(Adapter&&) = default;
|
||||
virtual ~Adapter() = default;
|
||||
virtual size_t size() const = 0;
|
||||
virtual std::string operator[](size_t i) const = 0;
|
||||
virtual std::string_view operator[](size_t i) const = 0;
|
||||
};
|
||||
using Variant = std::variant<const std::vector<std::string>, //
|
||||
const std::vector<std::string>*, //
|
||||
const std::vector<std::wstring>*, //
|
||||
Adapter*, //
|
||||
std::unique_ptr<Adapter> //
|
||||
using Variant = std::variant<const std::vector<std::string>, //
|
||||
const std::vector<std::string>*, //
|
||||
const std::vector<std::string_view>, //
|
||||
const std::vector<std::string_view>*, //
|
||||
const std::vector<std::wstring>*, //
|
||||
Adapter*, //
|
||||
std::unique_ptr<Adapter> //
|
||||
>;
|
||||
|
||||
ConstStringListRef() = default;
|
||||
@@ -149,6 +152,14 @@ class ConstStringListRef {
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(std::vector<std::string_view> value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::string_view>* value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::wstring>* value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
@@ -169,7 +180,62 @@ class ConstStringListRef {
|
||||
}
|
||||
|
||||
std::string operator[](size_t i) const {
|
||||
return variant_ ? std::visit(IndexedGetter(i), *variant_) : "";
|
||||
if (!variant_) {
|
||||
return "";
|
||||
}
|
||||
auto& v = *variant_;
|
||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
||||
return std::get<const std::vector<std::string>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
||||
return std::string(std::get<const std::vector<std::string_view>>(v)[i]);
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
||||
return std::string(
|
||||
(*std::get<const std::vector<std::string_view>*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
||||
return to_string((*std::get<const std::vector<std::wstring>*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<Adapter*>(v)) {
|
||||
return std::string((*std::get<Adapter*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
||||
return std::string((*std::get<std::unique_ptr<Adapter>>(v))[i]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string_view at(size_t i) const {
|
||||
if (!variant_) {
|
||||
return "";
|
||||
}
|
||||
auto& v = *variant_;
|
||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
||||
return std::get<const std::vector<std::string>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
||||
return std::get<const std::vector<std::string_view>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string_view>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
||||
return {};
|
||||
}
|
||||
if (std::holds_alternative<Adapter*>(v)) {
|
||||
return (*std::get<Adapter*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
||||
return (*std::get<std::unique_ptr<Adapter>>(v))[i];
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -180,6 +246,12 @@ class ConstStringListRef {
|
||||
size_t operator()(const std::vector<std::string>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::string_view>& v) const {
|
||||
return v.size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::string_view>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::wstring>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
@@ -189,25 +261,6 @@ class ConstStringListRef {
|
||||
}
|
||||
};
|
||||
|
||||
struct IndexedGetter {
|
||||
IndexedGetter(size_t index) // NOLINT
|
||||
: index_(index) {}
|
||||
size_t index_;
|
||||
std::string operator()(const std::vector<std::string>& v) const {
|
||||
return v[index_];
|
||||
}
|
||||
std::string operator()(const std::vector<std::string>* v) const {
|
||||
return (*v)[index_];
|
||||
}
|
||||
std::string operator()(const std::vector<std::wstring>* v) const {
|
||||
return to_string((*v)[index_]);
|
||||
}
|
||||
std::string operator()(const Adapter* v) const { return (*v)[index_]; }
|
||||
std::string operator()(const std::unique_ptr<Adapter>& v) const {
|
||||
return (*v)[index_];
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<Variant> variant_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user