From de4ac269e428d595d731b0da882f6942f08aa1e1 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Mon, 7 Jun 2021 00:34:08 +0200 Subject: [PATCH] Replace std::strlen with stringLength --- ChangeLog | 1 + src/fkeyboard.cpp | 12 +++++------ src/fmouse.cpp | 4 ++-- src/foptimove.cpp | 2 +- src/ftermcapquirks.cpp | 2 +- src/ftermdetection.cpp | 32 ++++++++++++++---------------- src/include/final/ftermdetection.h | 16 +++++++-------- src/include/final/ftypes.h | 8 +++++++- 8 files changed, 41 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index dbddeb34..fc097e89 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 2021-06-06 Markus Gans * Bug fixing in FString and FTermDetection * FTermDetection now has clean code by using FString instead of char* + * Replace std::strlen with stringLength 2021-06-03 Markus Gans * Some FString optimizations diff --git a/src/fkeyboard.cpp b/src/fkeyboard.cpp index bb469bf5..78260726 100644 --- a/src/fkeyboard.cpp +++ b/src/fkeyboard.cpp @@ -249,7 +249,7 @@ inline FKey FKeyboard::getMouseProtocolKey() const if ( ! mouse_support ) return NOT_SET; - const std::size_t buf_len = std::strlen(fifo_buf); + const std::size_t buf_len = stringLength(fifo_buf); // x11 mouse tracking if ( buf_len >= 6 && fifo_buf[1] == '[' && fifo_buf[2] == 'M' ) @@ -282,7 +282,7 @@ inline FKey FKeyboard::getTermcapKey() for (auto&& entry : *key_map) { const char* kstr = entry.string; - const std::size_t len = kstr ? std::strlen(kstr) : 0; + const std::size_t len = kstr ? stringLength(kstr) : 0; if ( kstr && std::strncmp(kstr, fifo_buf, len) == 0 ) // found { @@ -312,7 +312,7 @@ inline FKey FKeyboard::getKnownKey() for (auto&& entry : fc::fkey_table) { const char* kstr = entry.string; // The string is never null - const std::size_t len = std::strlen(kstr); + const std::size_t len = stringLength(kstr); if ( std::strncmp(kstr, fifo_buf, len) == 0 ) // found { @@ -355,7 +355,7 @@ inline FKey FKeyboard::getSingleKey() if ( utf8_input && (firstchar & 0xc0) == 0xc0 ) { std::array utf8char{}; // Init array with '\0' - const std::size_t buf_len = std::strlen(fifo_buf); + const std::size_t buf_len = stringLength(fifo_buf); if ( (firstchar & 0xe0) == 0xc0 ) len = 2; @@ -482,14 +482,14 @@ void FKeyboard::parseKeyBuffer() { key = fkey; mouseTracking(); - fifo_offset = int(std::strlen(fifo_buf)); + fifo_offset = int(stringLength(fifo_buf)); break; } if ( fkey != FKey::Incomplete ) { fkey_queue.push(fkey); - fifo_offset = int(std::strlen(fifo_buf)); + fifo_offset = int(stringLength(fifo_buf)); } } diff --git a/src/fmouse.cpp b/src/fmouse.cpp index ae9233de..c4e2a711 100644 --- a/src/fmouse.cpp +++ b/src/fmouse.cpp @@ -739,7 +739,7 @@ void FMouseSGR::setRawData (FKeyboard::keybuffer& fifo_buf) // Import the X11 xterm mouse protocol (SGR-Mode) raw mouse data const std::size_t fifo_buf_size = sizeof(fifo_buf); - std::size_t len = std::strlen(fifo_buf); + std::size_t len = stringLength(fifo_buf); std::size_t n{3}; while ( n < len && n <= MOUSE_BUF_SIZE + 1 ) @@ -969,7 +969,7 @@ void FMouseUrxvt::setRawData (FKeyboard::keybuffer& fifo_buf) // Import the X11 xterm mouse protocol (Urxvt-Mode) raw mouse data const std::size_t fifo_buf_size = sizeof(fifo_buf); - std::size_t len = std::strlen(fifo_buf); + std::size_t len = stringLength(fifo_buf); std::size_t n{2}; while ( n < len && n <= MOUSE_BUF_SIZE ) diff --git a/src/foptimove.cpp b/src/foptimove.cpp index 8aa1930a..37bc50b4 100644 --- a/src/foptimove.cpp +++ b/src/foptimove.cpp @@ -608,7 +608,7 @@ int FOptiMove::repeatedAppend ( std::string& dst , const Capability& o , int count ) const { - const std::size_t src_len = std::strlen(o.cap); + const std::size_t src_len = stringLength(o.cap); const std::size_t dst_len = dst.length(); int total{0}; diff --git a/src/ftermcapquirks.cpp b/src/ftermcapquirks.cpp index 9f4e5097..b0606fd0 100644 --- a/src/ftermcapquirks.cpp +++ b/src/ftermcapquirks.cpp @@ -381,7 +381,7 @@ void FTermcapQuirks::sunConsole() fc::fkey_cap_table[i].string = "\b"; // backspace key if ( std::strncmp(fc::fkey_cap_table[i].tname, "kD", 2) == 0 - && std::strlen(fc::fkey_cap_table[i].tname) == 2 ) + && stringLength(fc::fkey_cap_table[i].tname) == 2 ) fc::fkey_cap_table[i].string = "\177"; // delete-character key if ( std::strncmp(fc::fkey_cap_table[i].tname, "@7", 2) == 0 ) diff --git a/src/ftermdetection.cpp b/src/ftermdetection.cpp index 73f23bc9..9ea6ebac 100644 --- a/src/ftermdetection.cpp +++ b/src/ftermdetection.cpp @@ -390,14 +390,14 @@ bool FTermDetection::get256colorEnvString() color_env.string7 = std::getenv("COLORFGBG"); color_env.string8 = std::getenv("KITTY_WINDOW_ID"); - if ( color_env.string1 != nullptr - || color_env.string2 != nullptr - || color_env.string3 != nullptr - || color_env.string4 != nullptr - || color_env.string5 != nullptr - || color_env.string6 != nullptr - || color_env.string7 != nullptr - || color_env.string8 != nullptr ) + if ( ! color_env.string1.isEmpty() + || ! color_env.string2.isEmpty() + || ! color_env.string3.isEmpty() + || ! color_env.string4.isEmpty() + || ! color_env.string5.isEmpty() + || ! color_env.string6.isEmpty() + || ! color_env.string7.isEmpty() + || ! color_env.string8.isEmpty() ) return true; return false; @@ -408,9 +408,8 @@ FString FTermDetection::termtype_256color_quirks() { FString new_termtype{}; - if ( color_env.string2 - || (color_env.string1 - && std::strncmp(color_env.string1, "gnome-terminal", 14) == 0) ) + if ( ! color_env.string2.isEmpty() + || color_env.string1 == "gnome-terminal" ) { terminal_type.gnome_terminal = true; // Each gnome-terminal should be able to use 256 colors @@ -439,21 +438,20 @@ FString FTermDetection::termtype_256color_quirks() new_termtype = "mlterm-256color"; if ( termtype.left(4) == "rxvt" - && color_env.string1 - && std::strncmp(color_env.string1, "rxvt-xpm", 8) == 0 ) + && color_env.string1.left(8) == "rxvt-xpm" ) { new_termtype = "rxvt-256color"; terminal_type.rxvt = true; } - if ( (color_env.string5 && std::strlen(color_env.string5) > 0) - || (color_env.string6 && std::strlen(color_env.string6) > 0) ) + if ( color_env.string5.getLength() > 0 + || color_env.string6.getLength() > 0 ) { terminal_type.kde_konsole = true; new_termtype = "konsole-256color"; } - if ( color_env.string3 && std::strlen(color_env.string3) > 0 ) + if ( color_env.string3.getLength() > 0 ) decscusr_support = true; return new_termtype; @@ -538,7 +536,7 @@ FString FTermDetection::getXTermColorName (FColor color) const if ( pos > 4 && std::sscanf(temp.data(), parse, &index, buf.data()) == 2 ) { - std::size_t n = std::strlen(buf.data()); + auto n = stringLength(buf.data()); // BEL + '\0' = string terminator if ( n >= 6 && buf[n - 1] == BEL[0] && buf[n] == '\0' ) diff --git a/src/include/final/ftermdetection.h b/src/include/final/ftermdetection.h index 61819395..4ca3d58b 100644 --- a/src/include/final/ftermdetection.h +++ b/src/include/final/ftermdetection.h @@ -170,14 +170,14 @@ class FTermDetection final private: struct colorEnv { - char* string1{nullptr}; - char* string2{nullptr}; - char* string3{nullptr}; - char* string4{nullptr}; - char* string5{nullptr}; - char* string6{nullptr}; - char* string7{nullptr}; - char* string8{nullptr}; + FString string1{}; + FString string2{}; + FString string3{}; + FString string4{}; + FString string5{}; + FString string6{}; + FString string7{}; + FString string8{}; }; struct secondaryDA diff --git a/src/include/final/ftypes.h b/src/include/final/ftypes.h index 064cd06b..e5324b5b 100644 --- a/src/include/final/ftypes.h +++ b/src/include/final/ftypes.h @@ -119,12 +119,18 @@ std::unique_ptr make_unique (Args&&... args) return std::unique_ptr(new T(std::forward(args)...)); } -template +template constexpr std::reverse_iterator make_reverse_iterator (Iter iter) { return std::reverse_iterator(iter); } +template +constexpr std::size_t stringLength (T&& array) +{ + return std::string(std::forward(array)).length(); +} + using charSubstitution = std::unordered_map; struct FCharAttribute