/* $Id$
This file is part of libmspstrings
-Copyright © 2006-2007 Mikko Rasa
+Copyright © 2006-2008 Mikko Rasa
Distributed under the LGPL
*/
+#include <algorithm>
#include <list>
#include <msp/core/except.h>
#include "utils.h"
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);
{
vector<string> result;
- unsigned start=0;
+ string::size_type start=0;
if(!allow_empty)
start=str.find_first_not_of(sep);
while(start<str.size())
{
- unsigned end=str.find_first_of(sep, start);
+ string::size_type end=str.find_first_of(sep, start);
result.push_back(str.substr(start, end-start));
if(end==string::npos)
string result;
for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
{
- if(numeric_type==1)
+ if(numeric_type==16)
{
unsigned digit=0;
if(*i>='0' && *i<='9')
numeric_type=0;
}
}
- else if(numeric_type==2)
+ else if(numeric_type==8)
{
unsigned digit=0;
if(*i>='0' && *i<='7')
else
throw InvalidParameterValue("Invalid octal digit");
- if(numeric_pos==0 && digit>3)
- throw InvalidParameterValue("Octal escape sequence must start with [0-3]");
-
numeric_value=(numeric_value<<3 | digit);
++numeric_pos;
- if(numeric_pos==2)
+ if(numeric_pos==3)
{
result+=numeric_value;
numeric_type=0;
}
else if(escape)
{
- if(*i=='x' || (*i>='0' && *i<='7'))
+ if(*i=='x')
{
- numeric_type=(*i=='x' ? 1 : 2);
+ numeric_type=16;
numeric_pos=0;
numeric_value=0;
}
+ else if(*i>='0' && *i<='3')
+ {
+ numeric_type=8;
+ numeric_pos=1;
+ numeric_value=*i-'0';
+ }
else if(*i=='n')
result+='\n';
else if(*i=='t')
result+="\\\\";
else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
{
- char buf[4]={'\\', '0'+((*i>>6)&7), '0'+((*i>>3)&7), '0'+(*i&7)};
+ char buf[4]={'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)};
result.append(buf, 4);
}
else