nlib
misc/stringutils/stringutils.cpp

nn::nlib::StringViewnn::nlib::unicode::UnicodeNormalizer 等の利用方法が示されているサンプルです。

StringViewクラスを利用すると、std::stringが備えるメソッド(の大半)を文字列データの複製を伴わずに利用することができます。

UnicodeNormalizerクラスを利用すると、UTF-8の正規化を行うことができます。

/*---------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)2012-2016 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. 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.
*---------------------------------------------------------------------------*/
#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 (nlib_is_error(str.Proceed("count"))) {
// syntax error
return false;
}
str.TrimLeft();
if (nlib_is_error(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 (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 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