yapf/str.hpp
branchcustombridgeheads
changeset 5621 6ce400c0a2f4
parent 5620 3b40a41f90d2
child 5622 2f0cb276d32a
equal deleted inserted replaced
5620:3b40a41f90d2 5621:6ce400c0a2f4
     1 /* $Id$ */
       
     2 
       
     3 #ifndef  STR_HPP
       
     4 #define  STR_HPP
       
     5 
       
     6 #include <errno.h>
       
     7 #include <stdarg.h>
       
     8 
       
     9 #include "blob.hpp"
       
    10 #include "strapi.hpp"
       
    11 
       
    12 
       
    13 // simple string implementation
       
    14 template <typename Tchar, bool TcaseInsensitive>
       
    15 struct CStrT : public CBlobT<Tchar>
       
    16 {
       
    17 	typedef CBlobT<Tchar> base;
       
    18 	typedef CStrApiT<Tchar, TcaseInsensitive> Api;
       
    19 	typedef typename base::size_t size_t;
       
    20 	typedef typename base::OnTransfer OnTransfer;
       
    21 
       
    22 
       
    23 	FORCEINLINE CStrT(const Tchar* str = NULL) {AppendStr(str);}
       
    24 	FORCEINLINE CStrT(const Tchar* str, size_t num_chars) : base(str, num_chars) {base::FixTail();}
       
    25 	FORCEINLINE CStrT(const Tchar* str, const Tchar* end) : base(str, end - str) {base::FixTail();}
       
    26 	FORCEINLINE CStrT(const CBlobBaseSimple& src) : base(src) {base::FixTail();}
       
    27 	/** Take ownership constructor */
       
    28 	FORCEINLINE CStrT(const OnTransfer& ot) : base(ot) {}
       
    29 	FORCEINLINE Tchar* GrowSizeNC(size_t count) {Tchar* ret = base::GrowSizeNC(count); base::FixTail(); return ret;}
       
    30 	FORCEINLINE void AppendStr(const Tchar* str) {if (str != NULL && str[0] != '\0') base::Append(str, (size_t)Api::StrLen(str)); base::FixTail();}
       
    31 	FORCEINLINE CStrT& operator = (const Tchar* src) {base::Clear(); Append(src); return *this;}
       
    32 	FORCEINLINE bool operator < (const CStrT &other) const {return (Api::StrCmp(base::Data(), other.Data()) < 0);}
       
    33 
       
    34 	int FormatL(const Tchar *fmt, va_list args)
       
    35 	{
       
    36 		size_t addSize = Api::StrLen(fmt);
       
    37 		if (addSize < 16) addSize = 16;
       
    38 		addSize += addSize > 1;
       
    39 		int ret;
       
    40 		do {
       
    41 			Tchar *buf = MakeFreeSpace(addSize);
       
    42 			ret = Api::SPrintFL(buf, base::GetReserve(), fmt, args);
       
    43 			addSize *= 2;
       
    44 		} while(ret < 0 && (errno == ERANGE || errno == 0));
       
    45 		if (ret > 0) {
       
    46 			GrowSizeNC(ret);
       
    47 		} else {
       
    48 //			int err = errno;
       
    49 			base::FixTail();
       
    50 		}
       
    51 		return ret;
       
    52 	}
       
    53 
       
    54 	int Format(const Tchar *format, ...)
       
    55 	{
       
    56 		va_list args;
       
    57 		va_start(args, format);
       
    58 		int ret = FormatL(format, args);
       
    59 		va_end(args);
       
    60 		return ret;
       
    61 	}
       
    62 
       
    63 };
       
    64 
       
    65 typedef CStrT<char   , false> CStrA;
       
    66 typedef CStrT<char   , true > CStrCiA;
       
    67 typedef CStrT<wchar_t, false> CStrW;
       
    68 typedef CStrT<wchar_t, true > CStrCiW;
       
    69 
       
    70 #endif /* STR_HPP */