String.h

今までのポストのタイトルは Poco のクラス名だったのですが、今回は String.h にある幾つかのテンプレート関数を紹介しようと思うので、タイトルがヘッダファイル名となりました。

Template Function Description
trimLeft() 左の空白文字削除 元文字列不変
trimLeftInPlace() 元文字列変更
trimRight() 右の空白文字削除 元文字列不変
trimRightInPlace() 元文字列変更
trim() 左右の空白文字削除 元文字列不変
trimInPlace() 元文字列変更
toUpper() 大文字に変換 元文字列不変
toUpperInPlace() 元文字列変更
toLower() 小文字に変換 元文字列不変
toLowerInPlace() 元文字列変更
icompare() 大文字/小文字を無視して比較
translate() まとめて置換 元文字列不変
translateInPlace() 元文字列変更
replace() 置換 元文字列不変
replaceInPlace() 元文字列変更
cat() 結合

StringTest.cpp

・*InPlace() は、元の文字列が変化する。

#include <Poco/Format.h>
#include <Poco/String.h>
 
#include <string>
 
#include "ScopedLogMessage.h"
#include "PrepareConsoleLogger.h"
 
void TestTrimLeft(void)
{
	ScopedLogMessage msg("TestTrimLeft ", "start", "end\n");
 
	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trimLeft(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimLeft(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trimLeftInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimLeftInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestTrimRight(void)
{
	ScopedLogMessage msg("TestTrimRight ", "start", "end\n");
 
	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trimRight(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimRight(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trimRightInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimRightInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestTrim(void)
{
	ScopedLogMessage msg("TestTrim ", "start", "end\n");
 
	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trim(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trim(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  trimInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::trimInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestToUpper(void)
{
	ScopedLogMessage msg("TestToUpper ", "start", "end\n");
 
	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  toUpper(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toUpper(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  toUpperInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toUpperInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestToLower(void)
{
	ScopedLogMessage msg("TestToLower ", "start", "end\n");
 
	std::string str(" a b c ");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  toLower(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toLower(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  toLowerInPlace(\"%s\"):", str));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::toLowerInPlace(str)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestICompare(void)
{
	ScopedLogMessage msg("TestICompare ", "start", "end\n");
 
	std::string str1(" A B C ");
	std::string str2(" a b c ");
	msg.Message(Poco::format("    source  string 1: \"%s\"", str1));
	msg.Message(Poco::format("    source  string 2: \"%s\"", str2));
 
	msg.Message(Poco::format("  iCompare(\"%s\", \"%s\"):", str1, str2));
	msg.Message(Poco::format("    result: \"%s\"", std::string(Poco::icompare(str1, str2) ? "false":"true")));
}
 
void TestTranslate(void)
{
	ScopedLogMessage msg("TestTranslate ", "start", "end\n");
 
	std::string str(" a b c ");
	std::string from("abc");
	std::string to("ABC");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  translate(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::translate(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  translateInPlace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::translateInPlace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestReplace(void)
{
	ScopedLogMessage msg("TestReplace ", "start", "end\n");
 
	std::string str("The quick brown fox jumps over the lazy dog.");
	std::string from("dog");
	std::string to("cat");
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  replace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::replace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
 
	msg.Message(Poco::format("  replaceInPlace(\"%s\", \"%s\", \"%s\"):", str, from, to));
	msg.Message(Poco::format("    changed string: \"%s\"", Poco::replaceInPlace(str, from, to)));
	msg.Message(Poco::format("    source  string: \"%s\"", str));
}
 
void TestCat(void)
{
	ScopedLogMessage msg("TestCat ", "start", "end\n");
 
	const char* strs[] = {	"Pack "
			,	"my box "
			,	"with "
			,	"five dozen "
			,	"liquor "
			,	"jugs."};
 
	msg.Message(Poco::format("  cat(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\")",
					std::string(strs[0])
				,	std::string(strs[1])
				,	std::string(strs[2])
				,	std::string(strs[3])
				,	std::string(strs[4])
				,	std::string(strs[5])));
	msg.Message(Poco::format("    combined string: \"%s\"",
				Poco::cat(
					std::string(strs[0])
				,	std::string(strs[1])
				,	std::string(strs[2])
				,	std::string(strs[3])
				,	std::string(strs[4])
				,	std::string(strs[5]))));
}
 
typedef void (*TestProc)(void);
 
TestProc testProcs[] = {	TestTrimLeft
			,	TestTrimRight
			,	TestTrim
			,	TestToUpper
			,	TestToLower
			,	TestICompare
			,	TestTranslate
			,	TestReplace
			,	TestCat
			};
 
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);
 
	ScopedLogMessage msg("StringTest ", "start\n", "end");
 
	for(std::size_t i=0; i<sizeof(testProcs)/sizeof(testProcs[0]); ++i)
	{
		testProcs[i]();
	}
 
	return 0;
}

Results of execution

[0] StringTest start
 
[0] TestTrimLeft start
[0]     source  string: " a b c "
[0]   trimLeft(" a b c "):
[0]     changed string: "a b c "
[0]     source  string: " a b c "
[0]   trimLeftInPlace(" a b c "):
[0]     changed string: "a b c "
[0]     source  string: "a b c "
[0] TestTrimLeft end
 
[0] TestTrimRight start
[0]     source  string: " a b c "
[0]   trimRight(" a b c "):
[0]     changed string: " a b c"
[0]     source  string: " a b c "
[0]   trimRightInPlace(" a b c "):
[0]     changed string: " a b c"
[0]     source  string: " a b c"
[0] TestTrimRight end
 
[0] TestTrim start
[0]     source  string: " a b c "
[0]   trim(" a b c "):
[0]     changed string: "a b c"
[0]     source  string: " a b c "
[0]   trimInPlace(" a b c "):
[0]     changed string: "a b c"
[0]     source  string: "a b c"
[0] TestTrim end
 
[0] TestToUpper start
[0]     source  string: " a b c "
[0]   toUpper(" a b c "):
[0]     changed string: " A B C "
[0]     source  string: " a b c "
[0]   toUpperInPlace(" a b c "):
[0]     changed string: " A B C "
[0]     source  string: " A B C "
[0] TestToUpper end
 
[0] TestToLower start
[0]     source  string: " a b c "
[0]   toLower(" a b c "):
[0]     changed string: " a b c "
[0]     source  string: " a b c "
[0]   toLowerInPlace(" a b c "):
[0]     changed string: " a b c "
[0]     source  string: " a b c "
[0] TestToLower end
 
[0] TestICompare start
[0]     source  string 1: " A B C "
[0]     source  string 2: " a b c "
[0]   iCompare(" A B C ", " a b c "):
[0]     result: "true"
[0] TestICompare end
 
[0] TestTranslate start
[0]     source  string: " a b c "
[0]   translate(" a b c ", "abc", "ABC"):
[0]     changed string: " A B C "
[0]     source  string: " a b c "
[0]   translateInPlace(" a b c ", "abc", "ABC"):
[0]     changed string: " A B C "
[0]     source  string: " A B C "
[0] TestTranslate end
 
[0] TestReplace start
[0]     source  string: "The quick brown fox jumps over the lazy dog."
[0]   replace("The quick brown fox jumps over the lazy dog.", "dog", "cat"):
[0]     changed string: "The quick brown fox jumps over the lazy cat."
[0]     source  string: "The quick brown fox jumps over the lazy dog."
[0]   replaceInPlace("The quick brown fox jumps over the lazy dog.", "dog", "cat"):
[0]     changed string: "The quick brown fox jumps over the lazy cat."
[0]     source  string: "The quick brown fox jumps over the lazy cat."
[0] TestReplace end
 
[0] TestCat start
[0]   cat("Pack ", "my box ", "with ", "five dozen ", "liquor ", "jugs.")
[0]     combined string: "Pack my box with five dozen liquor jugs."
[0] TestCat end
 
[0] StringTest end

Downloads

ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年5月11日からのダウンロード数:816

Subversion

・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。

Reference

http://pocoproject.org にある StringsAndFormatting のプレセンテーション。(PDF)

Powered by POCO Copyright © 2010 Round Square Inc. All rights reserved.


Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">