LoginSignup
1
2

More than 5 years have passed since last update.

自作String拡張クラスの生きる痕跡

Last updated at Posted at 2017-12-06

2017年、githubのお試しfirst repositoryとしてつくったものの、誰の目にも触れられずひっそりと生きているわが子(repository)が不憫なため、ここに生きる痕跡を残しておきます。

これ↓
112KA/String

どんなクラス?

文字を操作しやすくしたstd::stringの機能拡張ライブラリ。
あまり他に見ない特徴としては、自分のmemory allocatorを設定できるところです。

仕様類似物
-ezhikman/String
-Lmachado73/String

std::basic_stringを拡張したかったけど、関数がprivateになってて拡張できなかったので、全部新しく書いた。(ほぼコピペ)

custom memory allocatorが設定できるString拡張クラス見つからなかったけど、そもそも需要がないのかな。。細かくメモリ管理したいなどがなければ使わないかも。。

使い方

設定

#include "BasicString.hpp"

// use std::allocator 
using string = BasicString<char, std::char_traits<char>, std::allocator<char> >;
// or original allocator
using my_string = BasicString<char, std::char_traits<char>, my_allocator<char> >;

fomat指定で初期化

//constructor
my_string s1("string1");
printf("%s\n", s1.c_str());     //string1

my_string s2("string%d",2);
printf("%s\n", s2.c_str());     //string2

文字連結

my_string s3(s1 + "_" + s2);
printf("%s\n", s3.c_str());     //string1_string2


//operator+=
s1 += "_";
s1 += s2;
s1 += "_" + s3;
printf("%s\n", s1.c_str());     //string1_string2_string1_string2


//append
s2.append("_string3");
s2.append("_string%d",4);
printf("%s\n", s2.c_str());     //string2_string3_string4


その他

-hpp
-CMake初めて書いた。
-Catch(Testing framework)初めて使った。いつのまにかCatch2がでてるが使ってるのは古いやつ。
-Doxygen初めて使った。
-CMake/Catch/Doxygen構成はgithubでも時々見かけるので、ちょっとしたライブラリ開発を考えてる初学者には構成要素の参考になるのでは。

参考リンク

-Catch というマクロライブラリで C++ のコードをテストする
-std::string::nposの正体
-クラスへの機能のちょい足し

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2