Files
MagicSetEditor2/src/util/string.cpp
T
twanvl 156d1f6632 Automatic use of singular names -> even more use of REFLECT instead of REFLECT_N.
This also works better for GetMember, since the plural names (which make sense for scripts) are the default.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@16 0fc631ac-6414-0410-93d0-97cfa31319b6
2006-10-11 22:58:55 +00:00

142 lines
4.2 KiB
C++

//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include "string.hpp"
#include "for_each.hpp"
#include <wx/txtstrm.h>
// ----------------------------------------------------------------------------- : Unicode
String decodeUTF8BOM(const String& s) {
#ifdef UNICODE
if (!s.empty() && s.GetChar(0) == L'\xFEFF') {
// skip byte-order-mark
return s.substr(1);
} else {
return s;
}
#else
wxWCharBuffer buf = s.wc_str(wxConvUTF8);
if (buf && buf[size_t(0)] == L'\xFEFF') {
// skip byte-order-mark
return String(buf + 1, *wxConvCurrent);
} else {
return String(buf, *wxConvCurrent);
}
#endif
}
void writeUTF8(wxTextOutputStream& stream, const String& str) {
#ifdef UNICODE
stream.WriteString(str);
#else
wxWCharBuffer buf = str.wc_str(*wxConvCurrent);
stream.WriteString(wxString(buf, wxConvUTF8));
#endif
}
// ----------------------------------------------------------------------------- : String utilities
String trim(const String& s){
size_t start = s.find_first_not_of(_(" \t"));
size_t end = s.find_last_not_of( _(" \t"));
if (start == String::npos) {
return String();
} else {
return s.substr(start, end - start + 1);
}
}
String trim_left(const String& s) {
size_t start = s.find_first_not_of(_(' '));
if (start == String::npos) {
return String();
} else {
return s.substr(start);
}
}
// ----------------------------------------------------------------------------- : Words
String last_word(const String& s) {
size_t endLastWord = s.find_last_not_of(_(' '));
size_t startLastWord = s.find_last_of( _(' '), endLastWord);
if (endLastWord == String::npos) {
return String(); // empty string
} else if (startLastWord == String::npos) {
return s.substr(0, endLastWord + 1);// first word
} else {
return s.substr(startLastWord + 1, endLastWord - startLastWord);
}
}
String strip_last_word(const String& s) {
size_t endLastWord = s.find_last_not_of(_(' '));
size_t startLastWord = s.find_last_of(_(' '), endLastWord);
if (endLastWord == String::npos || startLastWord == String::npos) {
return String(); // single word or empty string
} else {
return s.substr(0, startLastWord + 1);
}
}
// ----------------------------------------------------------------------------- : Caseing
/// Quick check to see if the substring starting at the given iterator is equal
/// to some given string
bool is_substr(const String& s, String::iterator it, const Char* cmp) {
while (it != s.end() && *cmp != 0) {
if (*it++ != *cmp++) return false;
}
return *cmp == 0;
}
String capitalize(const String& s) {
String result = s;
bool afterSpace = true;
FOR_EACH_IT(it, result) {
if (*it == ' ') {
afterSpace = true;
} else if (afterSpace) {
afterSpace = false;
if (it != s.begin() &&
(is_substr(result,it,_("is ")) || is_substr(result,it,_("the ")) ||
is_substr(result,it,_("in ")) || is_substr(result,it,_("of ")) ||
is_substr(result,it,_("to ")) || is_substr(result,it,_("at ")) ||
is_substr(result,it,_("a " )))) {
// Short words are not capitalized, keep lower case
} else {
*it = toUpper(*it);
}
}
}
return result;
}
String cannocial_name_form(const String& str) {
String ret;
ret.reserve(str.size());
bool leading = true;
FOR_EACH_CONST(c, str) {
if ((c == _('_') || c == _(' ')) && !leading) {
ret += _('_');
} else if (isAlnum(c)) {
ret += toLower(c);
leading = false;
} else {
// ignore non alpha numeric
}
}
return ret;
}
String singular_form(const String& str) {
assert(str.size() > 1);
assert(str.GetChar(str.size() - 1) == _('s')); // ends in 's'
return str.substr(0, str.size() - 1);
}