nlib
misc/stringutils/stringutils.cpp

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() {
nlib_printf("\nStringViewSample\n");
// with StringView, you can handle a string without copy or change.
char buf[128];
StringView str(" count = 12345 ");
str.ToCstring(buf);
nlib_printf("str: '%s'\n", buf);
str.Trim();
str.ToCstring(buf);
nlib_printf("str.Trim(): '%s'\n", buf);
if (!str.Proceed("count")) {
// syntax error
return false;
}
str.TrimLeft();
if (!str.Proceed('=')) {
// syntax error
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) {
// overflow or other errors
return false;
}
nlib_printf("Got integer '%d' from str\n", intval);
return true;
}
bool UnicodeNormalizerSample() {
nlib_printf("\nUnicodeNormalizerSample\n");
wchar_t text[] = L"un cafe\x0301, s'il vous plai\x0302t (a cup of coffee, please)";
char utf8[128];
errno_t e = nlib_wide_to_utf8(NULL, utf8, text);
if (e != 0) return false;
nlib_printf("Input Text is '%s'\n", utf8);
nlib_ns::MemoryInputStream istr(&utf8[0], strlen(utf8) + 1);
char normalized[128];
nlib_ns::MemoryOutputStream ostr(&normalized[0], 128);
// normalizes UTF-8 string.
// nlib supports unicode normalization in NFC, NFD, NFKC, NFKD.
// we use NFKC here.
UnicodeNormalizer::Normalize(&istr, &ostr, UnicodeNormalizer::NFKC);
ostr.Write('\0'); // write '\0' explicitly to use this as C string
ostr.Close();
// un café, s'il vous plaît (a cup of coffee, please)
nlib_printf("Normalized Text is '%s'\n", normalized);
return true;
}
bool SampleMain(int, char**) { return StringViewSample() && UnicodeNormalizerSample(); }
NLIB_MAINFUNC