* [Powertop] [PATCH 1/2] Stop using strlen() on C++ strings
@ 2018-03-03 19:53 Mingye Wang
0 siblings, 0 replies; only message in thread
From: Mingye Wang @ 2018-03-03 19:53 UTC (permalink / raw)
To: powertop
[-- Attachment #1: Type: text/plain, Size: 1910 bytes --]
C++'s strings have a quicker length operation. There's no need to
convert those strings to null-terminated C strings and run strlen()
on it.
---
src/devices/ahci.cpp | 2 +-
src/display.cpp | 7 +++----
src/lib.cpp | 2 +-
3 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/src/devices/ahci.cpp b/src/devices/ahci.cpp
index 2fa2f52..31a42d3 100644
--- a/src/devices/ahci.cpp
+++ b/src/devices/ahci.cpp
@@ -150,7 +150,7 @@ ahci::ahci(char *_name, char *path): device()
diskname = model_name(path, _name);
- if (strlen(diskname.c_str()) == 0)
+ if (diskname.length() == 0)
snprintf(humanname, sizeof(humanname), _("SATA link: %s"), _name);
else
snprintf(humanname, sizeof(humanname), _("SATA disk: %s"),
diskname.c_str());
diff --git a/src/display.cpp b/src/display.cpp
index 10ee48a..8484683 100644
--- a/src/display.cpp
+++ b/src/display.cpp
@@ -98,7 +98,7 @@ void show_tab(unsigned int tab)
class tab_window *win;
unsigned int i;
int tab_pos = 17;
- const char *c;
+ string& s = bottom_lines[tab_names[tab]];
if (!display)
return;
@@ -123,9 +123,8 @@ void show_tab(unsigned int tab)
wattrset(bottom_line, A_REVERSE);
mvwprintw(bottom_line, 0,0, "%120s", "");
- c = bottom_lines[tab_names[tab]].c_str();
- if (c && strlen(c) > 0)
- mvwprintw(bottom_line, 0,0, c);
+ if (s.length() > 0)
+ mvwprintw(bottom_line, 0,0, s.c_str());
else
mvwprintw(bottom_line, 0, 0,
"<ESC> %s | <TAB> / <Shift + TAB> %s | ", _("Exit"),
diff --git a/src/lib.cpp b/src/lib.cpp
index 5e48f37..b80ad36 100644
--- a/src/lib.cpp
+++ b/src/lib.cpp
@@ -424,7 +424,7 @@ char *pretty_print(const char *str, char *buf, int len)
p = pretty_prints[str].c_str();
- if (strlen(p) == 0)
+ if (pretty_prints[str].length() == 0)
p = str;
snprintf(buf, len, "%s", p);
--
2.16.1.windows.4
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2018-03-03 19:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-03 19:53 [Powertop] [PATCH 1/2] Stop using strlen() on C++ strings Mingye Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).