Prefer std::string over std::wstring. (#179)

In the past, FTXUI switched from std::string to std::wstring to support
fullwidth characters. The reasons was that fullwidth characters can be
stored inside a single wchar_t.

Then FTXUI added support for combining characters. A single glygh
doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large.

The usage of wstring doesn't really fit the new model and have several
drawbacks:
1. It doesn't simplify the implementation of FTXUI, because of combining
   characters.
2. It reduces drawing performance by 2x.
3. It increase Screen's memory allocation by 2x.

This patch converts FTXUI to use std::string internally. It now exposes
std::string based API. The std::wstring API remains, but is now
deprecated.

Tests and examples haven't been update to show the breakage is limited.
They will be updated in a second set of patches.

Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153
Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
This commit is contained in:
Arthur Sonzogni
2021-08-08 23:25:20 +02:00
committed by GitHub
parent 1ff894f6f5
commit 3b4ab618a3
84 changed files with 1234 additions and 996 deletions

View File

@@ -303,7 +303,7 @@ class Color {
bool operator==(const Color& rhs) const;
bool operator!=(const Color& rhs) const;
std::wstring Print(bool is_background_color) const;
std::string Print(bool is_background_color) const;
private:
enum class ColorType : uint8_t {

View File

@@ -2,7 +2,7 @@
#define FTXUI_SCREEN_SCREEN
#include <memory>
#include <string> // for allocator, wstring, string, basic_string
#include <string> // for string, allocator, basic_string
#include <vector> // for vector
#include "ftxui/screen/box.hpp" // for Box
@@ -16,8 +16,7 @@ namespace ftxui {
struct Pixel {
// The graphemes stored into the pixel. To support combining characters,
// like: a⃦, this can potentially contains multiple codepoitns.
// Required: character.size() >= 1;
std::wstring character = L" ";
std::string character = " ";
// Colors:
Color background_color = Color::Default;
@@ -55,7 +54,7 @@ class Screen {
static Screen Create(Dimensions width, Dimensions height);
// Node write into the screen using Screen::at.
wchar_t& at(int x, int y);
std::string& at(int x, int y);
Pixel& PixelAt(int x, int y);
// Convert the screen into a printable string in the terminal.

View File

@@ -2,6 +2,7 @@
#define FTXUI_SCREEN_STRING_HPP
#include <string>
#include <vector>
namespace ftxui {
std::string to_string(const std::wstring& s);
@@ -12,12 +13,10 @@ std::wstring to_wstring(T s) {
return to_wstring(std::to_string(s));
}
int wchar_width(char32_t);
int wchar_width(wchar_t);
int wchar_width_cjk(char32_t);
int wchar_width_cjk(wchar_t);
int wstring_width(const std::wstring&);
int wstring_width_cjk(const std::wstring&);
int string_width(const std::string&);
std::vector<std::string> Utf8ToGlyphs(const std::string& input);
} // namespace ftxui