33 template <
class T> std::string
str(T x) {
42 inline bool IsNotalnum(
char x) {
return !std::isalnum(x);}
45 x.erase(std::remove_if(x.begin(), x.end(),
IsNotalnum), x.end());
49 inline std::string
trimWS(
const std::string& s)
51 int start=0, end=s.length()-1;
52 while (start<
int(s.length()) && isspace(s[start])) ++start;
53 while (end>=0 && isspace(s[end])) --end;
55 return s.substr(start,end-start+1);
64 std::string r; r.reserve(s.length());
65 for (
size_t i=0; i<s.length(); ++i)
68 else if (isspace(s[i]))
72 if (r.empty())
return "_";
88 template <
class T,
class V>
89 void remove(std::vector<T>& x,
const V& v)
90 {x.erase(
std::remove(x.begin(),x.end(),v),x.end());}
92 template <
class T,
class D>
94 {
return std::unique_ptr<T,D>(x,d);}
102 if ((x&0xF8) == 0xF0)
104 if ((x&0xF0) == 0xE0)
106 if ((x&0xE0) == 0xC0)
115 for (
size_t i=4; i>1; --i)
118 return index>0? index-1: 0;
136 for (
unsigned j=0; j<n; ++j)
149 char bom[4]=
"\0\0\0";
151 if (strcmp(bom,
"\357\273\277")==0)
return;
152 if (memcmp(bom,
"\0\0\376",3)==0)
153 if (s && s.get()==
'\377')
154 throw std::runtime_error(
"Only UTF-8 encoded files supported");
155 if (strncmp(bom,
"\376\377",2)==0 || strncmp(bom,
"\377\376",2)==0)
157 throw std::runtime_error(
"Only UTF-8 encoded files supported");
162 inline std::string
CSVQuote(
const std::string& x,
char sep)
165 bool needsQuoting=
false;
169 if (c==
'"') {r+=
'"'; needsQuoting=
true;}
170 if (c==sep) needsQuoting=
true;
172 if (needsQuoting)
return "\""+r+
"\"";
GetUtf8Char(std::string &c)
unsigned numBytes(unsigned char x)
a wrapper around std::ofstream that checks the write succeeded, throwing an exception if not ...
std::istream & operator>>(std::istream &i, const GetUtf8Char &g)
void stripNonAlnum(std::string &x)
arrange for a functional to be called on stack exit
void remove(std::vector< T > &x, const V &v)
remove an element from a vector. V must be comparable to a T
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky's state cha...
std::string trimWS(const std::string &s)
size_t prevIndex(const std::string &str, size_t index)
return index of previous character to index
std::string stripActive(const std::string &s)
repaces characters that cause interpretation by TCL, backslashes are replaced by the set minus operat...
OnStackExit< F > onStackExit(F f)
generator function
std::string str(T x)
utility function to create a string representation of a numeric type
std::unique_ptr< T, D > uniqueDeleter(T *x, D d)
std::string CSVQuote(const std::string &x, char sep)
quotes a string if it contains a separator character, and double quotes quotes
void stripByteOrderingMarker(std::istream &s)
checks if the input stream has the UTF-8 byte ordering marker, and removes it if present ...