yapf/str.hpp
author KUDr
Sun, 31 Dec 2006 21:13:40 +0000
branchcustombridgeheads
changeset 5616 0570ae953222
child 5619 a2f1d08e2215
permissions -rw-r--r--
(svn r7708) [cbh] - Fix: [YAPF] added some YAPF debug messages, added string support used for new debugging stuff, resolved few cbh related issues
/* $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 */