Ansistring是一种超长字符串类型。这种字符串的内存动态分配,引用计数,并使用了更新前拷贝技术,存储于堆空间,长度没有限制(可以存储多达20亿个字符!)。其字符类型也是ANSIChar 类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
// DSTRING.H - Support for delphi strings in C++ // (AnsiString and template<sz> SmallString) // $Revision: 1.35.1.0.1.0 $ // $Date: 25 Jan 2002 12:54:18 $ // // Copyright (c) 1997, 2002 Borland Software Corporation #ifndef DSTRING_H #define DSTRING_H #pragma delphiheader begin #include <sysmac.h> #include <stdarg.h> namespace System { class TVarRec; class RTL_DELPHIRETURN Currency; class RTL_DELPHIRETURN WideString; ///////////////////////////////////////////////////////////////////////////// // AnsiString: String class compatible with Delphi's Native 'string' type ///////////////////////////////////////////////////////////////////////////// class RTL_DELPHIRETURN AnsiString { friend AnsiString __fastcall operator +(const char*, const AnsiString& rhs); public: // the TStringFloatFormat enum is used by FloatToStrF enum TStringFloatFormat {sffGeneral, sffExponent, sffFixed, sffNumber, sffCurrency}; static AnsiString __fastcall StringOfChar(char ch, int count); static AnsiString __fastcall LoadStr(int ident); static AnsiString __fastcall LoadStr(HINSTANCE hInstance, int ident); static AnsiString __fastcall FmtLoadStr(int ident, const TVarRec *args, int size); AnsiString& __fastcall LoadString(HINSTANCE hInstance, int ident); // Delphi style 'Format' // static AnsiString __fastcall Format(const AnsiString& format, const TVarRec *args, int size); // C style 'sprintf' (NOTE: Target buffer is the string) // AnsiString& __cdecl sprintf(const char* format, ...); // Returns *this int __cdecl printf(const char* format, ...); // Returns formatted length int __cdecl vprintf(const char* format, va_list); // Returns formatted length // Like above, but appends to the string rather than overwrite AnsiString& __cdecl cat_sprintf(const char* format, ...); // Returns *this int __cdecl cat_printf(const char* format, ...); // Returns formatted length int __cdecl cat_vprintf(const char* format, va_list); // Returns formatted length static AnsiString __fastcall FormatFloat(const AnsiString& format, const long double& value); static AnsiString __fastcall FloatToStrF(long double value, TStringFloatFormat format, int precision, int digits); static AnsiString __fastcall IntToHex(int value, int digits); static AnsiString __fastcall CurrToStr(Currency value); static AnsiString __fastcall CurrToStrF(Currency value, TStringFloatFormat format, int digits); // Constructors __fastcall AnsiString(): Data(0) {} __fastcall AnsiString(const char* src); __fastcall AnsiString(const AnsiString& src); // __fastcall AnsiString(const char* src, unsigned char len); __fastcall AnsiString(const char* src, unsigned int len); __fastcall AnsiString(const wchar_t* src); __fastcall AnsiString(char src); __fastcall AnsiString(short); __fastcall AnsiString(unsigned short); __fastcall AnsiString(int src); __fastcall AnsiString(unsigned int); __fastcall AnsiString(long); __fastcall AnsiString(unsigned long); __fastcall AnsiString(__int64); __fastcall AnsiString(unsigned __int64); __fastcall AnsiString(double src); __fastcall AnsiString(const WideString &src); // Destructor __fastcall ~AnsiString(); // Assignments AnsiString& __fastcall operator =(const AnsiString& rhs); AnsiString& __fastcall operator +=(const AnsiString& rhs); // Comparisons bool __fastcall operator ==(const AnsiString& rhs) const; bool __fastcall operator !=(const AnsiString& rhs) const; bool __fastcall operator <(const AnsiString& rhs) const; bool __fastcall operator >(const AnsiString& rhs) const; bool __fastcall operator <=(const AnsiString& rhs) const; bool __fastcall operator >=(const AnsiString& rhs) const; int __fastcall AnsiCompare(const AnsiString& rhs) const; int __fastcall AnsiCompareIC(const AnsiString& rhs) const; //ignorecase // Accessing character at specified index char __fastcall operator [](const int idx) const { ThrowIfOutOfRange(idx); // Should Range-checking be optional to avoid overhead ?? return Data[idx-1]; } #if defined(ANSISTRING_USE_PROXY_FOR_SUBSCRIPT) // The use of a proxy class optimizes the case where Unique() must be called // when accessing the string via the subscript operator. However, the use of // of the proxy class has some drawbacks. First, it breaks code that apply // operators to the return value. For example, &MyString[i]. Second, it // fails in cases where a implicit conversion was relied upon. For example, // callFuncThatTakesAnObjectWithACharCtr(MyString[i]); // In that case, two implicit conversions would be required... // The first issue can be remedied by enhancing the proxy class to support // all valid operators. The second issue can be lessened but not completely // eliminated. Hence, the use of the PROXY class is not the default! // private: class TCharProxy; friend TCharProxy; class TCharProxy { public: TCharProxy(AnsiString& strRef, int index) : m_Ref(strRef), m_Index(index) {} TCharProxy& operator=(char c) { m_Ref.Unique(); m_Ref.Data[m_Index-1] = c; return *this; } operator char() const { return m_Ref.Data[m_Index-1]; } protected: AnsiString& m_Ref; int m_Index; }; public: TCharProxy __fastcall operator [](const int idx) { ThrowIfOutOfRange(idx); // Should Range-checking be optional to avoid overhead ?? return TCharProxy(*this, idx); } #else char& __fastcall operator [](const int idx) { ThrowIfOutOfRange(idx); // Should Range-checking be optional to avoid overhead ?? Unique(); // Ensure we're not ref-counted return Data[idx-1]; } #endif // Concatenation AnsiString __fastcall operator +(const AnsiString& rhs) const; // C string operator char* __fastcall c_str() const { return (Data)? Data: "";} // Read access to raw Data ptr. Will be NULL for an empty string. const void* __fastcall data() const { return Data; } // Query attributes of string int __fastcall Length() const; bool __fastcall IsEmpty() const { return Data == NULL; } // Make string unique (refcnt == 1) AnsiString& __fastcall Unique(); // Modify string AnsiString& __fastcall Insert(const AnsiString& str, int index); AnsiString& __fastcall Delete(int index, int count); AnsiString& __fastcall SetLength(int newLength); int __fastcall Pos(const AnsiString& subStr) const; AnsiString __fastcall LowerCase() const; AnsiString __fastcall UpperCase() const; AnsiString __fastcall Trim() const; AnsiString __fastcall TrimLeft() const; AnsiString __fastcall TrimRight() const; AnsiString __fastcall SubString(int index, int count) const; int __fastcall ToInt() const; int __fastcall ToIntDef(int defaultValue) const; double __fastcall ToDouble() const; // Convert to Unicode int __fastcall WideCharBufSize() const; wchar_t* __fastcall WideChar(wchar_t* dest, int destSize) const; // MBCS support enum TStringMbcsByteType {mbSingleByte, mbLeadByte, mbTrailByte}; TStringMbcsByteType __fastcall ByteType(int index) const; bool __fastcall IsLeadByte(int index) const; bool __fastcall IsTrailByte(int index) const; bool __fastcall IsDelimiter(const AnsiString& delimiters, int index) const; bool __fastcall IsPathDelimiter(int index) const; int __fastcall LastDelimiter(const AnsiString& delimiters) const; int __fastcall AnsiPos(const AnsiString& subStr) const; char* __fastcall AnsiLastChar() const; protected: void ThrowIfOutOfRange(int idx) const; struct StrRec { int allocSiz; int refCnt; int length; }; const StrRec &GetRec() const; StrRec &GetRec(); private: // assert(offsetof(AnsiString, Data) == 0); char *Data; }; extern AnsiString __fastcall operator +(const char*, const AnsiString&); #if defined(VCL_IOSTREAM) // see <sysclass.h> ostream& operator << (ostream& os, const AnsiString& arg); istream& operator >> (istream& is, AnsiString& arg); #endif #if !defined(__CODEGUARD__) // Codeguard is not very happy about our "reverse indexing" of the // Data pointer. We'll address this by violating the ODR: when // Codeguard compile checks are enabled, these methods will not be // inlined. When building dstring.cpp, __DSTRING_INLINE will be // defined to generate out-of-line implementations of these methods. #if !defined(__DSTRING_INLINE) #define __DSTRING_INLINE inline #endif __DSTRING_INLINE const AnsiString::StrRec &AnsiString::GetRec() const { return reinterpret_cast<const StrRec *>(Data)[-1]; } __DSTRING_INLINE AnsiString::StrRec &AnsiString::GetRec() { return reinterpret_cast<StrRec *>(Data)[-1]; } __DSTRING_INLINE int __fastcall AnsiString::Length() const { return (Data)? GetRec().length : 0; } #undef __DSTRING_INLINE #endif // !defined(__CODEGUARD__) ///////////////////////////////////////////////////////////////////////////// // SmallStringBase ///////////////////////////////////////////////////////////////////////////// template <unsigned char sz> class SmallStringBase { protected: unsigned char Len; char Data[sz]; }; ///////////////////////////////////////////////////////////////////////////// // SmallString ///////////////////////////////////////////////////////////////////////////// template <unsigned char sz> class SmallString : SmallStringBase<sz> { public: __fastcall SmallString() { Len = 0; } __fastcall SmallString(const SmallString& src); __fastcall SmallString(const char* src); __fastcall SmallString(const AnsiString& src) { long len = src.Length(); Len = (unsigned char)((len > sz)? sz: len); strncpy(Data, src.c_str(), Len); } char& __fastcall operator [](const unsigned char idx) {return Data[idx-1];} SmallString& __fastcall operator =(const SmallString& rhs); __fastcall operator AnsiString() const; }; // used when SmallStrings are in unions (can't have a ctor) // must cast DummySmallString to SmallString to do anything useful template<unsigned char sz> __fastcall SmallString<sz>::SmallString(const char* src) { long len = strlen(src); Len = (unsigned char)((len > sz)? sz: len); strncpy(Data, src, Len); } template<unsigned char sz> __fastcall SmallString<sz>::SmallString(const SmallString& src) { Len = src.Len; for (int i = 0; i < Len; i++) Data[i] = src.Data[i]; } template<unsigned char sz> SmallString<sz>& __fastcall SmallString<sz>::operator =(const SmallString& rhs) { if (this != &rhs) { Len = rhs.Len; for (int i = 0; i < Len; i++) Data[i] = rhs.Data[i]; } return *this; } template<unsigned char sz> inline __fastcall SmallString<sz>::operator AnsiString() const { return AnsiString(Data, Len); } #if defined(VCL_IOSTREAM) // see sysclass.h template<unsigned char sz> ostream& operator <<(ostream& os, const SmallString<sz>& arg); template<unsigned char sz> istream& operator >>(istream& is, SmallString<sz>& arg); #endif } using namespace System; // The following is provided for backward compatibility. // Otherwise, the new IntToStr(__int64) causes ambiguity for old code // that used other integral types. // namespace Sysutils { extern PACKAGE AnsiString __fastcall IntToStr(int Value)/* overload */; extern PACKAGE AnsiString __fastcall IntToStr(__int64 Value)/* overload */; } #pragma option push -w-inl inline AnsiString __fastcall IntToStr(bool value) { return Sysutils::IntToStr(int(value)); } inline AnsiString __fastcall IntToStr(unsigned int value) { return Sysutils::IntToStr(int(value)); } inline AnsiString __fastcall IntToStr(long value) { return Sysutils::IntToStr(int(value)); } inline AnsiString __fastcall IntToStr(unsigned long value) { return Sysutils::IntToStr(int(value)); } #pragma option pop #pragma delphiheader end. #endif // DSTRING_H |
《AnsiString字符串头文件》上有1条评论
评论已关闭。