112 lines
1.9 KiB
C++
112 lines
1.9 KiB
C++
/**
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/string.h>
|
|
#include <sstream>
|
|
|
|
#define O_MINUTES (60)
|
|
#define O_HOURS (60 * O_MINUTES)
|
|
#define O_DAYS (24 * O_HOURS)
|
|
|
|
const std::string string :: utos(unsigned int u)
|
|
{
|
|
std::stringstream ss;
|
|
ss << u;
|
|
return ss.str();
|
|
}
|
|
|
|
const std::string string :: sec2str(unsigned int sec)
|
|
{
|
|
std::stringstream ss;
|
|
unsigned int minutes = sec / 60;
|
|
unsigned int seconds = sec % 60;
|
|
|
|
ss << minutes << ":";
|
|
if (seconds < 10)
|
|
ss << "0";
|
|
ss << seconds;
|
|
return ss.str();
|
|
}
|
|
|
|
static const std::string _time_detail(unsigned int value,
|
|
unsigned int remaining,
|
|
const std::string &field)
|
|
{
|
|
std::string res;
|
|
|
|
if (value > 0) {
|
|
res += string :: utos(value) + " " + field;
|
|
if (value > 1)
|
|
res += "s";
|
|
if (remaining > 0)
|
|
res += ", ";
|
|
}
|
|
return res;
|
|
}
|
|
|
|
const std::string string :: sec2str_detailed(unsigned int sec)
|
|
{
|
|
std::string res;
|
|
unsigned int factor[3] = { O_DAYS, O_HOURS, O_MINUTES };
|
|
std::string field[3] = { "day", "hour", "minute" };
|
|
unsigned int val;
|
|
|
|
for (unsigned int i = 0; i < 3; i++) {
|
|
val = sec / factor[i];
|
|
sec %= factor[i];
|
|
res += _time_detail(val, sec, field[i]);
|
|
}
|
|
|
|
res += _time_detail(sec, 0, "second");
|
|
return res;
|
|
}
|
|
|
|
static char _to_lower(char c)
|
|
{
|
|
if ((c >= 'a') && (c <= 'z'))
|
|
return c;
|
|
if ((c >= '0') && (c <= '9'))
|
|
return c;
|
|
if ((c >= 'A') && (c <= 'Z'))
|
|
return c + ('a' - 'A');
|
|
|
|
switch (c) {
|
|
case '\\':
|
|
case '/':
|
|
case ',':
|
|
case ';':
|
|
case '(':
|
|
case ')':
|
|
case '_':
|
|
case '-':
|
|
case '~':
|
|
case '+':
|
|
case '"':
|
|
case ' ':
|
|
case ' ':
|
|
return ' ';
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
const std::string string :: lowercase(const std::string &str)
|
|
{
|
|
std::string ret;
|
|
char c, last = ' ';
|
|
|
|
for (unsigned int i = 0; i < str.size(); i++) {
|
|
c = _to_lower(str[i]);
|
|
|
|
if (c > 0) {
|
|
if ((c != ' ') || (last != ' '))
|
|
ret += c;
|
|
last = c;
|
|
}
|
|
}
|
|
|
|
if (ret[ret.size() - 1] == ' ')
|
|
return ret.substr(0, ret.size() - 1);
|
|
return ret;
|
|
}
|