mirror of
https://github.com/polybar/polybar.git
synced 2024-11-18 13:55:11 -05:00
feat(string_util): Custom stringstream
This commit is contained in:
parent
6fb48c8e6f
commit
e1dbd98c40
7 changed files with 73 additions and 28 deletions
|
@ -23,7 +23,6 @@
|
||||||
POLYBAR_NS
|
POLYBAR_NS
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::stringstream;
|
|
||||||
using std::size_t;
|
using std::size_t;
|
||||||
using std::move;
|
using std::move;
|
||||||
using std::forward;
|
using std::forward;
|
||||||
|
|
|
@ -1,11 +1,61 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
|
|
||||||
POLYBAR_NS
|
POLYBAR_NS
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
/**
|
||||||
|
* Overload that allows sub-string removal at the end of given string
|
||||||
|
*/
|
||||||
|
inline string& operator-(string& a, const string& b) {
|
||||||
|
if (a.size() >= b.size() && a.substr(a.size() - b.size()) == b) {
|
||||||
|
return a.erase(a.size() - b.size());
|
||||||
|
} else {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload that allows sub-string removal at the end of given string
|
||||||
|
*/
|
||||||
|
inline void operator-=(string& a, const string& b) {
|
||||||
|
if (a.size() >= b.size() && a.substr(a.size() - b.size()) == b) {
|
||||||
|
a.erase(a.size() - b.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class stringstream {
|
||||||
|
public:
|
||||||
|
stringstream() : m_stream() {}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
stringstream& operator<<(const T& object) {
|
||||||
|
m_stream << object;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
stringstream& operator<<(const char* cz) {
|
||||||
|
m_stream << cz;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator string() const {
|
||||||
|
return m_stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
const string to_string() const {
|
||||||
|
return m_stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::stringstream m_stream;
|
||||||
|
};
|
||||||
|
|
||||||
namespace string_util {
|
namespace string_util {
|
||||||
/**
|
/**
|
||||||
* Hash type
|
* Hash type
|
||||||
|
@ -42,7 +92,6 @@ namespace string_util {
|
||||||
string filesize_gb(unsigned long long kbytes, size_t precision = 0, const string& locale = "");
|
string filesize_gb(unsigned long long kbytes, size_t precision = 0, const string& locale = "");
|
||||||
string filesize(unsigned long long bytes, size_t precision = 0, bool fixed = false, const string& locale = "");
|
string filesize(unsigned long long bytes, size_t precision = 0, bool fixed = false, const string& locale = "");
|
||||||
|
|
||||||
string from_stream(const std::basic_ostream<char>& os);
|
|
||||||
hash_type hash(const string& src);
|
hash_type hash(const string& src);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -193,8 +193,8 @@ namespace net {
|
||||||
suffixes.pop_back();
|
suffixes.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
return string_util::from_stream(stringstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0)
|
return stringstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate
|
||||||
<< std::fixed << speedrate << " " << suffix << "/s");
|
<< " " << suffix << "/s";
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
|
@ -24,7 +24,7 @@ http_downloader::~http_downloader() {
|
||||||
}
|
}
|
||||||
|
|
||||||
string http_downloader::get(const string& url) {
|
string http_downloader::get(const string& url) {
|
||||||
stringstream out{};
|
std::stringstream out{};
|
||||||
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &out);
|
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &out);
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ long http_downloader::response_code() {
|
||||||
|
|
||||||
size_t http_downloader::write(void* p, size_t size, size_t bytes, void* stream) {
|
size_t http_downloader::write(void* p, size_t size, size_t bytes, void* stream) {
|
||||||
string data{static_cast<const char*>(p), size * bytes};
|
string data{static_cast<const char*>(p), size * bytes};
|
||||||
*(static_cast<stringstream*>(stream)) << data << '\n';
|
*(static_cast<std::stringstream*>(stream)) << data << '\n';
|
||||||
return size * bytes;
|
return size * bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -161,7 +161,7 @@ namespace string_util {
|
||||||
*/
|
*/
|
||||||
vector<string>& split_into(const string& s, char delim, vector<string>& container) {
|
vector<string>& split_into(const string& s, char delim, vector<string>& container) {
|
||||||
string str;
|
string str;
|
||||||
stringstream buffer(s);
|
std::stringstream buffer(s);
|
||||||
while (getline(buffer, str, delim)) {
|
while (getline(buffer, str, delim)) {
|
||||||
container.emplace_back(str);
|
container.emplace_back(str);
|
||||||
}
|
}
|
||||||
|
@ -191,7 +191,7 @@ namespace string_util {
|
||||||
* Create a floating point string
|
* Create a floating point string
|
||||||
*/
|
*/
|
||||||
string floating_point(double value, size_t precision, bool fixed, const string& locale) {
|
string floating_point(double value, size_t precision, bool fixed, const string& locale) {
|
||||||
stringstream ss;
|
std::stringstream ss;
|
||||||
ss.imbue(!locale.empty() ? std::locale(locale.c_str()) : std::locale::classic());
|
ss.imbue(!locale.empty() ? std::locale(locale.c_str()) : std::locale::classic());
|
||||||
ss << std::fixed << std::setprecision(precision) << value;
|
ss << std::fixed << std::setprecision(precision) << value;
|
||||||
return fixed ? ss.str() : replace(ss.str(), ".00", "");
|
return fixed ? ss.str() : replace(ss.str(), ".00", "");
|
||||||
|
@ -225,18 +225,6 @@ namespace string_util {
|
||||||
return floating_point(value, precision, fixed, locale) + " GB";
|
return floating_point(value, precision, fixed, locale) + " GB";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the resulting string from a ostream/
|
|
||||||
*
|
|
||||||
* Example usage:
|
|
||||||
* @code cpp
|
|
||||||
* string_util::from_stream(stringstream() << ...);
|
|
||||||
* @endcode
|
|
||||||
*/
|
|
||||||
string from_stream(const std::basic_ostream<char>& os) {
|
|
||||||
return dynamic_cast<const stringstream&>(os).str();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute string hash
|
* Compute string hash
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -107,7 +107,7 @@ Visual* connection::visual(uint8_t depth) {
|
||||||
* Create X window id string
|
* Create X window id string
|
||||||
*/
|
*/
|
||||||
string connection::id(xcb_window_t w) const {
|
string connection::id(xcb_window_t w) const {
|
||||||
return string_util::from_stream(std::stringstream() << "0x" << std::hex << std::setw(7) << std::setfill('0') << w);
|
return stringstream() << "0x" << std::hex << std::setw(7) << std::setfill('0') << w;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -72,13 +72,6 @@ int main() {
|
||||||
expect(string_util::find_nth("foobarfoobar", 0, "o", 3) == size_t{7});
|
expect(string_util::find_nth("foobarfoobar", 0, "o", 3) == size_t{7});
|
||||||
};
|
};
|
||||||
|
|
||||||
"from_stream"_test = [] {
|
|
||||||
auto result =
|
|
||||||
string_util::from_stream(std::stringstream() << std::setw(6) << std::setfill('z') << "foo"
|
|
||||||
<< "bar");
|
|
||||||
expect(result == "zzzfoobar");
|
|
||||||
};
|
|
||||||
|
|
||||||
"hash"_test = [] {
|
"hash"_test = [] {
|
||||||
unsigned long hashA1{string_util::hash("foo")};
|
unsigned long hashA1{string_util::hash("foo")};
|
||||||
unsigned long hashA2{string_util::hash("foo")};
|
unsigned long hashA2{string_util::hash("foo")};
|
||||||
|
@ -106,4 +99,20 @@ int main() {
|
||||||
expect(string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024) == "4 GB");
|
expect(string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024) == "4 GB");
|
||||||
expect(string_util::filesize(3 * 1024 * 1024) == "3 GB");
|
expect(string_util::filesize(3 * 1024 * 1024) == "3 GB");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
"stringstream"_test = [] {
|
||||||
|
string s;
|
||||||
|
expect((s = (stringstream() << "test")) == "test"s);
|
||||||
|
expect((s = (stringstream() << std::setprecision(2) << std::fixed << 1.25)).erase(0, 2) == "25"s);
|
||||||
|
};
|
||||||
|
|
||||||
|
"operators"_test = [] {
|
||||||
|
string foo = "foobar";
|
||||||
|
expect(foo - "bar" == "foo");
|
||||||
|
string baz = "bazbaz";
|
||||||
|
expect(baz - "ba" == "bazbaz");
|
||||||
|
expect(baz - "bazbz" == "bazbaz");
|
||||||
|
string aaa = "aaa";
|
||||||
|
expect(aaa - "aaaaa" == "aaa");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue