dwstring.h.svn-base
来自「ffshow源码」· SVN-BASE 代码 · 共 905 行 · 第 1/3 页
SVN-BASE
905 行
//=============================================================================// File: dwstring.h// Contents: Declarations for DwString// Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>// WWW: http://www.fwb.gulf.net/~dwsauder/mimepp.html//// Copyright (c) 1996, 1997 Douglas W. Sauder// All rights reserved.//// IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,// INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF// THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER// HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//// DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT// NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A// PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"// BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,// SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.////=============================================================================#ifndef DW_STRING_H#define DW_STRING_H#include "char_t.h"#include "safe_bool.h"//=============================================================================// DwStringRep is an implementation class that should not be used externally.//=============================================================================template<class tchar> struct DwStringRep { DwStringRep(tchar* aBuf, size_t aSize); //DwStringRep(FILE* aFile, size_t aSize); ~DwStringRep(); // void* operator new(size_t); // void operator delete(void*, size_t); size_t mSize; tchar* mBuffer; int mRefCount, mPageMod;//private: // memory management // DwStringRep* mNext; // static DwStringRep* theirPool; // static int theirPoolCount;public: void CheckInvariants() const;};//=============================================================================//+ Name DwString -- String class//+ Description//. {\tt DwString} is the workhorse of the MIME++ library. Creating, parsing,//. or otherwise manipulating MIME messages is basically a matter of//. manipulating strings. {\tt DwString} provides all the basic functionality//. required of a string object, including copying, comparing, concatenating,//. and so on.//.//. {\tt DwString} is similar to the {\tt string} class that is part of//. the proposed ANSI standard C++ library. Some of the member functions//. present in the ANSI {\tt string} are not present in {\tt DwString}://. mostly these are the functions that deal with iterators. {\tt DwString}//. also includes some member functions and class utility functions that//. are not a part of the ANSI {\tt string} class. These non-ANSI//. functions are easy to distinguish: they all begin with upper-case//. letters, and all ANSI functions begin with lower-case letters. The//. library classes themselves use only the ANSI {\tt string} functions.//. At some point in the future, MIME++ will probably allow the option to//. substitute the ANSI {\tt string} class for {\tt DwString}.//.//. {\tt DwString} makes extensive use of copy-on-write, even when extracting//. substrings. It is this feature that distiguishes {\tt DwString} from most//. other string classes. {\tt DwString} also handles binary data, which can//. contain embedded NUL characters.//=============================================================================//+ Noentry _copy _replace Length AsCharBuf Substring Prefix Suffix Prepend//+ Noentry Append Insert Replace Delete mRep mStart mLength sEmptyString//+ Noentry ~DwStringclass CUnknown;template<class tchar> class DwString :public safe_bool< DwString<tchar> > {private: void init1(const tchar*); void init2(const DwString<tchar>& aStr, size_t aPos, size_t aLen);public: static CUnknown* WINAPI CreateInstance(LPUNKNOWN punk,HRESULT *phr) { return NULL; } static const size_t npos; //. {\tt npos} is assigned the value (size_t)-1. DwString(); DwString(const DwString<char>& aStr, size_t aPos=0, size_t aLen=npos); DwString(const DwString<wchar_t>& aStr, size_t aPos=0, size_t aLen=npos); DwString(const tchar* aBuf, size_t aLen); //DwString(FILE* aFile , size_t aLen); DwString(const char* aCstr); DwString(const wchar_t* aCstr); DwString(size_t aLen, tchar aChar); DwString(tchar* aBuf, size_t aSize, size_t aStart, size_t aLen); //. The first constructor is the default constructor, which sets the //. {\tt DwString} object's contents to be empty. //. //. The second constructor is the copy constructor, which copies at most //. {\tt aLen} characters beginning at position //. {\tt aPos} from {\tt aStr} to the new {\tt DwString} object. It will //. not copy more characters than what are available in {\tt aStr}. //. {\tt aPos} must be less than or equal to {\tt aStr.size()}. //. //. The third constructor copies {\tt aLen} characters from the buffer //. {\tt aBuf} into the new {\tt DwString} object. {\tt aBuf} need not be //. NUL-terminated and may contain NUL characters. //. //. The fourth constructor copies the contents of the NUL-terminated string //. {\tt aCstr} into the new {\tt DwString} object. //. //. The fifth constructor sets the contents of the new {\tt DwString} //. object to be the character {\tt aChar} repeated {\tt aLen} times. //. //. The sixth constructor is an {\it advanced} constructor that sets //. the contents of the new {\tt DwString} object to the {\tt aLen} //. characters starting at offset {\tt aStart} in the buffer {\tt aBuf}. //. {\tt aSize} is the allocated size of {\tt aBuf}. //. This constructor is provided for efficiency in setting a new //. {\tt DwString}'s contents from a large buffer. It is efficient //. because no copying takes place. Instead, {\tt aBuf} becomes the //. buffer used internally by the {\tt DwString} object, which //. takes responsibility for deleting the buffer. //. Because {\tt DwString} will free the buffer using {\tt delete []}, //. the buffer should have been allocated using {\tt new}. //. See also: TakeBuffer(), and ReleaseBuffer(). ~DwString(); static DwString intToStr(int i); DwString& operator = (const DwString& aStr) { return assign(aStr); } inline DwString& operator = (const char* aCstr); inline DwString& operator = (const wchar_t* aCstr); DwString& operator = (tchar aChar) { return assign(1, aChar); } //. Assigns the contents of the operand to this string. //. {\tt aCstr} must point to a NUL-terminated array of characters //. (a C string). //. Returns {\tt *this}. size_t size() const { return mLength; } //. Returns the number of characters in this string's contents. This //. member function is identical to {\tt length()} size_t length() const { return mLength; } //. Returns the number of characters in this string's contents. This //. member function is identical to {\tt size()} size_t max_size() const; //. Returns the maximum length that this string can ever attain. void resize(size_t aLen, tchar aChar); void resize(size_t aLen); //. Changes the length of this string. If the string shortened, the final //. characters are truncated. If the string is expanded, the added //. characters will be NULs or the character specified by {\tt aChar}. size_t capacity() const { return mRep->mSize - 1; } //. Returns the size of the internal buffer used for this string, which //. will always be greater than or equal to the length of the string. void reserve(size_t aSize); //. If {\tt aSize} is greater than the current capacity of this string, //. this member function will increase the capacity to be at least //. {\tt aSize}. void clear(); //. Sets this string's contents to be empty. inline bool empty() const { return mLength == 0; } //. Returns a true value if and only if the contents of this string //. are empty. bool boolean_test() const { return !empty(); } const tchar& operator [] (size_t aPos) const { return at(aPos); } tchar& operator [] (size_t aPos) { return at(aPos); } //. Returns {\tt DwString::at(aPos) const} or {\tt DwString::at(aPos)}. //. Note that the non-const version always assumes that the contents //. will be modified and therefore always copies a shared internal //. buffer before it returns. const tchar& at(size_t aPos) const { // Returning const tchar& instead of tchar will allow us to use DwString::at() // in the following way: // if (&s.at(1) == ' ') { /* ... */ } assert(aPos <= mLength); if (aPos < mLength) { return data()[aPos]; } else if (aPos == mLength) { return sEmptyRep->mBuffer[0]; } else { // This "undefined behavior" // Normally, this will not occur. The assert() macro will catch it, // or at some point we may throw an exception. return data()[0]; } } tchar& at(size_t aPos) { assert(aPos < mLength); if (aPos < mLength) { return (tchar&) c_str()[aPos]; } else { // This is "undefined behavior" assert(0); return (tchar&) c_str()[0]; } } //. Returns the character at position {\tt aPos} in the string's contents. //. The non-const version returns an lvalue that may be assigned to. //. Note that the non-const version always assumes that the contents //. will be modified and therefore always copies a shared internal //. buffer before it returns. DwString& operator += (const DwString& aStr) { return append(aStr); } DwString& operator += (const tchar* aCstr) { return append(aCstr); } DwString& operator += (tchar aChar) { return append(1, aChar); } //. Appends the contents of the operand to this string. //. {\tt aCstr} must point to a NUL-terminated array of characters //. (a C string). //. Returns {\tt *this}. DwString& append(const DwString& aStr); DwString& append(const DwString& aStr, size_t aPos, size_t aLen); DwString& append(const tchar* aBuf, size_t aLen); DwString& append(const tchar* aCstr); DwString& append(size_t aLen, tchar aChar); //. Appends characters to (the end of) this string. //. Returns {\tt *this}. //. //. The first version appends all of the characters from {\tt aStr}. //. //. The second version appends at most {\tt aLen} characters from //. {\tt aStr} beginning at position {\tt aPos}. {\tt aPos} must be //. less than or equal to {\tt aStr.size()}. The function will not //. append more characters than what are available in {\tt aStr}. //. //. The third version appends {\tt aLen} characters from {\tt aBuf}, //. which is not assumed to be NUL-terminated and can contain embedded //. NULs. //. //. The fourth version appends characters from the NUL-terminated //. string {\tt aCstr}. //. //. The fifth version appends {\tt aChar} repeated {\tt aLen} times. DwString& assign(const DwString& aStr); DwString& assign(const DwString& aStr, size_t aPos, size_t aLen);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?