Remove non-printable characters from a converted string

62 views Asked by At

I need to convert a unicode string represented by std::u16string to std::wstring and filter out nonprintable characters. The way I do this is as below. In our real project I use ranges for convenience, so I'll keep them here as well:

std::wstring printable = std::u16string{...}
                       | ranges::to<std::wstring>()
                       | ranges::actions::remove_if([](const wchar_t ch) noexcept {
                           return !std::iswprint(ch);
                         });

My colleague doubts if this is ok and there won't be any unicode characters falsely treated as printable. Is this approach correct? Maybe I should first remove non-printable characters from u16string and then convert it to wstring? Or maybe it is better to use some unicode-specific function on wstring instead of iswprint?

0

There are 0 answers