/**
* @file string.hpp
* @brief J Library String ヘッダファイル。
* @copyright 2001 - 2024 Nomura Kei
* @depends
* j/lang/object.hpp
*/
#ifndef J_LANG_STRING_HPP
#define J_LANG_STRING_HPP
#include <memory>
#include <ostream>
#include <istream>
#include <sstream>
#include <string>
#include <vector>
#include <j/lang/object.hpp>
namespace j
{
namespace lang
{
class String final : public Object
{
public:
// コンストラクタ
String(const char *str = "") noexcept;
// コンストラクタ
String(const std::string &str) noexcept;
// コピーコンストラクタ
String(const String &str) noexcept;
// ムーブコンストラクタ
String(String &&str) noexcept;
// デストラクタ
~String() noexcept;
// コピー代入演算子
String &operator=(const String &str) noexcept;
// ムーブ代入演算子
String &operator=(String &&str) noexcept;
// 文字列結合用
String &operator+=(const String &str) noexcept;
// 数値型との結合
template <typename T>
String operator+(T number) const
{
std::ostringstream oss;
oss << &value[0] << number;
return String(oss.str());
}
// 比較演算子
bool operator==(const String &str) const noexcept;
// 比較演算子
bool operator!=(const String &str) const noexcept;
// C言語文字列表現
operator const char *() const;
// 文字列長を返す。
int length() const noexcept;
// 指定された位置の文字を返す。
char charAt(int index) const;
// 部分文字列を返す。
String substring(int beginIndex) const;
// 部分文字列を返す。
String substring(int beginIndex, int endIndex) const;
// 指定文字列が含まれるかい否かを返す。
bool contains(const String &str) const noexcept;
// 文字置換
String replace(char oldChar, char newChar) const noexcept;
// 文字列置換
String replace(const String ®ex, const String &replacement) const;
// 文字列置換
String replaceAll(const String ®ex, const String &replacement) const;
// 分割
std::vector<String> split(const String ®ex) const noexcept;
// 先頭の文字列が一致するか
bool startsWith(const String &prefix) const noexcept;
// 末尾の文字列が一致するか
bool endsWith(const String &suffix) const noexcept;
// 小文字変換
String toLowerCase() const noexcept;
// 大文字変換
String toUpperCase() const noexcept;
// trim
String trim() const noexcept;
// 検索
int indexOf(int ch, int fromIndex = 0) const noexcept;
int indexOf(const String &str, int fromIndex = 0) const noexcept;
int lastIndexOf(int ch) const noexcept;
int lastIndexOf(const String &str) const noexcept;
int lastIndexOf(int ch, int fromIndex) const noexcept;
int lastIndexOf(const String &str, int fromIndex) const noexcept;
// 文字列表現取得
String toString() const noexcept override;
// 比較
bool equals(const Object &obj) const noexcept override;
// ハッシュコード
int hashCode() const noexcept override;
// 文字列結合用
friend String operator+(const String &str1, const String &str2) noexcept;
// 出力用
friend std::ostream &operator<<(std::ostream &os, const String &str);
// 入力用
friend std::istream &operator>>(std::istream &is, String &str);
// 数値型との結合(数値が左辺)
template <typename T>
friend String operator+(T number, const String &str) noexcept
{
std::ostringstream oss;
oss << number << &str.value[0];
return String(oss.str());
}
protected:
// クローン
std::unique_ptr<Object> clone() const noexcept override;
private:
// 値
std::unique_ptr<char[]> value;
// 文字列の長さ
int len;
// データ設定関数
void setValue(const char *str);
};
} // namespace lang
} // namespace j
#endif // J_LANG_STRING_HPP