软件架构设计之Utility模块——string

软件架构设计之Utility模块——string,第1张

YKString类是对STL 中的std::wstring的功能扩展,没有什么好解释的了,就直接看代码吧。

头文件:
class YKString : public std::wstring
{
public:
	typedef std::wstring::size_type	size_type;

	YKString() : std::wstring() {}
	YKString(const std::wstring& str, size_type pos = 0, size_type n = npos)
		: std::wstring(str, pos, n) {}
	YKString(const value_type* str) : std::wstring(str) {}
	YKString(const value_type* str, size_type n) : std::wstring(str, n) {}
	YKString(size_type n, value_type val) : std::wstring(n, val) {}
	YKString(value_type ch) : std::wstring(1, ch) {}
	YKString(const std::string& str) { Assign(str.c_str()); }
	YKString(const YK_CHAR* str) { Assign(str); }

	template <class InputIter>
	YKString(InputIter fIter, InputIter lIter) : std::wstring(fIter, lIter) { }

	~YKString() {}

	YKString& operator= (const value_type str)
	{ assign(1, str); return *this; }
	YKString& operator= (const value_type* str)
	{ assign(str); return *this; }
	YKString& operator= (const std::wstring& str)
	{ assign(str.c_str()); return *this; }
	YKString& operator= (const std::string& str)
	{ Assign(str.c_str()); return *this; }
	YKString& operator= (const YK_CHAR* str)
	{ Assign(str); return *this; }

	YKString& operator+= (const YKString& str) 
	{ return (*this += str.c_str());}
	YKString& operator+= (const value_type* str)
	{ append(str); return *this; }

//////////////////////////////////////////////////////////////////////////
//	非变更 *** 作

	// 指定pos的字符是否为小写字母、pos==npos则是否全为小写字母
	YK_BOOL IsLower(size_type pos = npos) const;
	// 指定pos的字符是否为大写字母、pos==npos则是否全为大写字母
	YK_BOOL IsUpper(size_type pos = npos) const;
	// 指定pos的字符是否为数字、pos==npos则是否全为数字
	YK_BOOL IsNumeric(size_type pos = npos) const;
	// 指定pos的字符是否为字母(不分大小写)、pos==npos则是否全为字母(不分大小写)
	YK_BOOL IsAlpha(size_type pos = npos) const;
	// 指定pos的字符是否为字母或数字、pos==npos则是否全为字母或数字
	YK_BOOL IsAlnum(size_type pos = npos) const;
	// 指定pos的字符是否为空白字符、pos==npos则是否全为空白字符
	YK_BOOL IsBlank(size_type pos = npos) const;
	// 忽略大小写的比较两个字符串
	YK_INT iCompare(const YKString& rhs) const;

	// know-C++
	// 意义是将*this设置为const,即将this指针指向用来调用该成员函数的对象设置为const,那么该成员函数不可以修改这个对象
	// const成员函数只能调用const成员函数,不能调用非const成员函数
	// const成员函数可以调用成员变量,但是不能修改成员变量
	// const成员函数可以修改mutable修饰的成员变量;如果成员变量是指针的话,可以修改指针指向的变量的值

//////////////////////////////////////////////////////////////////////////
//	变更 *** 作


	// 将字母全转为小写
	YKString& ToLower();
	// 将字母全转为大写
	YKString& ToUpper();
	// 以rVal替换所有lVal字符串
	YKString& Replace(const YKString& lVal, const YKString& rVal);
	// 去除字符串左边的空白字符
	void TrimLeft();
	// 去除字符串右边的空白字符
	void TrimRight();
	// 去除字符串左右两边的空白字符
	void Trim();

	// 字符串追加指定类型的值,precision为double型的精度
	template <typename T>
	void Append(T val, size_type precision = 0) {
		append(Format(val, precision).c_str());
	}

//////////////////////////////////////////////////////////////////////////
/// 查找

	// 为find_first_of的忽略大小写版本
	size_type ifind_first_of(const YKString& str, size_type off = 0, size_type count = 0);
	// 为find_first_not_of的忽略大小写版本
	size_type ifind_first_not_of();
	// 为find_last_of的忽略大小写版本
	size_type ifind_last_of();
	// 为find_last_not_of的忽略大小写版本
	size_type ifind_last_not_of();
	// 
	size_type find_nth();
	size_type ifind_nth();
	YKString find_head(const YKString& sep) const;
	YKString find_tail(const YKString& sep) const;

//////////////////////////////////////////////////////////////////////////
// 高级 *** 作

	// 转换为string
	std::string ToString() const;

	// 将容器内的内容以sep字符串作为分隔符拼接
	// bPair = false,格式形如:XXXwstrSepXXX
	// bPari = true,格式形如:XXXwstrSepXXXwstrSep
	template <typename T>
	static YKString Join(const T& container, const YKString& sep, YK_BOOL bPair = false) {
		T::const_iterator fIter = container.begin();
		T::const_iterator lIter = container.end();
		return Join(fIter, lIter, sep, bPair);
	}
	// 将迭代器内的内容以sep字符串作为分隔符拼接
	// bPair = false,格式形如:XXXwstrSepXXX
	// bPari = true,格式形如:XXXwstrSepXXXwstrSep
	template <typename InputIter>
	static YKString Join(InputIter fIter, InputIter lIter, const YKString& sep, YK_BOOL bPair = false) {
		YKString strRt;
		for (; fIter != lIter; ++fIter)
		{
			strRt.Append(*fIter);
			strRt.append(sep.c_str());
		}
		if (false == bPair && !strRt.empty())
		{
			strRt.erase(strRt.length() - sep.length());
		}

		return strRt;
	}

	// 将以sep分隔的字符串分解到vector容器中
	template <typename T>
	std::vector<T> Parse(const YKString& sep) const {
		std::vector<T> vecVal;
		size_type idxPre = 0;
		size_type idx = find(sep.c_str());
		while (idx != YKString::npos)
		{
			YKString strTemp = substr(idxPre, idx-idxPre);
			vecVal.push_back(strTemp.Convert<T>());
			idx += sep.size();
			idxPre = idx;
			idx = find(sep, idx);
		}

		if (idxPre <= size() - sep.size())
		{
			YKString strTemp = substr(idxPre);
			vecVal.push_back(strTemp.Convert<T>());
		}

		return vecVal;
	}

	// 将值转换为YKString,可指定浮点数的格式化精度
	template <typename T>
	static YKString Format(T val, size_type precision = 0) {
		std::wostringstream stream;
		if (precision > 0)
		{
			stream.precision(precision);
			stream.flags(std::wostringstream::fixed);
		}
		stream << val;
		return YKString(stream.str());
	}

	// 数字转为YKString,按宽度格式化,左边填充0
	static YKString FormatDigit(YK_LONG val, size_type width);

	// 将字符串里内容转换为指定的类型
	template <typename T>
	T Convert() const {
		try
		{
			return boost::lexical_cast<T>(c_str());
		}
		catch (boost::bad_lexical_cast&)
		{
			if (!empty())	// 字符串内存在不符合指定类型的字符
				assert(0);

			return T();	// 空字符串返回类型的默认值
		}
	}

	// 将字符串里的RGB转换为整形值,字符串内容形式要求:(RGB(xxx,xxx,xxx))
	YK_ULONG ConvertRGB();

private:
	void Assign(const YK_CHAR* pChar);

};

实现文件:

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/2082797.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-07-22
下一篇2022-07-22

发表评论

登录后才能评论

评论列表(0条)

    保存