2018-03-11 07:57:57 +00:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
|
|
|
namespace StringUtils{
|
|
|
|
|
2018-03-29 14:09:58 +00:00
|
|
|
bool stringEndsWith(std::string const &fullString, std::string const &ending){
|
|
|
|
if (fullString.length() >= ending.length()) {
|
|
|
|
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 07:57:57 +00:00
|
|
|
std::string toString(float val){
|
|
|
|
float decimal = std::fmod(val, 1.0f);
|
|
|
|
int places = 1;
|
|
|
|
while (std::fmod(decimal * 10, 1.0f) > 0){
|
|
|
|
decimal *= 10;
|
|
|
|
places++;
|
|
|
|
}
|
|
|
|
char buffer[50];
|
|
|
|
std::string arg = "%."+std::to_string(places)+"f";
|
|
|
|
sprintf(buffer, arg.c_str(), val);
|
|
|
|
std::string s = buffer;
|
2018-03-29 14:09:58 +00:00
|
|
|
if (stringEndsWith(s, ".0")){
|
|
|
|
while (s.find('.') != std::string::npos){
|
|
|
|
s.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 07:57:57 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:29:10 +00:00
|
|
|
void printVector(std::vector<std::string> &vect){
|
|
|
|
std::printf("Vector of %ld elements:\n", vect.size());
|
|
|
|
int c = 0;
|
|
|
|
for (std::string s : vect){
|
|
|
|
std::printf("\t[%d] = %s\n", c, s.c_str());
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 07:57:57 +00:00
|
|
|
}
|