Format remaining C++ sources
This commit is contained in:
67
src/util.cpp
67
src/util.cpp
@@ -33,7 +33,8 @@ bool is_utf8_continuation(unsigned char ch) {
|
||||
return (ch & 0xC0U) == 0x80U;
|
||||
}
|
||||
|
||||
std::uint32_t decode_utf8_codepoint(const std::string& text, std::size_t offset, std::size_t* size_out = nullptr) {
|
||||
std::uint32_t decode_utf8_codepoint(const std::string &text, std::size_t offset,
|
||||
std::size_t *size_out = nullptr) {
|
||||
if (offset >= text.size()) {
|
||||
if (size_out != nullptr) {
|
||||
*size_out = 0;
|
||||
@@ -62,23 +63,23 @@ std::uint32_t decode_utf8_codepoint(const std::string& text, std::size_t offset,
|
||||
}
|
||||
if (size == 2) {
|
||||
return ((lead & 0x1FU) << 6) |
|
||||
(static_cast<unsigned char>(text[offset + 1]) & 0x3FU);
|
||||
(static_cast<unsigned char>(text[offset + 1]) & 0x3FU);
|
||||
}
|
||||
if (size == 3) {
|
||||
return ((lead & 0x0FU) << 12) |
|
||||
((static_cast<unsigned char>(text[offset + 1]) & 0x3FU) << 6) |
|
||||
(static_cast<unsigned char>(text[offset + 2]) & 0x3FU);
|
||||
((static_cast<unsigned char>(text[offset + 1]) & 0x3FU) << 6) |
|
||||
(static_cast<unsigned char>(text[offset + 2]) & 0x3FU);
|
||||
}
|
||||
return ((lead & 0x07U) << 18) |
|
||||
((static_cast<unsigned char>(text[offset + 1]) & 0x3FU) << 12) |
|
||||
((static_cast<unsigned char>(text[offset + 2]) & 0x3FU) << 6) |
|
||||
(static_cast<unsigned char>(text[offset + 3]) & 0x3FU);
|
||||
((static_cast<unsigned char>(text[offset + 1]) & 0x3FU) << 12) |
|
||||
((static_cast<unsigned char>(text[offset + 2]) & 0x3FU) << 6) |
|
||||
(static_cast<unsigned char>(text[offset + 3]) & 0x3FU);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
std::string get_env(const char* name) {
|
||||
const char* value = std::getenv(name);
|
||||
std::string get_env(const char *name) {
|
||||
const char *value = std::getenv(name);
|
||||
return value == nullptr ? std::string() : std::string(value);
|
||||
}
|
||||
|
||||
@@ -94,7 +95,7 @@ std::string trim_copy(std::string value) {
|
||||
}
|
||||
|
||||
std::string single_line(std::string text) {
|
||||
for (char& c : text) {
|
||||
for (char &c : text) {
|
||||
if (c == '\n' || c == '\r' || c == '\t') {
|
||||
c = ' ';
|
||||
}
|
||||
@@ -102,16 +103,16 @@ std::string single_line(std::string text) {
|
||||
return trim_copy(std::move(text));
|
||||
}
|
||||
|
||||
bool is_decimal_number(const std::string& value) {
|
||||
return !value.empty() &&
|
||||
std::all_of(value.begin(), value.end(), [](unsigned char ch) { return std::isdigit(ch); });
|
||||
bool is_decimal_number(const std::string &value) {
|
||||
return !value.empty() && std::all_of(value.begin(), value.end(),
|
||||
[](unsigned char ch) { return std::isdigit(ch); });
|
||||
}
|
||||
|
||||
std::filesystem::path data_root() {
|
||||
if (const char* xdg = std::getenv("XDG_DATA_HOME"); xdg != nullptr && *xdg != '\0') {
|
||||
if (const char *xdg = std::getenv("XDG_DATA_HOME"); xdg != nullptr && *xdg != '\0') {
|
||||
return std::filesystem::path(xdg) / "telegram-tui";
|
||||
}
|
||||
if (const char* home = std::getenv("HOME"); home != nullptr && *home != '\0') {
|
||||
if (const char *home = std::getenv("HOME"); home != nullptr && *home != '\0') {
|
||||
return std::filesystem::path(home) / ".local" / "share" / "telegram-tui";
|
||||
}
|
||||
return std::filesystem::current_path() / ".telegram-tui-data";
|
||||
@@ -130,16 +131,16 @@ StoredConfig load_app_config() {
|
||||
return {};
|
||||
}
|
||||
return StoredConfig{
|
||||
safe_string(config, "api_id"),
|
||||
safe_string(config, "api_hash"),
|
||||
config.value("auto_reload_chat_history", false),
|
||||
safe_string(config, "api_id"),
|
||||
safe_string(config, "api_hash"),
|
||||
config.value("auto_reload_chat_history", false),
|
||||
};
|
||||
} catch (const json::exception&) {
|
||||
} catch (const json::exception &) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool save_app_config(const StoredConfig& config) {
|
||||
bool save_app_config(const StoredConfig &config) {
|
||||
const std::filesystem::path path = data_root() / "config.json";
|
||||
try {
|
||||
if (path.has_parent_path()) {
|
||||
@@ -161,26 +162,26 @@ bool save_app_config(const StoredConfig& config) {
|
||||
}
|
||||
output << document.dump(2) << '\n';
|
||||
return static_cast<bool>(output);
|
||||
} catch (const std::exception&) {
|
||||
} catch (const std::exception &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string safe_string(const json& object, const char* key) {
|
||||
std::string safe_string(const json &object, const char *key) {
|
||||
if (!object.contains(key) || !object.at(key).is_string()) {
|
||||
return {};
|
||||
}
|
||||
return object.at(key).get<std::string>();
|
||||
}
|
||||
|
||||
std::int64_t safe_i64(const json& object, const char* key) {
|
||||
std::int64_t safe_i64(const json &object, const char *key) {
|
||||
if (!object.contains(key) || !object.at(key).is_number_integer()) {
|
||||
return 0;
|
||||
}
|
||||
return object.at(key).get<std::int64_t>();
|
||||
}
|
||||
|
||||
std::int32_t safe_i32(const json& object, const char* key) {
|
||||
std::int32_t safe_i32(const json &object, const char *key) {
|
||||
if (!object.contains(key) || !object.at(key).is_number_integer()) {
|
||||
return 0;
|
||||
}
|
||||
@@ -228,7 +229,7 @@ std::string format_file_size(std::int64_t size_bytes) {
|
||||
return "?";
|
||||
}
|
||||
|
||||
static constexpr const char* units[] = {"B", "KB", "MB", "GB", "TB"};
|
||||
static constexpr const char *units[] = {"B", "KB", "MB", "GB", "TB"};
|
||||
double size = static_cast<double>(size_bytes);
|
||||
std::size_t unit_index = 0;
|
||||
while (size >= 1024.0 && unit_index + 1 < std::size(units)) {
|
||||
@@ -243,7 +244,7 @@ std::string format_file_size(std::int64_t size_bytes) {
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::vector<std::string> wrap_text(const std::string& text, int width) {
|
||||
std::vector<std::string> wrap_text(const std::string &text, int width) {
|
||||
if (width <= 1) {
|
||||
return {text};
|
||||
}
|
||||
@@ -286,7 +287,7 @@ std::vector<std::string> wrap_text(const std::string& text, int width) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::size_t utf8_byte_index_from_utf16_offset(const std::string& text, std::size_t utf16_offset) {
|
||||
std::size_t utf8_byte_index_from_utf16_offset(const std::string &text, std::size_t utf16_offset) {
|
||||
std::size_t byte_index = 0;
|
||||
std::size_t utf16_units = 0;
|
||||
while (byte_index < text.size() && utf16_units < utf16_offset) {
|
||||
@@ -305,7 +306,7 @@ std::size_t utf8_byte_index_from_utf16_offset(const std::string& text, std::size
|
||||
return byte_index;
|
||||
}
|
||||
|
||||
std::size_t utf8_prev_index(const std::string& text, std::size_t byte_index) {
|
||||
std::size_t utf8_prev_index(const std::string &text, std::size_t byte_index) {
|
||||
if (byte_index == 0 || text.empty()) {
|
||||
return 0;
|
||||
}
|
||||
@@ -317,7 +318,7 @@ std::size_t utf8_prev_index(const std::string& text, std::size_t byte_index) {
|
||||
return index;
|
||||
}
|
||||
|
||||
std::size_t utf8_next_index(const std::string& text, std::size_t byte_index) {
|
||||
std::size_t utf8_next_index(const std::string &text, std::size_t byte_index) {
|
||||
if (byte_index >= text.size()) {
|
||||
return text.size();
|
||||
}
|
||||
@@ -327,7 +328,7 @@ std::size_t utf8_next_index(const std::string& text, std::size_t byte_index) {
|
||||
return std::min(text.size(), byte_index + std::max<std::size_t>(1, size));
|
||||
}
|
||||
|
||||
int utf8_display_width(const std::string& text, std::size_t byte_limit) {
|
||||
int utf8_display_width(const std::string &text, std::size_t byte_limit) {
|
||||
const std::size_t limit = std::min(byte_limit, text.size());
|
||||
int width = 0;
|
||||
std::size_t index = 0;
|
||||
@@ -351,7 +352,7 @@ int utf8_display_width(const std::string& text, std::size_t byte_limit) {
|
||||
return width;
|
||||
}
|
||||
|
||||
void pop_utf8_back(std::string& text) {
|
||||
void pop_utf8_back(std::string &text) {
|
||||
if (text.empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -363,4 +364,4 @@ void pop_utf8_back(std::string& text) {
|
||||
text.erase(start);
|
||||
}
|
||||
|
||||
} // namespace telegram_tui
|
||||
} // namespace telegram_tui
|
||||
|
||||
Reference in New Issue
Block a user