From: Mikko Rasa Date: Fri, 16 Mar 2007 21:57:47 +0000 (+0000) Subject: Add utils X-Git-Tag: strings-1.0~26 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4ec2cf9b91a9918f02990c40145bb39a87b0e2bb;p=libs%2Fcore.git Add utils Fix a few bugs in Fmt --- diff --git a/source/fmt.cpp b/source/fmt.cpp index c9f5d6c..b476c36 100644 --- a/source/fmt.cpp +++ b/source/fmt.cpp @@ -32,7 +32,7 @@ void Fmt::apply(ostream &out) const { out.flags(((base==HEX) ? ios_base::hex : (base==OCT) ? ios_base::oct : ios_base::dec) | ((fmode==SCI) ? ios_base::scientific : (fmode==FIXED) ? ios_base::fixed : ios_base::fmtflags(0)) - | (fillc ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right) + | (fillc=='0' ? ios_base::internal : (align==LEFT) ? ios_base::left : ios_base::right) | (sbase ? ios_base::showbase : ios_base::fmtflags(0)) | (spoint ? ios_base::showpoint : ios_base::fmtflags(0)) | (spos ? ios_base::showpos : ios_base::fmtflags(0)) @@ -93,11 +93,11 @@ void Fmt::parse(const char *f) else if(*f=='o') base=OCT; else if(*f=='e' || *f=='E') - fmode=EXP; + fmode=SCI; else if(*f=='f' || *f=='F') fmode=FIXED; else if(*f=='g' || *f=='G') - fmode=SCI; + fmode=EXP; else if(*f=='p') { base=HEX; diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..3ef7b79 --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,116 @@ +#include +#include "utils.h" + +using namespace std; + +namespace Msp { + +/** +Compares two strings, ignoring case. + +@param s1 First string +@param s2 Second string + +@return -1 if s1s2 +*/ +int strcasecmp(const string &s1, const string &s2) +{ + string::const_iterator i1=s1.begin(); + string::const_iterator i2=s2.begin(); + for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2) + { + const char c1=::tolower(*i1); + const char c2=::tolower(*i2); + if(c1!=c2) return c1-c2; + } + if(i1!=s1.end()) return *i1; + if(i2!=s2.end()) return -*i2; + return 0; +} + +/** +Returns a lowercase copy of the given string. +*/ +string tolower(const string &str) +{ + string result(str); + transform(result.begin(), result.end(), result.begin(), ::tolower); + return result; +} + +/** +Returns an uppercase copy of the given string. +*/ +string toupper(const string &str) +{ + string result(str); + transform(result.begin(), result.end(), result.begin(), ::toupper); + return result; +} + +/** +Splits a string to parts. + +@param str String to be split +@param sep A set of separator characters +@param allow_empty Whether or not to produce empty parts for sequences of + more than one separator character +*/ +vector split(const string &str, const string &sep, bool allow_empty) +{ + vector result; + unsigned start=str.find_first_not_of(sep); + while(start split(const string &str, char sep, bool allow_empty) +{ + return split(str, string(1,sep), allow_empty); +} + +/** +Builds a single string from the strings in the given sequence by concatenating +them. + +@param seq A sequence of strings +@param sep Separator to be inserted between strings +*/ +template +string join(const T &seq, const string &sep) +{ + string result; + for(typename T::const_iterator i=seq.begin(); i!=seq.end(); ++i) + { + if(i!=seq.begin()) + result+=sep; + result+=*i; + } + + return result; +} +template string join >(const list &, const string &); +template string join >(const vector &, const string &); + +/** +Returns a copy of the given string with leading and trailing whitespace +removed. +*/ +string strip(const string &s) +{ + string result=s; + if(!result.erase(0, result.find_first_not_of(" \t\n")).empty()) + result.erase(result.find_last_not_of(" \t\n")+1); + return result; +} + +} // namespace Msp diff --git a/source/utils.h b/source/utils.h new file mode 100644 index 0000000..1823f66 --- /dev/null +++ b/source/utils.h @@ -0,0 +1,20 @@ +#ifndef MSP_STRINGS_UTILS_H_ +#define MSP_STRINGS_UTILS_H_ + +#include +#include + +namespace Msp { + +int strcasecmp(const std::string &, const std::string &); +std::string tolower(const std::string &); +std::string toupper(const std::string &); +std::vector split(const std::string &, const std::string & =" \t\r\n", bool =false); +std::vector split(const std::string &, char, bool =false); +template +std::string join(const T &, const std::string & =" "); +std::string strip(const std::string &); + +} // namespace Msp + +#endif