You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

204 lines
5.9 KiB

#include "StringUtils.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
/**
* get an extension from a filename
* @param fileName a filename string
* @return '' or a string with the found extension
*/
const std::string getFilenameExtension(std::string const& fileName) {
auto dotPos = fileName.find_last_of('.');
if (dotPos != std::string::npos && dotPos + 1 != std::string::npos) {
//std::cout << "StringUtils::getFilenameExtension - " << fileName.substr(dotPos + 1) << std::endl;
return fileName.substr(dotPos + 1);
}
return "";
}
/**
* get an document from a url
* @param url url string
* @return '' or a string with the found document
*/
const std::string getDocumentFromURL(const std::string &url) {
int slashes = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
slashes++;
if (slashes == 3) {
return url.substr(i, url.length() - i);
}
}
}
return "";
}
/**
* get host from a url
* @param url url string
* @return '' or a string with the found host
*/
const std::string getHostFromURL(const std::string &url) {
auto colonDoubleSlash = url.find("://");
if (colonDoubleSlash != std::string::npos) {
auto startPos = colonDoubleSlash + 3;
auto thirdSlash = url.find("/", startPos);
if (thirdSlash == std::string::npos) {
return url.substr(startPos);
}
return url.substr(startPos, thirdSlash - startPos);
}
return "";
}
/**
* get scheme from a url
* @param url url string
* @return '' or a string with the found protocol
*/
const std::string getSchemeFromURL(const std::string &url) {
auto colonDoubleSlash = url.find("://");
if (colonDoubleSlash != std::string::npos) {
return url.substr(0, colonDoubleSlash);
}
return "";
}
/**
* convert string to lowercase
* @param str string
* @return lowercased version of str
*/
const std::string toLowercase(const std::string &str) {
std::string returnString = "";
std::transform(str.begin(),
str.end(),
back_inserter(returnString),
tolower);
return returnString;
/*
std::string stringToLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::tolower(c); });
return s;
*/
}
std::vector<std::string> split(const std::string &text, char sep) {
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
if (end != start) {
tokens.push_back(text.substr(start, end - start));
}
start = end + 1;
}
if (end != start) {
tokens.push_back(text.substr(start));
}
return tokens;
}
bool in_array(std::string needle, std::vector<std::string> haystack) {
for(auto it : haystack) {
if (it == needle) return true;
}
return false;
}
// FIXME: , in quotes or {} (JSON) <= top priority for 4chan
std::vector<std::string> parseSepButNotBetween(std::string string, std::string sep, std::vector<std::string> open, std::vector<std::string> close) {
std::vector<std::string> arr;
size_t mp = string.length();
std::string str3 = "";
size_t opens = 0;
size_t closes = 0;
for(int p = 0; p < mp; p++) {
auto next = string.find(sep, p); // find next comma after p
// if last string without a ,
if (next == std::string::npos) {
next = mp; // set to end
}
auto diff = next - p; // number of charaters to examine
std::string str2 = string.substr(p, diff); // get substring in this scan
for(auto o : open) {
opens += std::count(str2.begin(), str2.end(), o[0]);
}
for(auto c : close) {
closes += std::count(str2.begin(), str2.end(), c[0]);
}
str3 += str2;
bool flush = true;
if (opens != closes) {
str3 += sep;
flush = false;
}
if (flush) {
arr.push_back(str3);
str3 = "";
}
p += diff;
}
return arr;
}
/*
std::vector<std::string> parseCommas(std::string string, std::string sep, std::vector<std::string> quotes) {
std::vector<std::string> arr;
if (!quotes.size()) {
quotes.push_back("'");
quotes.push_back("\"");
}
size_t mp = string.length();
std::string str3 = "";
for(int p = 0; p < mp; p++) {
auto next = string.find(sep, p); // find next comma after p
// if last string without a ,
if (next == std::string::npos) {
next = mp; // set to end
}
auto diff = next - p; // number of charaters to examine
auto str2 = string.substr(p, diff); // get substring in this scan
auto open = str2.substr(0, 1);
str3 += str2;
bool flush = true;
if (in_array(open, quotes)) {
auto last = trim(str2).substr(-1, 1);
if (last!=open) {
str3 += ',';
flush = false;
}
}
if (flush) {
arr.push_back(str3);
str3 = "";
}
p += diff;
}
return arr;
}
size_t findMatching(std::string string, std::string opens, std::string closes, std::string escape="\\") {
// find open
auto opos = string.find(open);
if (opos == std::string::npos) {
return 0;
}
auto pos = opos;
while(pos != std::string::npos) {
auto cpos = string.find(close, pos+(open==close?1:0)); // don't want to end up with the opening marker as the start
if (escape!="") {
auto prevchar = string.substr(cpos - escape.length(), escape.length()); // get previous char before cpos
if (prevchar != escape) {
return cpos;
}
}
pos = cpos;
}
return 0;
}
*/