yapf/str.hpp
branchcustombridgeheads
changeset 5616 0570ae953222
child 5619 a2f1d08e2215
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yapf/str.hpp	Sun Dec 31 21:13:40 2006 +0000
@@ -0,0 +1,66 @@
+/* $Id$ */
+
+#ifndef  STR_HPP
+#define  STR_HPP
+
+#include <errno.h>
+
+#include "blob.hpp"
+#include "strapi.hpp"
+
+
+// simple string implementation
+template <typename Tchar, bool TcaseInsensitive>
+struct CStrT : public CBlobT<Tchar>
+{
+	typedef CBlobT<Tchar> base;
+	typedef CStrApiT<Tchar, TcaseInsensitive> Api;
+
+	FORCEINLINE CStrT(const Tchar* str = NULL) {AppendStr(str);}
+	FORCEINLINE CStrT(const Tchar* str, size_t num_chars) : base(str, num_chars) {FixTail();}
+	FORCEINLINE CStrT(const Tchar* str, const Tchar* end) : base(str, end - str) {FixTail();}
+	FORCEINLINE CStrT(const CBlobBaseSimple& src) : base(src) {FixTail();}
+	/** Take ownership constructor */
+	FORCEINLINE CStrT(OnTransfer& ot) : base(ot) {}
+	FORCEINLINE Titem* GrowSizeNC(size_t count) {Titem* ret = base::GrowSizeNC(count); FixTail(); return ret;}
+	FORCEINLINE void AppendStr(const Tchar* str) {if (str != NULL && str[0] != '\0') base::Append(str, (size_t)Api::StrLen(str)); FixTail();}
+	FORCEINLINE CStrT& operator = (const Tchar* src) {Clear(); Append(src); return *this;}
+	FORCEINLINE bool operator < (const CStrT &other) const {return (Api::StrCmp(Data(), other.Data()) < 0);}
+
+	int FormatL(const Tchar *fmt, va_list args)
+	{
+		size_t addSize = Api::StrLen(fmt);
+		if (addSize < 16) addSize = 16;
+		addSize += addSize > 1;
+		int ret;
+		do {
+			Tchar *buf = MakeFreeSpace(addSize);
+			ret = Api::SPrintFL(buf, GetReserve(), fmt, args);
+			addSize *= 2;
+		} while(ret < 0 && (errno == ERANGE || errno == 0));
+		if (ret > 0) {
+			GrowSizeNC(ret);
+		} else {
+			int err = errno;
+			FixTail();
+		}
+		return ret;
+	}
+
+	int Format(const Tchar *format, ...)
+	{
+		va_list args;
+		va_start(args, format);
+		int ret = FormatL(format, args);
+		va_end(args);
+		return ret;
+	}
+
+};
+
+typedef CStrT<char   , false> CStrA;
+typedef CStrT<char   , true > CStrCiA;
+typedef CStrT<wchar_t, false> CStrW;
+typedef CStrT<wchar_t, true > CStrCiW;
+
+#endif /* STR_HPP */