Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/76 string view #77

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,638 changes: 1,638 additions & 0 deletions include/tabulate/string_view_lite.hpp

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion include/tabulate/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ SOFTWARE.
#include <tabulate/table_internal.hpp>

#if __cplusplus >= 201703L
#include <string_view>
#include <variant>
using std::get_if;
using std::holds_alternative;
using std::variant;
using std::visit;
using std::string_view;
#else
#include <tabulate/string_view_lite.hpp>
#include <tabulate/variant_lite.hpp>
using nonstd::get_if;
using nonstd::holds_alternative;
using nonstd::variant;
using nonstd::visit;
using nonstd::string_view;
#endif

#include <utility>
Expand All @@ -56,7 +60,9 @@ class Table {
public:
Table() : table_(TableInternal::create()) {}

Table &add_row(const std::vector<variant<std::string, const char *, Table>> &cells) {
using Row_t = std::vector<variant<std::string, const char *, string_view, Table>>;

Table &add_row(const Row_t &cells) {

if (rows_ == 0) {
// This is the first row added
Expand All @@ -79,6 +85,8 @@ class Table {
cell_strings[i] = *get_if<std::string>(&cell);
} else if (holds_alternative<const char *>(cell)) {
cell_strings[i] = *get_if<const char *>(&cell);
} else if (holds_alternative<string_view>(cell)) {
cell_strings[i] = std::string{*get_if<string_view>(&cell)};
} else {
auto table = *get_if<Table>(&cell);
std::stringstream stream;
Expand Down
3 changes: 3 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ target_link_libraries(asciidoc_export PRIVATE tabulate::tabulate)

add_executable(refresh_table refresh_table.cpp)
target_link_libraries(refresh_table PRIVATE tabulate::tabulate pthread)

add_executable(string_view_in_row string_view_in_row.cpp)
target_link_libraries(string_view_in_row PRIVATE tabulate::tabulate pthread)
10 changes: 1 addition & 9 deletions samples/asciidoc_export.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/asciidoc_exporter.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table movies;
Expand Down
10 changes: 1 addition & 9 deletions samples/class_diagram.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table class_diagram;
Expand Down
10 changes: 1 addition & 9 deletions samples/colors.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table colors;
Expand Down
10 changes: 1 addition & 9 deletions samples/employees.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table employees;
Expand Down
10 changes: 1 addition & 9 deletions samples/font_styles.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table styled_table;
Expand Down
10 changes: 1 addition & 9 deletions samples/iterators.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table table;
Expand Down
10 changes: 1 addition & 9 deletions samples/latex_export.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/latex_exporter.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table movies;
Expand Down
12 changes: 2 additions & 10 deletions samples/mario.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table mario;
mario.format().color(Color::white).border("").corner("").column_separator("").padding(0);
size_t rows = 16;
for (size_t i = 0; i < rows; ++i) {
std::vector<variant<std::string, const char *, Table>> row;
Row_t row;
size_t cols = 30;
for (size_t j = 0; j < cols; ++j) {
row.push_back("█");
Expand Down
10 changes: 1 addition & 9 deletions samples/markdown_export.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/markdown_exporter.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table movies;
Expand Down
2 changes: 1 addition & 1 deletion samples/movies.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table movies;
Expand Down
10 changes: 1 addition & 9 deletions samples/padding.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table no_padding;
Expand Down
2 changes: 1 addition & 1 deletion samples/refresh_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


using namespace tabulate;
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;
std::atomic_bool keep_running(true);

void waitingForWorkEnterKey() {
Expand Down
10 changes: 1 addition & 9 deletions samples/runic.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table table;
Expand Down
10 changes: 1 addition & 9 deletions samples/shape.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

void print_shape(Table &table) {
auto shape = table.shape();
Expand Down
30 changes: 30 additions & 0 deletions samples/string_view_in_row.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <tabulate/table.hpp>
using namespace tabulate;
using Row_t = Table::Row_t;

#if __cplusplus >= 201703L
#include <string_view>
using std::string_view;
#else
#include <tabulate/string_view_lite.hpp>
using nonstd::string_view;
#endif

int main() {
Table table;

string_view c0 = "string_view";
const char * c1 = "const char *";
std::string c2 = "std::string";

Table c3;
c3.add_row({"Table", "", ""});
c3.add_row({c0, c1, c2});
c3[0].format()
.border("")
.corner("");

table.add_row({c0, c1, c2, c3});

std::cout << table << std::endl;
}
14 changes: 3 additions & 11 deletions samples/summary.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {

Expand Down Expand Up @@ -117,15 +109,15 @@ int main() {
.hide_border();

for (size_t i = 0; i < 9; ++i) {
std::vector<variant<std::string, const char *, Table>> row;
Row_t row;
row.push_back(std::to_string(90 - i * 10));
for (size_t j = 0; j <= 50; ++j) {
row.push_back(" ");
}
chart.add_row(row);
}

std::vector<variant<std::string, const char *, Table>> row;
Row_t row;
for (int i = 0; i <= 12; ++i) {
if ((i + 1) % 4 == 0) {
row.push_back(std::to_string(i + 1));
Expand Down
10 changes: 1 addition & 9 deletions samples/unicode.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table table;
Expand Down
10 changes: 1 addition & 9 deletions samples/universal_constants.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {

Expand Down
10 changes: 1 addition & 9 deletions samples/word_wrapping.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <tabulate/table.hpp>
using namespace tabulate;

#if __cplusplus >= 201703L
#include <variant>
using std::variant;
#else
#include <tabulate/variant_lite.hpp>
using nonstd::variant;
#endif
using Row_t = std::vector<variant<std::string, const char *, Table>>;
using Row_t = Table::Row_t;

int main() {
Table table;
Expand Down
1 change: 1 addition & 0 deletions single_include.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"sources": [
"include/tabulate/variant_lite.hpp",
"include/tabulate/optional_lite.hpp",
"include/tabulate/string_view_lite.hpp",
"include/tabulate/termcolor.hpp",
"include/tabulate/utf8.hpp",
"include/tabulate/color.hpp",
Expand Down