| 1 |
#ifndef SOURCETOOLS_MULTIBYTE_MULTIBYTE_H |
| 2 |
#define SOURCETOOLS_MULTIBYTE_MULTIBYTE_H |
| 3 |
|
| 4 |
#include <cstdlib> |
| 5 |
#include <cwchar> |
| 6 |
|
| 7 |
namespace sourcetools { |
| 8 |
namespace multibyte { |
| 9 |
|
| 10 |
template <typename T> |
| 11 |
inline bool countWhitespaceBytes(const char* data, |
| 12 |
T* pBytes) |
| 13 |
{ |
| 14 |
wchar_t ch; |
| 15 |
T bytes = 0; |
| 16 |
const char* it = data; |
| 17 |
|
| 18 |
while (true) { |
| 19 |
|
| 20 |
int status = std::mbtowc(&ch, it, MB_CUR_MAX); |
| 21 |
if (status == 0) { |
| 22 |
break; |
| 23 |
} else if (status == -1) { |
| 24 |
break; |
| 25 |
} |
| 26 |
|
| 27 |
if (!std::iswspace(ch)) |
| 28 |
break; |
| 29 |
|
| 30 |
bytes += status; |
| 31 |
it += status; |
| 32 |
} |
| 33 |
|
| 34 |
*pBytes = bytes; |
| 35 |
return bytes != 0; |
| 36 |
} |
| 37 |
|
| 38 |
} // namespace multibyte |
| 39 |
} // namespace sourcetools |
| 40 |
|
| 41 |
#endif /* SOURCETOOLS_MULTIBYTE_MULTIBYTE_H */ |