Menu screen prototype, refactor

This commit is contained in:
MihailRis
2023-11-16 03:00:05 +03:00
parent 5af04e20d1
commit b03bcb5191
14 changed files with 267 additions and 52 deletions
+69 -4
View File
@@ -3,6 +3,7 @@
#include <vector>
#include <locale>
#include <sstream>
#include <stdexcept>
using std::vector;
using std::string;
@@ -10,7 +11,7 @@ using std::stringstream;
using std::wstring;
using std::wstringstream;
wstring lfill(wstring s, uint length, wchar_t c) {
wstring util::lfill(wstring s, uint length, wchar_t c) {
if (s.length() >= length) {
return s;
}
@@ -22,7 +23,7 @@ wstring lfill(wstring s, uint length, wchar_t c) {
return ss.str();
}
wstring rfill(wstring s, uint length, wchar_t c) {
wstring util::rfill(wstring s, uint length, wchar_t c) {
if (s.length() >= length) {
return s;
}
@@ -34,7 +35,7 @@ wstring rfill(wstring s, uint length, wchar_t c) {
return ss.str();
}
uint encode_utf8(uint c, ubyte* bytes) {
uint util::encode_utf8(uint32_t c, ubyte* bytes) {
if (c < 0x80) {
bytes[0] = c >> 0 & 0x7F | 0x00;
return 1;
@@ -56,7 +57,52 @@ uint encode_utf8(uint c, ubyte* bytes) {
}
}
string wstr2str_utf8(const wstring ws) {
struct utf_t {
char mask;
char lead;
uint32_t beg;
uint32_t end;
int bits_stored;
};
const utf_t utf[] = {
/* mask lead beg end bits */
{(char)0b00111111, (char)0b10000000, 0, 0, 6},
{(char)0b01111111, (char)0b00000000, 0000, 0177, 7},
{(char)0b00011111, (char)0b11000000, 0200, 03777, 5},
{(char)0b00001111, (char)0b11100000, 04000, 0177777, 4},
{(char)0b00000111, (char)0b11110000, 0200000, 04177777, 3},
{0, 0, 0, 0, 0},
};
inline uint utf8_len(ubyte cp) {
uint len = 0;
for (const utf_t* u = utf; u->mask; ++u) {
if((cp >= u->beg) && (cp <= u->end)) {
break;
}
++len;
}
if(len > 4) /* Out of bounds */
throw std::runtime_error("utf-8 decode error");
return len;
}
extern uint32_t util::decode_utf8(uint& size, const char* chr) {
size = utf8_len(*chr);
int shift = utf[0].bits_stored * (size - 1);
uint32_t code = (*chr++ & utf[size].mask) << shift;
for(uint i = 1; i < size; ++i, ++chr) {
shift -= utf[0].bits_stored;
code |= ((char)*chr & utf[0].mask) << shift;
}
return code;
}
string util::wstr2str_utf8(const wstring ws) {
vector<char> chars;
char buffer[4];
for (wchar_t wc : ws) {
@@ -66,4 +112,23 @@ string wstr2str_utf8(const wstring ws) {
}
}
return string(chars.data(), chars.size());
}
wstring util::str2wstr_utf8(const string s) {
vector<wchar_t> chars;
size_t pos = 0;
uint size = 0;
while (pos < s.length()) {
chars.push_back(decode_utf8(size, &s.at(pos)));
pos += size;
}
return wstring(chars.data(), chars.size());
}
bool util::is_integer(string text) {
for (char c : text) {
if (c < '0' || c > '9')
return false;
}
return true;
}
+9 -4
View File
@@ -4,10 +4,15 @@
#include <string>
#include "../typedefs.h"
extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
namespace util {
extern std::wstring lfill(std::wstring s, uint length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c);
extern uint encode_utf8(uint c, ubyte* bytes);
extern std::string wstr2str_utf8(const std::wstring ws);
extern uint encode_utf8(uint32_t c, ubyte* bytes);
extern uint32_t decode_utf8(uint& size, const char* bytes);
extern std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text);
}
#endif // UTIL_STRINGUTIL_H_