How to parse in c++ list of null-terminated Unicode strings where the list is terminated with two NULL characters?
There's this little example on Raymond Chen's blog (which, perhaps not surprisingly, is the first find in Google for "double null terminated string"):
This reinterpretation of a double-null-terminated string as really a list of strings with an empty string as the terminator makes writing code to walk through a double-null-terminated string quite straightforward:
> for (LPTSTR pszz = pszzStart; *pszz; pszz += lstrlen(pszz) + 1) { ...
> do something with pszz ... }
The LPTSTR and lstrlen are wrappers which change depending on whether or not _UNICODE is set.
You simply build a list of strings and abort when one is empty:
std::vector<std::string> result;
result.push_back( std::string() );
while (std::cin) {
char c = std::cin.get();
if ( c == 0 ) {
if ( result.back().empty() ) { result.pop_back(); return; }
else result.push_back(std::string()); }
} else {
result.back().push_back(c);
}
}
精彩评论