1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/include/utils/string.hpp

99 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
2016-05-19 10:41:06 -04:00
2016-06-14 23:32:35 -04:00
#include <sstream>
2017-01-13 05:09:56 -05:00
#include <cstring>
2016-05-19 10:41:06 -04:00
2016-06-14 23:32:35 -04:00
#include "common.hpp"
2016-05-19 10:41:06 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-05-19 10:41:06 -04:00
2017-01-13 05:09:56 -05:00
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 sstream {
2017-01-13 05:09:56 -05:00
public:
sstream() : m_stream() {}
2017-01-13 05:09:56 -05:00
template <typename T>
sstream& operator<<(const T& object) {
2017-01-13 05:09:56 -05:00
m_stream << object;
return *this;
}
sstream& operator<<(const char* cz) {
2017-01-13 05:09:56 -05:00
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;
};
2016-06-14 23:32:35 -04:00
namespace string_util {
/**
* Hash type
*/
using hash_type = unsigned long;
2016-11-02 15:22:45 -04:00
bool contains(const string& haystack, const string& needle);
string upper(const string& s);
string lower(const string& s);
bool compare(const string& s1, const string& s2);
2017-01-12 14:28:44 -05:00
2016-12-22 23:18:58 -05:00
string replace(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
size_t end = string::npos);
2016-12-22 23:18:58 -05:00
string replace_all(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
size_t end = string::npos);
2017-01-12 14:28:44 -05:00
2016-11-02 15:22:45 -04:00
string squeeze(const string& haystack, char needle);
2017-01-12 14:28:44 -05:00
2016-11-02 15:22:45 -04:00
string strip(const string& haystack, char needle);
string strip_trailing_newline(const string& haystack);
2017-01-12 14:28:44 -05:00
string ltrim(string&& value, const char& needle);
string rtrim(string&& value, const char& needle);
string trim(string&& value, const char& needle);
2017-01-12 14:28:44 -05:00
string join(const vector<string>& strs, const string& delim);
2016-11-25 07:55:15 -05:00
vector<string>& split_into(const string& s, char delim, vector<string>& container);
2016-11-02 15:22:45 -04:00
vector<string> split(const string& s, char delim);
2017-01-12 14:28:44 -05:00
2016-11-25 07:55:15 -05:00
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth);
2017-01-12 14:28:44 -05:00
string floating_point(double value, size_t precision, bool fixed = false, const string& locale = "");
string filesize_mb(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 = "");
2016-11-25 07:55:15 -05:00
hash_type hash(const string& src);
2016-05-19 10:41:06 -04:00
}
2016-06-14 23:32:35 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END