Page MenuHomePhorge

StringUtils.cpp
No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None

StringUtils.cpp

#include <echo/Util/StringUtils.h>
#include <echo/UTF8String.h>
namespace Echo
{
namespace Utils
{
namespace String
{
bool SplitInTwo(const std::string& stringToSplit, std::string separators, std::string& left, std::string& right)
{
std::string temp;
size_t divider=stringToSplit.find_first_of(separators);
//If a divider isn't found then we've failed to split.
if(divider==std::string::npos)
{
return false;
}
//No failure means we have at least left.
left=stringToSplit.substr(0,divider);
//if the divider was at the end of the string then right will be empty.
if(divider+1==stringToSplit.length())
{
//Make sure it is empty.
right.clear();
}else
{
//Otherwise, right is the divider+1 to the end of the string.
right=stringToSplit.substr(divider+1);
}
return true;
}
bool SplitInTwo(const std::string& stringToSplit, std::string separators, std::string& left, UTF8String& right)
{
std::string rightTemp;
bool result=SplitInTwo(stringToSplit, separators, left, rightTemp);
if(result)
{
right=rightTemp;
}
return result;
}
void Split(const std::string& stringToSplit, std::string separators, std::vector<std::string> &outStrings)
{
std::string temp;
outStrings.clear();
for(size_t i=0; i < stringToSplit.size(); ++i)
{
if(separators.find_first_of(stringToSplit[i])!=std::string::npos)
{
outStrings.push_back(temp);
temp="";
continue;
}
temp+=stringToSplit[i];
}
if(!temp.empty())
{
outStrings.push_back(temp);
}
}
std::string SplitLine(const std::string& line, std::string lineSeparators, std::string rightPartSeparators, std::vector<std::string>& splitOut)
{
size_t firstEq=line.find_first_of(lineSeparators);
if(firstEq==std::string::npos)
return line;
std::string token=line.substr(0,firstEq);
std::string ds;
ds=line.substr(firstEq+1,line.length()-firstEq);
//split paramB up with ,
size_t comma=0;
comma=ds.find_first_of(rightPartSeparators);
while(comma!=std::string::npos)
{
comma=ds.find_first_of(rightPartSeparators);
if(comma==std::string::npos)
continue;
if(comma==ds.length())
{
comma=std::string::npos;
continue;
}
splitOut.push_back( ds.substr(0,comma) );
ds=ds.substr(comma+1,ds.length()-comma);
}
if(!ds.empty())
{
splitOut.push_back(ds);
}
return token;
}
std::string GetFileExtension(const std::string& fileName)
{
size_t slash = fileName.find_last_of('/');
if(slash!=std::string::npos)
{
return GetFileExtension(fileName.substr(slash+1));
}
size_t dot = fileName.find_last_of('.');
//If there is no '.' or it is the last character.
if(dot==std::string::npos || dot==fileName.size()-1)
{
return std::string();
}
//Otherwise
return fileName.substr(dot+1);
}
std::string GetPathFromFilename(const std::string& fileName)
{
size_t slash = fileName.find_last_of('/');
if(slash==std::string::npos)
{
return std::string();
}
//Otherwise
return fileName.substr(0,slash+1);
}
}
}
}

File Metadata

Mime Type
text/x-c++
Expires
Thu, Dec 5, 2:05 AM (6 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
63571
Default Alt Text
StringUtils.cpp (3 KB)

Event Timeline