fix(parser): Apply clang-tidy fixes

This commit is contained in:
Michael Carlberg 2016-12-14 05:17:18 +01:00
parent 7d07812fa6
commit c2caf4d7a6
3 changed files with 29 additions and 36 deletions

View File

@ -6,7 +6,6 @@
POLYBAR_NS POLYBAR_NS
class signal_emitter; class signal_emitter;
class logger;
struct bar_settings; struct bar_settings;
enum class attribute : uint8_t; enum class attribute : uint8_t;
enum class mousebtn : uint8_t; enum class mousebtn : uint8_t;
@ -18,21 +17,21 @@ DEFINE_CHILD_ERROR(unclosed_actionblocks, parser_error);
class parser { class parser {
public: public:
explicit parser(signal_emitter& emitter, const logger& logger, const bar_settings& bar); explicit parser(signal_emitter& emitter, const bar_settings& bar);
void operator()(string data); void operator()(string data);
void codeblock(string data);
size_t text(string data);
protected: protected:
uint32_t parse_color(string s, uint32_t fallback = 0); void codeblock(string&& data);
int8_t parse_fontindex(string s); size_t text(string&& data);
uint32_t parse_color(const string& s, uint32_t fallback = 0);
int8_t parse_fontindex(const string& s);
attribute parse_attr(const char attr); attribute parse_attr(const char attr);
mousebtn parse_action_btn(string data); mousebtn parse_action_btn(const string& data);
string parse_action_cmd(const string& data); string parse_action_cmd(string&& data);
private: private:
signal_emitter& m_sig; signal_emitter& m_sig;
const logger& m_log;
const bar_settings& m_bar; const bar_settings& m_bar;
vector<int> m_actions; vector<int> m_actions;
}; };

View File

@ -292,7 +292,7 @@ void bar::parse(const string& data, bool force) {
try { try {
if (!data.empty()) { if (!data.empty()) {
parser parser{m_sig, m_log, m_opts}; parser parser{m_sig, m_opts};
parser(data); parser(data);
} }
} catch (const parser_error& err) { } catch (const parser_error& err) {

View File

@ -1,6 +1,5 @@
#include <cassert> #include <cassert>
#include "components/logger.hpp"
#include "components/parser.hpp" #include "components/parser.hpp"
#include "components/types.hpp" #include "components/types.hpp"
#include "events/signal.hpp" #include "events/signal.hpp"
@ -15,26 +14,23 @@ using namespace signals::parser;
/** /**
* Construct parser instance * Construct parser instance
*/ */
parser::parser(signal_emitter& emitter, const logger& logger, const bar_settings& bar) parser::parser(signal_emitter& emitter, const bar_settings& bar) : m_sig(emitter), m_bar(bar) {}
: m_sig(emitter), m_log(logger), m_bar(bar) {}
/** /**
* Process input string * Process input string
*/ */
void parser::operator()(string data) { void parser::operator()(string data) {
size_t pos; while (!data.empty()) {
size_t pos{string::npos};
m_log.trace_x("parser: %s", data);
while (data.length()) {
if (data.compare(0, 2, "%{") == 0 && (pos = data.find('}')) != string::npos) { if (data.compare(0, 2, "%{") == 0 && (pos = data.find('}')) != string::npos) {
codeblock(data.substr(2, pos - 2)); codeblock(data.substr(2, pos - 2));
data.erase(0, pos + 1); data.erase(0, pos + 1);
} else { } else if ((pos = data.find("%{")) != string::npos) {
if ((pos = data.find("%{")) == string::npos) {
pos = data.length();
}
data.erase(0, text(data.substr(0, pos))); data.erase(0, text(data.substr(0, pos)));
} else {
text(move(data));
return;
} }
} }
@ -46,7 +42,7 @@ void parser::operator()(string data) {
/** /**
* Process contents within tag blocks, i.e: %{...} * Process contents within tag blocks, i.e: %{...}
*/ */
void parser::codeblock(string data) { void parser::codeblock(string&& data) {
size_t pos; size_t pos;
while (data.length()) { while (data.length()) {
@ -129,10 +125,9 @@ void parser::codeblock(string data) {
case 'A': case 'A':
if (isdigit(data[0]) || data[0] == ':') { if (isdigit(data[0]) || data[0] == ':') {
value = parse_action_cmd(data); value = parse_action_cmd(data.substr(data[0] != ':' ? 1 : 0));
mousebtn btn = parse_action_btn(data); mousebtn btn = parse_action_btn(data);
m_actions.push_back(static_cast<int>(btn)); m_actions.push_back(static_cast<int>(btn));
m_sig.emit(action_begin{action{btn, value}}); m_sig.emit(action_begin{action{btn, value}});
// make sure we strip the correct length (btn+wrapping colons) // make sure we strip the correct length (btn+wrapping colons)
@ -159,7 +154,7 @@ void parser::codeblock(string data) {
/** /**
* Process text contents * Process text contents
*/ */
size_t parser::text(string data) { size_t parser::text(string&& data) {
uint8_t* utf = reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str())); uint8_t* utf = reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str()));
if (utf[0] < 0x80) { if (utf[0] < 0x80) {
@ -199,7 +194,7 @@ size_t parser::text(string data) {
/** /**
* Process color hex string and convert it to the correct value * Process color hex string and convert it to the correct value
*/ */
uint32_t parser::parse_color(string s, uint32_t fallback) { uint32_t parser::parse_color(const string& s, uint32_t fallback) {
uint32_t color{0}; uint32_t color{0};
if (s.empty() || s[0] == '-' || (color = color_util::parse(s, fallback)) == fallback) { if (s.empty() || s[0] == '-' || (color = color_util::parse(s, fallback)) == fallback) {
return fallback; return fallback;
@ -210,7 +205,7 @@ uint32_t parser::parse_color(string s, uint32_t fallback) {
/** /**
* Process font index and convert it to the correct value * Process font index and convert it to the correct value
*/ */
int8_t parser::parse_fontindex(string s) { int8_t parser::parse_fontindex(const string& s) {
if (s.empty() || s[0] == '-') { if (s.empty() || s[0] == '-') {
return -1; return -1;
} }
@ -239,7 +234,7 @@ attribute parser::parse_attr(const char attr) {
/** /**
* Process action button token and convert it to the correct value * Process action button token and convert it to the correct value
*/ */
mousebtn parser::parse_action_btn(string data) { mousebtn parser::parse_action_btn(const string& data) {
if (data[0] == ':') { if (data[0] == ':') {
return mousebtn::LEFT; return mousebtn::LEFT;
} else if (isdigit(data[0])) { } else if (isdigit(data[0])) {
@ -254,22 +249,21 @@ mousebtn parser::parse_action_btn(string data) {
/** /**
* Process action command string * Process action command string
*/ */
string parser::parse_action_cmd(const string& data) { string parser::parse_action_cmd(string&& data) {
size_t start{0}; if (data[0] != ':') {
while ((start = data.find(':', start)) != string::npos && data[start - 1] == '\\') {
start++;
}
if (start == string::npos) {
return ""; return "";
} }
size_t end{start + 1};
size_t end{1};
while ((end = data.find(':', end)) != string::npos && data[end - 1] == '\\') { while ((end = data.find(':', end)) != string::npos && data[end - 1] == '\\') {
end++; end++;
} }
if (end == string::npos) { if (end == string::npos) {
return ""; return "";
} }
return string_util::trim(data.substr(start, end), ':');
return data.substr(1, end - 1);
} }
POLYBAR_NS_END POLYBAR_NS_END