string functions don't even look
This commit is contained in:
parent
cc14aa47e8
commit
3c32001e4f
2 changed files with 65 additions and 3 deletions
|
@ -32,3 +32,63 @@ std::string* sosc::str::rtrim(std::string *str) {
|
||||||
str->erase(str->length() - marker - 1, marker);
|
str->erase(str->length() - marker - 1, marker);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> sosc::str::split
|
||||||
|
(const std::string& str, char delimiter, int count)
|
||||||
|
{
|
||||||
|
std::string buffer;
|
||||||
|
std::stringstream stream(str);
|
||||||
|
auto parts = std::vector<std::string>();
|
||||||
|
|
||||||
|
--count;
|
||||||
|
while(count != 0) {
|
||||||
|
if(!std::getline(stream, buffer, delimiter))
|
||||||
|
break;
|
||||||
|
|
||||||
|
parts.push_back(buffer);
|
||||||
|
--count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(std::getline(stream, buffer))
|
||||||
|
parts.push_back(buffer);
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> sosc::str::split
|
||||||
|
(const std::string& str, std::string delimiter, int count)
|
||||||
|
{
|
||||||
|
auto parts = std::vector<std::string>();
|
||||||
|
std::size_t chunk_end = 0, chunk_start = 0;
|
||||||
|
|
||||||
|
--count;
|
||||||
|
while((chunk_end = str.find(delimiter, chunk_end))
|
||||||
|
!= std::string::npos && count != 0)
|
||||||
|
{
|
||||||
|
parts.push_back(str.substr(chunk_start, chunk_start - chunk_end));
|
||||||
|
chunk_start = (chunk_end += delimiter.length());
|
||||||
|
--count;
|
||||||
|
}
|
||||||
|
|
||||||
|
parts.push_back(str.substr(chunk_start, std::string::npos));
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string sosc::str::join(const std::vector<std::string>& parts,
|
||||||
|
char delimiter, int count)
|
||||||
|
{
|
||||||
|
return join(parts, std::string(1, delimiter), count);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string sosc::str::join(const std::vector<std::string>& parts,
|
||||||
|
std::string delimiter, int count)
|
||||||
|
{
|
||||||
|
std::string assembled;
|
||||||
|
int bounds = (count == -1)
|
||||||
|
? parts.size()
|
||||||
|
: std::min<int>(count, parts.size());
|
||||||
|
|
||||||
|
for(int i = 0; i < bounds; ++i)
|
||||||
|
assembled += (i == 0 ? "" : delimiter) + parts[i];
|
||||||
|
|
||||||
|
return assembled;
|
||||||
|
}
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace sosc {
|
namespace sosc {
|
||||||
namespace str {
|
namespace str {
|
||||||
std::string trim (std::string str);
|
std::string trim (std::string str);
|
||||||
std::string* trim (std::string* str);
|
std::string* trim (std::string* str);
|
||||||
|
|
||||||
|
@ -20,9 +22,9 @@ std::vector<std::string> split
|
||||||
std::vector<std::string> split
|
std::vector<std::string> split
|
||||||
(const std::string& str, std::string delimiter, int count = -1);
|
(const std::string& str, std::string delimiter, int count = -1);
|
||||||
|
|
||||||
std::string join(const std::vector<std::string>& strs,
|
std::string join(const std::vector<std::string>& parts,
|
||||||
char delimiter, int count = -1);
|
char delimiter, int count = -1);
|
||||||
std::string join(const std::vector<std::string>& strs,
|
std::string join(const std::vector<std::string>& parts,
|
||||||
std::string delimiter, int count = -1);
|
std::string delimiter, int count = -1);
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue