⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tc3d_string.h

📁 自己写的一些基本的3d引擎的基础的代码
💻 H
📖 第 1 页 / 共 2 页
字号:
/**
 * Comet 3D Engine file (c) 2007 - 2008 Tianjie Wei, THSystems Research Group
 *
 * Released under BSD license, please refer to license.txt for more information
 */

#ifndef _TC3D_STRING_H_
#define _TC3D_STRING_H_

#include "TC3D_Array.h"

namespace C3D
{
	namespace Util
	{
		/**
		 * Comet 3D string class
		 *
		 * @Author Tianjie (James) Wei
		 * @Version 3.0
		 */
		template < class T, class TAlloc = TC3D_Allocator<T> >
		class TC3D_String : public TC3D_Array<T, TAlloc>
		{

			private:

				friend class CC3D_Iterator;

				TC3D_U32 tSize;

			public:
                   
			    class CC3D_Iterator
				{
					private:

						friend class TC3D_String<T, TAlloc>;

						class TC3D_Array<T, TAlloc>::CC3D_Iterator tData;

						TC3D_String<T, TAlloc> *pBase;
	
					private:

						/** Private constructor, for the TC3D_String class */
						CC3D_Iterator(const class TC3D_Array<T, TAlloc>::CC3D_Iterator &src, 
							TC3D_String<T, TAlloc> *base)
						{
							tData = src;
							pBase = base;
						}

					public:

						/** Default constructor */
						CC3D_Iterator()
						{
							pBase = DC3D_NULL;
						}

						/** Copy constructor */
						CC3D_Iterator(const CC3D_Iterator &src)
						{
							tData = src.tData;
							pBase = src.pBase;
						}

						/** Destructor */
						virtual ~CC3D_Iterator()
						{
						}

						/** Dereferencing */
						T &operator * () const
						{  
							MC3D_DEBUG_ASSERT((tData.Pointer() - pBase->Pointer()) < (TC3D_S32)pBase->Size());
							return tData.operator *();
						}

						/** Calling member functions */
						T *operator -> () const
						{ 
							MC3D_DEBUG_ASSERT((tData.Pointer() - pBase->Pointer()) < (TC3D_S32)pBase->Size());
							return tData.operator ->();
						}

						/** Pre increment pointer */
						CC3D_Iterator &operator ++ ()	
						{ 
							MC3D_DEBUG_ASSERT(tData.Pointer() < (pBase->Pointer() + pBase->Size()));
							++tData;
							return *this;
						}

						/** Pre decrement pointer */
						CC3D_Iterator &operator -- ()	
						{
							MC3D_DEBUG_ASSERT(tData.Pointer() > pBase->Pointer());
							--tData;
							return *this;
						}

						/** Post increment pointer */
						CC3D_Iterator operator ++ (TC3D_S32 dummy)	
						{ 
							MC3D_DEBUG_ASSERT(tData.Pointer() < (pBase->Pointer() + pBase->Size()));
							CC3D_Iterator temp(*this);
							++tData;
							return temp;
						}

						/** Post decrement pointer */
						CC3D_Iterator operator -- (TC3D_S32 dummy)	
						{
							MC3D_DEBUG_ASSERT(tData.Pointer() > pBase->Pointer());
							CC3D_Iterator temp(*this);
							--tData;
							return temp; 
						}

						/** Addition operator */
						CC3D_Iterator operator + (TC3D_S32 value) const
						{ 
							CC3D_Iterator temp(tData + value, pBase);
							return temp;
						}

						/** Subtraction operator */
						CC3D_Iterator operator - (TC3D_S32 value) const
						{ 
							CC3D_Iterator temp(tData - value, pBase);
							return temp;
						}

						/** Subtraction operator */
						TC3D_S32 operator - (const CC3D_Iterator &src) const
						{ 
							return (tData - src.tData);
						}

						/** Addition and assignment operator */
						CC3D_Iterator &operator += (TC3D_S32 value) 
						{ 
							tData += value;
							return *this; 
						}

						/** Subtraction and assignment operator */
						CC3D_Iterator &operator -= (TC3D_S32 value) const
						{ 
							tData -= value;
							return *this; 
						}

						/** Iterator comparison */
						TC3D_Bool operator == (const CC3D_Iterator &it) const
						{ 
							return (tData == it.tData);
						}

						/** Iterator comparison */
						TC3D_Bool operator != (const CC3D_Iterator &it) const 
						{ 
							return (tData != it.tData); 
						}

						/** Iterator comparison */
						TC3D_Bool operator >= (const CC3D_Iterator &it) const
						{ 
							return (tData >= it.tData); 
						}

						/** Iterator comparison */
						TC3D_Bool operator <= (const CC3D_Iterator &it) const 
						{ 
							return (tData <= it.tData); 
						}

						/** Iterator comparison */
						TC3D_Bool operator > (const CC3D_Iterator &it) const 
						{ 
							return (tData > it.tData); 
						}

						/** Iterator comparison */
						TC3D_Bool operator < (const CC3D_Iterator &it) const 
						{
							return (tData < it.tData); 
						}
				};

			public:

				/** Default constructor */
				TC3D_String()
				{
					tSize = 0;
					TC3D_Array<T, TAlloc>::Push_Back('\0');
				}

				/** Constructor with default C-Style string */
				TC3D_String(const T *str)
				{
					tSize = 0;
					Append(str);
				}

				/** Copy constructor */
				TC3D_String(const TC3D_String<T, TAlloc> &src)
				{
					tSize = 0;
					Append(src);
				}

				/** Class destructor */
				virtual ~TC3D_String()
				{
					// Array already cleared, so do nothing
				}

				/** Check if string is inside this string */
				virtual TC3D_Bool In_String(const T *str) const
				{
					if(!str || !*str)
						return DC3D_FALSE;

					TC3D_U32 len = Get_Length(str);

					for(TC3D_U32 i = 0; i < tSize; i++)
					{
						// This is where it gets interesting
						if(TC3D_Array<T, TAlloc>::operator [] (i) == *str)
						{
							for(TC3D_U32 j = 0; j < len; j++)
							{
								if(!TC3D_Array<T, TAlloc>::operator [] (i + j) || !str[j])
									return DC3D_FALSE;

								if(TC3D_Array<T, TAlloc>::operator [] (i + j) != str[j])
									return DC3D_FALSE;
							}
							return DC3D_TRUE;
						}
					}

					return DC3D_FALSE;
				}

				/** Clears the string */
				virtual void Clear()
				{
					TC3D_Array<T, TAlloc>::Clear();
					TC3D_Array<T, TAlloc>::Push_Back('\0');
					tSize = 0;
				}

				/** Gets the length of a given C-Style string */
				virtual TC3D_U32 Get_Length(const T *str) const
				{
					if(!str)
						return 0;

					TC3D_U32 i = 0;

					while(str[i])
						i++;

					return i;
				}

				/** Gets the string iterator pointing at the beginning of the string */
				CC3D_Iterator Begin()
				{
					return CC3D_Iterator(TC3D_Array<T, TAlloc>::Begin(), this);
				}

				/** Gets the string iterator pointing at the end of the string */
				CC3D_Iterator End()
				{
					return CC3D_Iterator(TC3D_Array<T, TAlloc>::End() - 1, this);
				}

				/** Converts to a C-Style string */
				virtual const T *To_String_Const() const
				{
					return TC3D_Array<T, TAlloc>::Pointer_Const();
				}

				/** Gets the pointer to the string */
				virtual const T *Pointer_Const() const
				{
					return TC3D_Array<T, TAlloc>::Pointer_Const();
				}

				/** Converts to a C-Style string */
				virtual T *To_String() const
				{
					return TC3D_Array<T, TAlloc>::Pointer();
				}

				/** Gets the pointer to the string */
				virtual T *Pointer() const
				{
					return TC3D_Array<T, TAlloc>::Pointer();
				}

				/** Convert the string to an integer */
				virtual TC3D_S32 To_Integer(TC3D_U32 indexIn, TC3D_U32 &indexOut) const
				{
					TC3D_S32 result = 0;
					TC3D_Bool negate = DC3D_FALSE;

					if(indexIn >= tSize)
						return 0;

					while(MC3D_IS_SPACE((TC3D_Array<T, TAlloc>::operator [] (indexIn))))
					{
						if(indexIn >= tSize)
							return 0;
						indexIn++;
					}

					if(TC3D_Array<T, TAlloc>::operator [] (indexIn) == '-')
					{
						indexIn++;
						negate = DC3D_TRUE;
					}

					while(MC3D_IS_SPACE((TC3D_Array<T, TAlloc>::operator [] (indexIn))))
					{
						if(indexIn >= tSize)
							return 0;
						indexIn++;
					}

					TC3D_U32 i;
					for(i = indexIn; i < tSize; i++)
					{
						if(!MC3D_IS_DIGIT(TC3D_Array<T>::operator[](i)))
							break;
						result *= 10;
						result += (TC3D_Array<T, TAlloc>::operator [] (i) - '0');
					}

					indexOut = i;

					return negate ? -result : result;
				}

				/** Convert the string to a float */
				virtual TC3D_F32 To_Float(TC3D_U32 indexIn, TC3D_U32 &indexOut) const
				{
					TC3D_S32 resInt = 0;
					TC3D_F32 resFloat = 0.0f;
					TC3D_Bool negate = DC3D_FALSE;

					if(indexIn >= tSize)
						return 0.0f;

					while(MC3D_IS_SPACE((TC3D_Array<T, TAlloc>::operator [] (indexIn))))
					{
						if(indexIn >= tSize)
							return 0.0f;
						indexIn++;
					}

					if(TC3D_Array<T, TAlloc>::operator [] (indexIn) == '-')
					{
						indexIn++;
						negate = DC3D_TRUE;
					}

					while(MC3D_IS_SPACE((TC3D_Array<T, TAlloc>::operator [] (indexIn))))
					{
						if(indexIn >= tSize)
							return 0.0f;
						indexIn++;
					}

					TC3D_U32 i;
					for(i = indexIn; i < tSize; i++)
					{
						if(TC3D_Array<T>::operator[](i) == '.')
							break;
						if(!MC3D_IS_DIGIT(TC3D_Array<T>::operator[](i)))
						{
							indexOut = i;
							return (TC3D_F32)(negate ? -resInt : resInt);
						}
						resInt *= 10;
						resInt += TC3D_Array<T, TAlloc>::operator [] (i) - '0';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -