This sample demonstrates the use of objects, including nn::nlib::StringView
and nn::nlib::unicode::UnicodeNormalizer
.
By using the StringView
class, you can use the majority of the std:: string
member functions without making duplicates of the string data.
Use the UnicodeNormalizer
class for UTF-8 normalization.
#include <string.h>
using nlib_ns::unicode::UnicodeNormalizer;
bool StringViewSample() {
char buf[128];
StringView str(" count = 12345 ");
str.ToCstring(buf);
str.Trim();
str.ToCstring(buf);
if (!str.Proceed("count")) {
return false;
}
str.TrimLeft();
if (!str.Proceed('=')) {
return false;
}
str.TrimLeft();
str.ToCstring(buf);
nlib_printf(
"token 'count' and '=' are taken, str is now '%s'\n", buf);
int32_t intval;
if (str.ToInteger(&intval) != 0) {
return false;
}
return true;
}
bool UnicodeNormalizerSample() {
wchar_t text[] = L"un cafe\x0301, s'il vous plai\x0302t (a cup of coffee, please)";
char utf8[128];
if (e != 0) return false;
char normalized[128];
UnicodeNormalizer::Normalize(&istr, &ostr, UnicodeNormalizer::NFKC);
ostr.Write('\0');
ostr.Close();
return true;
}
bool SampleMain(int, char**) { return StringViewSample() && UnicodeNormalizerSample(); }
NLIB_MAINFUNC