diff options
author | Kevin Ballard <kevin@sb.org> | 2014-09-21 19:18:56 -0700 |
---|---|---|
committer | Kevin Ballard <kevin@sb.org> | 2014-09-21 19:27:26 -0700 |
commit | 8f8c4cdd176fda4d93a2d1d4b0ae6321d5706e5f (patch) | |
tree | 68c2f6d4eb2bbd7a4b975b7901cc0db95f83b740 /wcstringutil.cpp | |
parent | f889ad0fda9bf8d1f354cad37d508e0c4205af48 (diff) |
Implement new `read --null` flag
The `--null` flag to `read` makes it split incoming lines on NUL instead
of newlines. This is intended for processing the output of a command
that uses NUL separators (such as `find -print0`).
Fixes #1694.
Diffstat (limited to 'wcstringutil.cpp')
-rw-r--r-- | wcstringutil.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/wcstringutil.cpp b/wcstringutil.cpp new file mode 100644 index 00000000..51ec1a1c --- /dev/null +++ b/wcstringutil.cpp @@ -0,0 +1,40 @@ +/** \file wcstringutil.cpp + +Helper functions for working with wcstring +*/ + +#include "config.h" + +#include "wcstringutil.h" + +typedef wcstring::size_type size_type; + +wcstring_range wcstring_tok(wcstring& str, const wcstring &needle, wcstring_range last) +{ + size_type pos = last.second == wcstring::npos ? wcstring::npos : last.first; + if (pos != wcstring::npos && last.second != wcstring::npos) pos += last.second; + if (pos != wcstring::npos && pos != 0) ++pos; + if (pos == wcstring::npos || pos >= str.size()) + { + return std::make_pair(wcstring::npos, wcstring::npos); + } + + if (needle.empty()) + { + return std::make_pair(pos, wcstring::npos); + } + + pos = str.find_first_not_of(needle, pos); + if (pos == wcstring::npos) return std::make_pair(wcstring::npos, wcstring::npos); + + size_type next_pos = str.find_first_of(needle, pos); + if (next_pos == wcstring::npos) + { + return std::make_pair(pos, wcstring::npos); + } + else + { + str[next_pos] = L'\0'; + return std::make_pair(pos, next_pos - pos); + } +} |