bool str_to_bool(const string &s)
{
+ if(s.empty())
+ throw LexicalError("Empty input in boolean conversion");
+
if(s=="1" || s=="true" || s=="yes" || s=="on")
return true;
else if(s=="0" || s=="false" || s=="no" || s=="off")
- return true;
+ return false;
+
throw LexicalError("Invalid input in boolean conversion");
}
/* $Id$
This file is part of libmspstrings
-Copyright © 2006-2007 Mikko Rasa
+Copyright © 2006-2008 Mikko Rasa
Distributed under the LGPL
*/
return result;
}
+bool check_str(const std::string &str, int (*pred)(int))
+{
+ for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+ if(!pred(*i))
+ return false;
+ return true;
+}
+
}
namespace Msp {
return result;
}
+bool isnumrc(const string &str)
+{
+ return check_str(str, isdigit);
+}
+
+bool isalpha(const string &str)
+{
+ return check_str(str, isalpha);
+}
+
+bool isalnum(const string &str)
+{
+ return check_str(str, isalnum);
+}
+
vector<string> split(const string &str, const string &sep, int max_split)
{
return do_split<false, false>(str, sep, max_split);
/* $Id$
This file is part of libmspstrings
-Copyright © 2006-2007 Mikko Rasa
+Copyright © 2006-2008 Mikko Rasa
Distributed under the LGPL
*/
*/
std::string toupper(const std::string &);
+/**
+Checks whether a string consists of digits only.
+*/
+bool isnumrc(const std::string &);
+
+/**
+Checks whether a string consists of alphabetic characters only.
+*/
+bool isalpha(const std::string &);
+
+/**
+Checks whether a string consists of alphanumeric characters only.
+*/
+bool isalnum(const std::string &);
+
+/* These are required to make the standard version work from inside the Msp
+namespace */
+using std::tolower;
+using std::toupper;
+using std::isalpha;
+using std::isalnum;
+
/**
Splits a string at occurrences of any of the characters in sep. If max_split
is non-negative, at most that many split will be performed, i.e. the resulting