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.

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
#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 ");
ToCstring(buf, str);
nlib_printf("str: '%s'\n", buf);
Trim(str);
ToCstring(buf, str);
nlib_printf("Trim(str): '%s'\n", buf);
if (nlib_is_error(Proceed(str, "count"))) {
// syntax error
return false;
}
TrimLeft(str);
if (nlib_is_error(Proceed(str, '='))) {
// syntax error
return false;
}
TrimLeft(str);
ToCstring(buf, str);
nlib_printf("token 'count' and '=' are taken, str is now '%s'\n", buf);
int32_t intval;
if (nlib_is_error(str.ToInteger(&intval))) {
// 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 (nlib_is_error(e)) 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 kNfc, kNfd, kNfkc, kNfkd.
// we use kNfkc here.
UnicodeNormalizer::Normalize(&istr, &ostr, UnicodeNormalizer::kNfkc);
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