Quote:Original post by Antheus
0) which language? The rest assumes C++.
Quote:Original post by Topic Title
how do I know if std::string really empty?
Quote:Original post by Antheus
0) which language? The rest assumes C++.
Quote:Original post by Topic Title
how do I know if std::string really empty?
#include <vector>#include <string>#include <iostream>#include <boost/algorithm/string/split.hpp>#include <boost/algorithm/string/trim.hpp>std::string parse(const std::string & line) { using namespace std; using namespace boost::algorithm; vector<string> result; split(result, line, is_any_of("\'")); trim(result.front()); return result.front();}void print(const std::string & s, bool test) { std::string copy = parse(s); std::cout << (test == copy.empty() ? "pass" : "fail") << " : "; std::cout << (copy.empty() ? "String is empty" : copy) << "\n";}int main() { print("", true); print("Hello World", false); print("sub foo() \' comment", false); print("\' full line comment", true); print("\t\t ' \t mixed bag", true);}
Quote:Original post by Antheus
From this description it sounds like you are trying to parse Visual Basic source code.
The simplest solution would be to use boost's function.
Quote:Original post by phresnel
Your code is buggy, at least you should have used the functions from cctype.
Quote:
Further, I guess monchito is looking for a function that checks whether a string is made of zero or more whitespace, and not for a string that does not include any of [a-zA-Z0-9], so your code is wrong by principle. E.g. it fails for "!", for which you return true, but where the OP wants false.
Quote:
an empty srting when there are no
digits or alpha characters
Quote:Original post by cache_hit
IMO you should write the following functions, as they are extremely useful:
*** Source Snippet Removed ***
Then you can write your function like this:
*** Source Snippet Removed ***
Plus, you can use these two generic template functions for just about anything.
Quote:Original post by visitorQuote:Original post by cache_hit
IMO you should write the following functions, as they are extremely useful:
*** Source Snippet Removed ***
Then you can write your function like this:
*** Source Snippet Removed ***
Plus, you can use these two generic template functions for just about anything.
Indeed a very good idea, but you fall for the same thing as I did initially: isspace is overloaded (one version taking std::locale if I'm not mistaken). So the function pointer needs to be stored in a variable, or casted.
struct is_space{ bool operator()(char c) const { return std::isspace(c); } bool operator()(wchar_t c) const { return std::iswspace(c); }};std::string str;std::wstring wstr;if (!every(str.begin(), str.end(), is_space()){}if (!every(wstr.begin(), wstr.end(), is_space()){}
// Attempt to read from a stream constructed from the string into a local// string. Since this attempts to read a "word", the string contains// non-whitespace if and only if this is successful. We test for success using// the implicit bool conversion of the stream.bool contains_non_whitespace(const std::string& str) { std::istringstream ss(str); std::string non_whitespace; return ss >> non_whitespace;}// This doesn't work with temporaries because neither the stringstream nor the// local string are const. The resulting error messages can be quite confusing.
Quote:Original post by cache_hitQuote:Original post by Antheus
From this description it sounds like you are trying to parse Visual Basic source code.
The simplest solution would be to use boost's function.
Well, if he's really trying to parse Visual Basic source code, then Boost.Spirit2 would be even better as it's a complete grammar parser.