2016-04-02 21:50:40 +00:00
|
|
|
#ifndef INCLUDED_FORMAT_H
|
|
|
|
#define INCLUDED_FORMAT_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2016-04-02 22:08:25 +00:00
|
|
|
#include <cstdint>
|
2016-06-02 03:14:46 +00:00
|
|
|
#include <vector>
|
2016-04-02 21:50:40 +00:00
|
|
|
|
|
|
|
namespace Util
|
|
|
|
{
|
|
|
|
class Format
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
Format &operator<<(const T &data)
|
|
|
|
{
|
|
|
|
m_stream << data;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::string() const
|
|
|
|
{
|
|
|
|
return str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str() const
|
|
|
|
{
|
|
|
|
return m_stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Format &Join(const T &collection)
|
|
|
|
{
|
|
|
|
std::string separator = m_stream.str();
|
|
|
|
clear();
|
|
|
|
for (auto it = collection.begin(); it != collection.end(); )
|
|
|
|
{
|
|
|
|
m_stream << *it;
|
|
|
|
++it;
|
|
|
|
if (it != collection.end())
|
|
|
|
m_stream << separator;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-02 03:14:46 +00:00
|
|
|
|
|
|
|
std::vector<std::string> Split(char separator)
|
|
|
|
{
|
|
|
|
// Very inefficient: lots of intermediate string copies!
|
|
|
|
std::string str = m_stream.str();
|
|
|
|
const char *start = str.c_str();
|
|
|
|
const char *end = start;
|
|
|
|
std::vector<std::string> parts;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (*end == separator || !*end)
|
|
|
|
{
|
|
|
|
size_t len = end - start;
|
|
|
|
if (len)
|
|
|
|
parts.emplace_back(start, len);
|
|
|
|
else
|
|
|
|
parts.emplace_back();
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
++end;
|
|
|
|
} while (end[-1]);
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2016-04-02 21:50:40 +00:00
|
|
|
Format(const std::string &str)
|
|
|
|
: m_stream(str)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Format()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::stringstream m_stream;
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_stream.str(std::string());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-05 01:15:58 +00:00
|
|
|
std::string TrimWhiteSpace(const std::string &str);
|
|
|
|
std::string Hex(uint32_t n, size_t num_digits);
|
|
|
|
std::string Hex(uint32_t n);
|
|
|
|
std::string Hex(uint16_t n);
|
|
|
|
std::string Hex(uint8_t n);
|
2016-04-02 21:50:40 +00:00
|
|
|
} // Util
|
|
|
|
|
|
|
|
#endif // INCLUDED_FORMAT_H
|