Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #ifndef CORE_FXCRT_INCLUDE_FX_STRING_H_ | 7 #ifndef CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| 8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ | 8 #define CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| 9 | 9 |
| 10 #include <stdint.h> // For intptr_t. | 10 #include <stdint.h> // For intptr_t. |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 #include "core/fxcrt/include/cfx_retain_ptr.h" | |
| 13 #include "core/fxcrt/include/fx_memory.h" | 14 #include "core/fxcrt/include/fx_memory.h" |
| 14 #include "core/fxcrt/include/fx_system.h" | 15 #include "core/fxcrt/include/fx_system.h" |
| 15 | 16 |
| 16 class CFX_BinaryBuf; | 17 class CFX_BinaryBuf; |
| 17 class CFX_ByteString; | 18 class CFX_ByteString; |
| 18 class CFX_WideString; | 19 class CFX_WideString; |
| 19 | 20 |
| 20 // An immutable string with caller-provided storage which must outlive the | 21 // An immutable string with caller-provided storage which must outlive the |
| 21 // string itself. | 22 // string itself. |
| 22 class CFX_ByteStringC { | 23 class CFX_ByteStringC { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 FXSYS_memcmp(other.m_Ptr, m_Ptr, m_Length) == 0; | 86 FXSYS_memcmp(other.m_Ptr, m_Ptr, m_Length) == 0; |
| 86 } | 87 } |
| 87 bool operator!=(const char* ptr) const { return !(*this == ptr); } | 88 bool operator!=(const char* ptr) const { return !(*this == ptr); } |
| 88 bool operator!=(const CFX_ByteStringC& other) const { | 89 bool operator!=(const CFX_ByteStringC& other) const { |
| 89 return !(*this == other); | 90 return !(*this == other); |
| 90 } | 91 } |
| 91 | 92 |
| 92 uint32_t GetID(FX_STRSIZE start_pos = 0) const; | 93 uint32_t GetID(FX_STRSIZE start_pos = 0) const; |
| 93 | 94 |
| 94 const uint8_t* GetPtr() const { return m_Ptr; } | 95 const uint8_t* GetPtr() const { return m_Ptr; } |
| 95 | |
| 96 const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } | 96 const FX_CHAR* GetCStr() const { return (const FX_CHAR*)m_Ptr; } |
| 97 | 97 |
| 98 FX_STRSIZE GetLength() const { return m_Length; } | 98 FX_STRSIZE GetLength() const { return m_Length; } |
| 99 | |
| 100 bool IsEmpty() const { return m_Length == 0; } | 99 bool IsEmpty() const { return m_Length == 0; } |
| 101 | 100 |
| 102 uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | 101 uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } |
| 103 | 102 |
| 104 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { | 103 CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const { |
| 105 if (index < 0) { | 104 if (index < 0) { |
| 106 index = 0; | 105 index = 0; |
| 107 } | 106 } |
| 108 if (index > m_Length) { | 107 if (index > m_Length) { |
| 109 return CFX_ByteStringC(); | 108 return CFX_ByteStringC(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 135 return rhs != lhs; | 134 return rhs != lhs; |
| 136 } | 135 } |
| 137 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) | 136 #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) |
| 138 | 137 |
| 139 // A mutable string with shared buffers using copy-on-write semantics that | 138 // A mutable string with shared buffers using copy-on-write semantics that |
| 140 // avoids the cost of std::string's iterator stability guarantees. | 139 // avoids the cost of std::string's iterator stability guarantees. |
| 141 class CFX_ByteString { | 140 class CFX_ByteString { |
| 142 public: | 141 public: |
| 143 typedef FX_CHAR value_type; | 142 typedef FX_CHAR value_type; |
| 144 | 143 |
| 145 CFX_ByteString() : m_pData(nullptr) {} | 144 CFX_ByteString() {} |
| 146 | 145 CFX_ByteString(const CFX_ByteString& other) : m_pData(other.m_pData) {} |
| 147 // Copy constructor. | 146 CFX_ByteString(CFX_ByteString&& other) { m_pData.Swap(other.m_pData); } |
| 148 CFX_ByteString(const CFX_ByteString& str); | |
| 149 | |
| 150 // Move constructor. | |
| 151 inline CFX_ByteString(CFX_ByteString&& other) { | |
| 152 m_pData = other.m_pData; | |
| 153 other.m_pData = nullptr; | |
| 154 } | |
| 155 | 147 |
| 156 CFX_ByteString(char ch); | 148 CFX_ByteString(char ch); |
| 157 CFX_ByteString(const FX_CHAR* ptr) | 149 CFX_ByteString(const FX_CHAR* ptr) |
| 158 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} | 150 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} |
| 159 | 151 |
| 160 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); | 152 CFX_ByteString(const FX_CHAR* ptr, FX_STRSIZE len); |
| 161 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); | 153 CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); |
| 162 | 154 |
| 163 CFX_ByteString(const CFX_ByteStringC& bstrc); | 155 CFX_ByteString(const CFX_ByteStringC& bstrc); |
| 164 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); | 156 CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); |
| 165 | 157 |
| 166 ~CFX_ByteString(); | 158 ~CFX_ByteString(); |
| 167 | 159 |
| 160 void Empty() { m_pData.Reset(); } | |
|
Lei Zhang
2016/03/30 00:15:33
I hope we can rename this to clear() to be closer
Tom Sepez
2016/03/30 18:49:18
Added clear().
| |
| 161 | |
| 168 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); | 162 static CFX_ByteString FromUnicode(const FX_WCHAR* ptr, FX_STRSIZE len = -1); |
| 169 | |
| 170 static CFX_ByteString FromUnicode(const CFX_WideString& str); | 163 static CFX_ByteString FromUnicode(const CFX_WideString& str); |
| 171 | 164 |
| 172 // Explicit conversion to C-style string. | 165 // Explicit conversion to C-style string. |
| 173 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } | 166 const FX_CHAR* c_str() const { return m_pData ? m_pData->m_String : ""; } |
| 174 | 167 |
| 175 // Implicit conversion to C-style string -- deprecated. | 168 // Implicit conversion to C-style string -- deprecated. |
| 176 operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } | 169 operator const FX_CHAR*() const { return m_pData ? m_pData->m_String : ""; } |
| 177 | 170 |
| 178 // Explicit conversion to uint8_t*. | 171 // Explicit conversion to uint8_t*. |
| 179 const uint8_t* raw_str() const { | 172 const uint8_t* raw_str() const { |
| 180 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | 173 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) |
| 181 : nullptr; | 174 : nullptr; |
| 182 } | 175 } |
| 183 | 176 |
| 184 // Implicit conversiont to uint8_t* -- deprecated. | 177 // Implicit conversiont to uint8_t* -- deprecated. |
| 185 operator const uint8_t*() const { | 178 operator const uint8_t*() const { |
| 186 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) | 179 return m_pData ? reinterpret_cast<const uint8_t*>(m_pData->m_String) |
| 187 : nullptr; | 180 : nullptr; |
| 188 } | 181 } |
| 189 | 182 |
| 190 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 183 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } |
| 191 | |
| 192 bool IsEmpty() const { return !GetLength(); } | 184 bool IsEmpty() const { return !GetLength(); } |
| 193 | 185 |
| 194 int Compare(const CFX_ByteStringC& str) const; | 186 int Compare(const CFX_ByteStringC& str) const; |
| 195 | 187 |
| 196 bool Equal(const char* ptr) const; | 188 bool Equal(const char* ptr) const; |
| 197 bool Equal(const CFX_ByteStringC& str) const; | 189 bool Equal(const CFX_ByteStringC& str) const; |
| 198 bool Equal(const CFX_ByteString& other) const; | 190 bool Equal(const CFX_ByteString& other) const; |
| 199 | 191 |
| 200 bool EqualNoCase(const CFX_ByteStringC& str) const; | 192 bool EqualNoCase(const CFX_ByteStringC& str) const; |
| 201 | 193 |
| 202 bool operator==(const char* ptr) const { return Equal(ptr); } | 194 bool operator==(const char* ptr) const { return Equal(ptr); } |
| 203 bool operator==(const CFX_ByteStringC& str) const { return Equal(str); } | 195 bool operator==(const CFX_ByteStringC& str) const { return Equal(str); } |
| 204 bool operator==(const CFX_ByteString& other) const { return Equal(other); } | 196 bool operator==(const CFX_ByteString& other) const { return Equal(other); } |
| 205 | 197 |
| 206 bool operator!=(const char* ptr) const { return !(*this == ptr); } | 198 bool operator!=(const char* ptr) const { return !(*this == ptr); } |
| 207 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } | 199 bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } |
| 208 bool operator!=(const CFX_ByteString& other) const { | 200 bool operator!=(const CFX_ByteString& other) const { |
| 209 return !(*this == other); | 201 return !(*this == other); |
| 210 } | 202 } |
| 211 | 203 |
| 212 bool operator<(const CFX_ByteString& str) const { | 204 bool operator<(const CFX_ByteString& str) const { |
| 213 int result = FXSYS_memcmp(c_str(), str.c_str(), | 205 int result = FXSYS_memcmp(c_str(), str.c_str(), |
| 214 std::min(GetLength(), str.GetLength())); | 206 std::min(GetLength(), str.GetLength())); |
| 215 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | 207 return result < 0 || (result == 0 && GetLength() < str.GetLength()); |
| 216 } | 208 } |
| 217 | 209 |
| 218 void Empty(); | |
| 219 | |
| 220 const CFX_ByteString& operator=(const FX_CHAR* str); | 210 const CFX_ByteString& operator=(const FX_CHAR* str); |
| 221 | |
| 222 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); | 211 const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); |
| 223 | |
| 224 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); | 212 const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); |
| 225 | |
| 226 const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); | 213 const CFX_ByteString& operator=(const CFX_BinaryBuf& buf); |
| 227 | 214 |
| 228 void Load(const uint8_t* str, FX_STRSIZE len); | 215 void Load(const uint8_t* str, FX_STRSIZE len); |
| 229 | 216 |
| 230 const CFX_ByteString& operator+=(FX_CHAR ch); | 217 const CFX_ByteString& operator+=(FX_CHAR ch); |
| 231 | |
| 232 const CFX_ByteString& operator+=(const FX_CHAR* str); | 218 const CFX_ByteString& operator+=(const FX_CHAR* str); |
| 233 | |
| 234 const CFX_ByteString& operator+=(const CFX_ByteString& str); | 219 const CFX_ByteString& operator+=(const CFX_ByteString& str); |
| 235 | |
| 236 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); | 220 const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); |
| 237 | 221 |
| 238 uint8_t GetAt(FX_STRSIZE nIndex) const { | 222 uint8_t GetAt(FX_STRSIZE nIndex) const { |
| 239 return m_pData ? m_pData->m_String[nIndex] : 0; | 223 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 240 } | 224 } |
| 241 | 225 |
| 242 uint8_t operator[](FX_STRSIZE nIndex) const { | 226 uint8_t operator[](FX_STRSIZE nIndex) const { |
| 243 return m_pData ? m_pData->m_String[nIndex] : 0; | 227 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 244 } | 228 } |
| 245 | 229 |
| 246 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); | 230 void SetAt(FX_STRSIZE nIndex, FX_CHAR ch); |
| 247 | |
| 248 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); | 231 FX_STRSIZE Insert(FX_STRSIZE index, FX_CHAR ch); |
| 249 | |
| 250 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 232 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
| 251 | 233 |
| 252 void Format(const FX_CHAR* lpszFormat, ...); | 234 void Format(const FX_CHAR* lpszFormat, ...); |
| 253 | |
| 254 void FormatV(const FX_CHAR* lpszFormat, va_list argList); | 235 void FormatV(const FX_CHAR* lpszFormat, va_list argList); |
| 255 | 236 |
| 256 void Reserve(FX_STRSIZE len); | 237 void Reserve(FX_STRSIZE len); |
| 257 | |
| 258 FX_CHAR* GetBuffer(FX_STRSIZE len); | 238 FX_CHAR* GetBuffer(FX_STRSIZE len); |
| 259 | |
| 260 void ReleaseBuffer(FX_STRSIZE len = -1); | 239 void ReleaseBuffer(FX_STRSIZE len = -1); |
| 261 | 240 |
| 262 CFX_ByteString Mid(FX_STRSIZE first) const; | 241 CFX_ByteString Mid(FX_STRSIZE first) const; |
| 263 | |
| 264 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | 242 CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; |
| 265 | |
| 266 CFX_ByteString Left(FX_STRSIZE count) const; | 243 CFX_ByteString Left(FX_STRSIZE count) const; |
| 267 | |
| 268 CFX_ByteString Right(FX_STRSIZE count) const; | 244 CFX_ByteString Right(FX_STRSIZE count) const; |
| 269 | 245 |
| 270 FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; | 246 FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; |
| 271 | |
| 272 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; | 247 FX_STRSIZE Find(FX_CHAR ch, FX_STRSIZE start = 0) const; |
| 273 | |
| 274 FX_STRSIZE ReverseFind(FX_CHAR ch) const; | 248 FX_STRSIZE ReverseFind(FX_CHAR ch) const; |
| 275 | 249 |
| 276 void MakeLower(); | 250 void MakeLower(); |
| 277 | |
| 278 void MakeUpper(); | 251 void MakeUpper(); |
| 279 | 252 |
| 280 void TrimRight(); | 253 void TrimRight(); |
| 281 | |
| 282 void TrimRight(FX_CHAR chTarget); | 254 void TrimRight(FX_CHAR chTarget); |
| 283 | |
| 284 void TrimRight(const CFX_ByteStringC& lpszTargets); | 255 void TrimRight(const CFX_ByteStringC& lpszTargets); |
| 285 | 256 |
| 286 void TrimLeft(); | 257 void TrimLeft(); |
| 287 | |
| 288 void TrimLeft(FX_CHAR chTarget); | 258 void TrimLeft(FX_CHAR chTarget); |
| 289 | |
| 290 void TrimLeft(const CFX_ByteStringC& lpszTargets); | 259 void TrimLeft(const CFX_ByteStringC& lpszTargets); |
| 291 | 260 |
| 292 FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, | 261 FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, |
| 293 const CFX_ByteStringC& lpszNew); | 262 const CFX_ByteStringC& lpszNew); |
| 294 | 263 |
| 295 FX_STRSIZE Remove(FX_CHAR ch); | 264 FX_STRSIZE Remove(FX_CHAR ch); |
| 296 | 265 |
| 297 CFX_WideString UTF8Decode() const; | 266 CFX_WideString UTF8Decode() const; |
| 298 | 267 |
| 299 uint32_t GetID(FX_STRSIZE start_pos = 0) const; | 268 uint32_t GetID(FX_STRSIZE start_pos = 0) const; |
| 300 | 269 |
| 301 #define FXFORMAT_SIGNED 1 | 270 #define FXFORMAT_SIGNED 1 |
| 302 #define FXFORMAT_HEX 2 | 271 #define FXFORMAT_HEX 2 |
| 303 #define FXFORMAT_CAPITAL 4 | 272 #define FXFORMAT_CAPITAL 4 |
| 304 | 273 |
| 305 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); | 274 static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); |
| 306 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); | 275 static CFX_ByteString FormatFloat(FX_FLOAT f, int precision = 0); |
| 307 | 276 |
| 308 protected: | 277 protected: |
| 309 // To ensure ref counts do not overflow, consider the worst possible case: | |
| 310 // the entire address space contains nothing but pointers to this object. | |
| 311 // Since the count increments with each new pointer, the largest value is | |
| 312 // the number of pointers that can fit into the address space. The size of | |
| 313 // the address space itself is a good upper bound on it; we need not go | |
| 314 // larger. | |
| 315 class StringData { | 278 class StringData { |
| 316 public: | 279 public: |
| 317 static StringData* Create(int nLen); | 280 static StringData* Create(FX_STRSIZE nLen); |
| 281 static StringData* Create(const StringData& other); | |
| 282 static StringData* Create(const FX_CHAR* pStr, FX_STRSIZE nLen); | |
| 283 | |
| 318 void Retain() { ++m_nRefs; } | 284 void Retain() { ++m_nRefs; } |
| 319 void Release() { | 285 void Release() { |
| 320 if (--m_nRefs <= 0) | 286 if (--m_nRefs <= 0) |
| 321 FX_Free(this); | 287 FX_Free(this); |
| 322 } | 288 } |
| 323 | 289 |
| 290 bool CanOperateInPlace(FX_STRSIZE nTotalLen) { | |
|
Lei Zhang
2016/03/30 00:15:33
const method
Tom Sepez
2016/03/30 18:49:18
Done.
| |
| 291 return m_nRefs <= 1 && nTotalLen <= m_nAllocLength; | |
| 292 } | |
| 293 | |
| 294 void CopyContents(const StringData& other); | |
| 295 void CopyContents(const FX_CHAR* pStr, FX_STRSIZE nLen); | |
| 296 void CopyContentsAt(FX_STRSIZE offset, | |
| 297 const FX_CHAR* pStr, | |
| 298 FX_STRSIZE nLen); | |
| 299 | |
| 300 // To ensure ref counts do not overflow, consider the worst possible case: | |
| 301 // the entire address space contains nothing but pointers to this object. | |
| 302 // Since the count increments with each new pointer, the largest value is | |
| 303 // the number of pointers that can fit into the address space. The size of | |
| 304 // the address space itself is a good upper bound on it. | |
| 324 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. | 305 intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. |
| 306 | |
| 307 // |FX_STRSIZE| is currently typedef'd as in |int|. | |
|
dsinclair
2016/03/30 13:27:20
nit: s/in/an
| |
| 308 // TODO(palmer): It should be a |size_t|, or at least unsigned. | |
| 325 FX_STRSIZE m_nDataLength; | 309 FX_STRSIZE m_nDataLength; |
| 326 FX_STRSIZE m_nAllocLength; | 310 FX_STRSIZE m_nAllocLength; |
| 311 | |
| 312 // Not really 1, variable size. | |
| 327 FX_CHAR m_String[1]; | 313 FX_CHAR m_String[1]; |
| 328 | 314 |
| 329 private: | 315 private: |
| 330 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen) | 316 StringData(FX_STRSIZE dataLen, FX_STRSIZE allocLen); |
| 331 : m_nRefs(1), m_nDataLength(dataLen), m_nAllocLength(allocLen) { | |
| 332 FXSYS_assert(dataLen >= 0); | |
| 333 FXSYS_assert(allocLen >= 0); | |
| 334 FXSYS_assert(dataLen <= allocLen); | |
| 335 m_String[dataLen] = 0; | |
| 336 } | |
| 337 ~StringData() = delete; | 317 ~StringData() = delete; |
| 338 }; | 318 }; |
| 339 | 319 |
| 320 void CopyBeforeWrite(); | |
| 321 void AllocBeforeWrite(FX_STRSIZE nLen); | |
| 340 void AllocCopy(CFX_ByteString& dest, | 322 void AllocCopy(CFX_ByteString& dest, |
| 341 FX_STRSIZE nCopyLen, | 323 FX_STRSIZE nCopyLen, |
| 342 FX_STRSIZE nCopyIndex) const; | 324 FX_STRSIZE nCopyIndex) const; |
| 343 void AssignCopy(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); | 325 void AssignCopy(const FX_CHAR* pSrcData, FX_STRSIZE nSrcLen); |
| 344 void ConcatCopy(FX_STRSIZE nSrc1Len, | 326 void Concat(const FX_CHAR* lpszSrcData, FX_STRSIZE nSrcLen); |
| 345 const FX_CHAR* lpszSrc1Data, | |
| 346 FX_STRSIZE nSrc2Len, | |
| 347 const FX_CHAR* lpszSrc2Data); | |
| 348 void ConcatInPlace(FX_STRSIZE nSrcLen, const FX_CHAR* lpszSrcData); | |
| 349 void CopyBeforeWrite(); | |
| 350 void AllocBeforeWrite(FX_STRSIZE nLen); | |
| 351 | 327 |
| 352 StringData* m_pData; | 328 CFX_RetainPtr<StringData> m_pData; |
| 353 friend class fxcrt_ByteStringConcatInPlace_Test; | 329 friend class fxcrt_ByteStringConcat_Test; |
| 354 }; | 330 }; |
| 331 | |
| 355 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { | 332 inline CFX_ByteStringC::CFX_ByteStringC(const CFX_ByteString& src) { |
| 356 m_Ptr = (const uint8_t*)src; | 333 m_Ptr = (const uint8_t*)src; |
| 357 m_Length = src.GetLength(); | 334 m_Length = src.GetLength(); |
| 358 } | 335 } |
| 359 inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { | 336 inline CFX_ByteStringC& CFX_ByteStringC::operator=(const CFX_ByteString& src) { |
| 360 m_Ptr = (const uint8_t*)src; | 337 m_Ptr = (const uint8_t*)src; |
| 361 m_Length = src.GetLength(); | 338 m_Length = src.GetLength(); |
| 362 return *this; | 339 return *this; |
| 363 } | 340 } |
| 364 | 341 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 return CFX_ByteString(str1, str2); | 389 return CFX_ByteString(str1, str2); |
| 413 } | 390 } |
| 414 inline CFX_ByteString operator+(const CFX_ByteString& str1, | 391 inline CFX_ByteString operator+(const CFX_ByteString& str1, |
| 415 const CFX_ByteStringC& str2) { | 392 const CFX_ByteStringC& str2) { |
| 416 return CFX_ByteString(str1, str2); | 393 return CFX_ByteString(str1, str2); |
| 417 } | 394 } |
| 418 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, | 395 inline CFX_ByteString operator+(const CFX_ByteStringC& str1, |
| 419 const CFX_ByteString& str2) { | 396 const CFX_ByteString& str2) { |
| 420 return CFX_ByteString(str1, str2); | 397 return CFX_ByteString(str1, str2); |
| 421 } | 398 } |
| 399 | |
| 422 class CFX_WideStringC { | 400 class CFX_WideStringC { |
| 423 public: | 401 public: |
| 424 typedef FX_WCHAR value_type; | 402 typedef FX_WCHAR value_type; |
| 425 | 403 |
| 426 CFX_WideStringC() { | 404 CFX_WideStringC() { |
| 427 m_Ptr = NULL; | 405 m_Ptr = NULL; |
| 428 m_Length = 0; | 406 m_Length = 0; |
| 429 } | 407 } |
| 430 | 408 |
| 431 CFX_WideStringC(const FX_WCHAR* ptr) { | 409 CFX_WideStringC(const FX_WCHAR* ptr) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 } | 447 } |
| 470 bool operator==(const CFX_WideStringC& str) const { | 448 bool operator==(const CFX_WideStringC& str) const { |
| 471 return str.m_Length == m_Length && wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; | 449 return str.m_Length == m_Length && wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; |
| 472 } | 450 } |
| 473 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | 451 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } |
| 474 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | 452 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } |
| 475 | 453 |
| 476 const FX_WCHAR* GetPtr() const { return m_Ptr; } | 454 const FX_WCHAR* GetPtr() const { return m_Ptr; } |
| 477 | 455 |
| 478 FX_STRSIZE GetLength() const { return m_Length; } | 456 FX_STRSIZE GetLength() const { return m_Length; } |
| 479 | |
| 480 bool IsEmpty() const { return m_Length == 0; } | 457 bool IsEmpty() const { return m_Length == 0; } |
| 481 | 458 |
| 482 FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } | 459 FX_WCHAR GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } |
| 483 | 460 |
| 484 CFX_WideStringC Left(FX_STRSIZE count) const { | 461 CFX_WideStringC Left(FX_STRSIZE count) const { |
| 485 if (count < 1) { | 462 if (count < 1) { |
| 486 return CFX_WideStringC(); | 463 return CFX_WideStringC(); |
| 487 } | 464 } |
| 488 if (count > m_Length) { | 465 if (count > m_Length) { |
| 489 count = m_Length; | 466 count = m_Length; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 521 return result < 0 || (result == 0 && m_Length < that.m_Length); | 498 return result < 0 || (result == 0 && m_Length < that.m_Length); |
| 522 } | 499 } |
| 523 | 500 |
| 524 protected: | 501 protected: |
| 525 const FX_WCHAR* m_Ptr; | 502 const FX_WCHAR* m_Ptr; |
| 526 FX_STRSIZE m_Length; | 503 FX_STRSIZE m_Length; |
| 527 | 504 |
| 528 private: | 505 private: |
| 529 void* operator new(size_t) throw() { return NULL; } | 506 void* operator new(size_t) throw() { return NULL; } |
| 530 }; | 507 }; |
| 508 | |
| 531 inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { | 509 inline bool operator==(const wchar_t* lhs, const CFX_WideStringC& rhs) { |
| 532 return rhs == lhs; | 510 return rhs == lhs; |
| 533 } | 511 } |
| 534 inline bool operator!=(const wchar_t* lhs, const CFX_WideStringC& rhs) { | 512 inline bool operator!=(const wchar_t* lhs, const CFX_WideStringC& rhs) { |
| 535 return rhs != lhs; | 513 return rhs != lhs; |
| 536 } | 514 } |
| 537 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) | 515 #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) |
| 538 | 516 |
| 539 // A mutable string with shared buffers using copy-on-write semantics that | 517 // A mutable string with shared buffers using copy-on-write semantics that |
| 540 // avoids the cost of std::string's iterator stability guarantees. | 518 // avoids the cost of std::string's iterator stability guarantees. |
| 541 class CFX_WideString { | 519 class CFX_WideString { |
| 542 public: | 520 public: |
| 543 typedef FX_WCHAR value_type; | 521 typedef FX_WCHAR value_type; |
| 544 | 522 |
| 545 CFX_WideString() : m_pData(nullptr) {} | 523 CFX_WideString() : m_pData(nullptr) {} |
| 546 | 524 |
| 547 // Copy constructor. | 525 // Copy constructor. |
| 548 CFX_WideString(const CFX_WideString& str); | 526 CFX_WideString(const CFX_WideString& str); |
| 549 | 527 |
| 550 // Move constructor. | 528 // Move constructor. |
| 551 inline CFX_WideString(CFX_WideString&& other) { | 529 CFX_WideString(CFX_WideString&& other) { |
| 552 m_pData = other.m_pData; | 530 m_pData = other.m_pData; |
| 553 other.m_pData = nullptr; | 531 other.m_pData = nullptr; |
| 554 } | 532 } |
| 555 | 533 |
| 556 CFX_WideString(const FX_WCHAR* ptr) | 534 CFX_WideString(const FX_WCHAR* ptr) |
| 557 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} | 535 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} |
| 558 | 536 |
| 559 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); | 537 CFX_WideString(const FX_WCHAR* ptr, FX_STRSIZE len); |
| 560 | |
| 561 CFX_WideString(FX_WCHAR ch); | 538 CFX_WideString(FX_WCHAR ch); |
| 562 | 539 |
| 563 CFX_WideString(const CFX_WideStringC& str); | 540 CFX_WideString(const CFX_WideStringC& str); |
| 564 | |
| 565 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); | 541 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); |
| 566 | 542 |
| 567 ~CFX_WideString(); | 543 ~CFX_WideString(); |
| 568 | 544 |
| 569 static CFX_WideString FromLocal(const CFX_ByteString& str); | 545 static CFX_WideString FromLocal(const CFX_ByteString& str); |
| 570 | |
| 571 static CFX_WideString FromCodePage(const CFX_ByteString& str, | 546 static CFX_WideString FromCodePage(const CFX_ByteString& str, |
| 572 uint16_t codepage); | 547 uint16_t codepage); |
| 573 | 548 |
| 574 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); | 549 static CFX_WideString FromUTF8(const char* str, FX_STRSIZE len); |
| 575 | |
| 576 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); | 550 static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); |
| 577 | 551 |
| 578 static FX_STRSIZE WStringLength(const unsigned short* str); | 552 static FX_STRSIZE WStringLength(const unsigned short* str); |
| 579 | 553 |
| 580 // Explicit conversion to C-style wide string. | 554 // Explicit conversion to C-style wide string. |
| 581 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } | 555 const FX_WCHAR* c_str() const { return m_pData ? m_pData->m_String : L""; } |
| 582 | 556 |
| 583 // Implicit conversion to C-style wide string -- deprecated. | 557 // Implicit conversion to C-style wide string -- deprecated. |
| 584 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } | 558 operator const FX_WCHAR*() const { return m_pData ? m_pData->m_String : L""; } |
| 585 | 559 |
| 586 void Empty(); | 560 void Empty(); |
| 587 | 561 |
| 588 bool IsEmpty() const { return !GetLength(); } | 562 bool IsEmpty() const { return !GetLength(); } |
| 589 | |
| 590 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } | 563 FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } |
| 591 | 564 |
| 592 const CFX_WideString& operator=(const FX_WCHAR* str); | 565 const CFX_WideString& operator=(const FX_WCHAR* str); |
| 593 | |
| 594 const CFX_WideString& operator=(const CFX_WideString& stringSrc); | 566 const CFX_WideString& operator=(const CFX_WideString& stringSrc); |
| 595 | |
| 596 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); | 567 const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); |
| 597 | 568 |
| 598 const CFX_WideString& operator+=(const FX_WCHAR* str); | 569 const CFX_WideString& operator+=(const FX_WCHAR* str); |
| 599 | |
| 600 const CFX_WideString& operator+=(FX_WCHAR ch); | 570 const CFX_WideString& operator+=(FX_WCHAR ch); |
| 601 | |
| 602 const CFX_WideString& operator+=(const CFX_WideString& str); | 571 const CFX_WideString& operator+=(const CFX_WideString& str); |
| 603 | |
| 604 const CFX_WideString& operator+=(const CFX_WideStringC& str); | 572 const CFX_WideString& operator+=(const CFX_WideStringC& str); |
| 605 | 573 |
| 606 bool operator==(const wchar_t* ptr) const { return Equal(ptr); } | 574 bool operator==(const wchar_t* ptr) const { return Equal(ptr); } |
| 607 bool operator==(const CFX_WideStringC& str) const { return Equal(str); } | 575 bool operator==(const CFX_WideStringC& str) const { return Equal(str); } |
| 608 bool operator==(const CFX_WideString& other) const { return Equal(other); } | 576 bool operator==(const CFX_WideString& other) const { return Equal(other); } |
| 609 | 577 |
| 610 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } | 578 bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } |
| 611 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } | 579 bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } |
| 612 bool operator!=(const CFX_WideString& other) const { | 580 bool operator!=(const CFX_WideString& other) const { |
| 613 return !(*this == other); | 581 return !(*this == other); |
| 614 } | 582 } |
| 615 | 583 |
| 616 bool operator<(const CFX_WideString& str) const { | 584 bool operator<(const CFX_WideString& str) const { |
| 617 int result = | 585 int result = |
| 618 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); | 586 wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); |
| 619 return result < 0 || (result == 0 && GetLength() < str.GetLength()); | 587 return result < 0 || (result == 0 && GetLength() < str.GetLength()); |
| 620 } | 588 } |
| 621 | 589 |
| 622 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { | 590 FX_WCHAR GetAt(FX_STRSIZE nIndex) const { |
| 623 return m_pData ? m_pData->m_String[nIndex] : 0; | 591 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 624 } | 592 } |
| 625 | 593 |
| 626 FX_WCHAR operator[](FX_STRSIZE nIndex) const { | 594 FX_WCHAR operator[](FX_STRSIZE nIndex) const { |
| 627 return m_pData ? m_pData->m_String[nIndex] : 0; | 595 return m_pData ? m_pData->m_String[nIndex] : 0; |
| 628 } | 596 } |
| 629 | 597 |
| 630 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); | 598 void SetAt(FX_STRSIZE nIndex, FX_WCHAR ch); |
| 631 | 599 |
| 632 int Compare(const FX_WCHAR* str) const; | 600 int Compare(const FX_WCHAR* str) const; |
| 633 | |
| 634 int Compare(const CFX_WideString& str) const; | 601 int Compare(const CFX_WideString& str) const; |
| 635 | |
| 636 int CompareNoCase(const FX_WCHAR* str) const; | 602 int CompareNoCase(const FX_WCHAR* str) const; |
| 637 | 603 |
| 638 bool Equal(const wchar_t* ptr) const; | 604 bool Equal(const wchar_t* ptr) const; |
| 639 bool Equal(const CFX_WideStringC& str) const; | 605 bool Equal(const CFX_WideStringC& str) const; |
| 640 bool Equal(const CFX_WideString& other) const; | 606 bool Equal(const CFX_WideString& other) const; |
| 641 | 607 |
| 642 CFX_WideString Mid(FX_STRSIZE first) const; | 608 CFX_WideString Mid(FX_STRSIZE first) const; |
| 643 | |
| 644 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; | 609 CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; |
| 645 | |
| 646 CFX_WideString Left(FX_STRSIZE count) const; | 610 CFX_WideString Left(FX_STRSIZE count) const; |
| 647 | |
| 648 CFX_WideString Right(FX_STRSIZE count) const; | 611 CFX_WideString Right(FX_STRSIZE count) const; |
| 649 | 612 |
| 650 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch); | 613 FX_STRSIZE Insert(FX_STRSIZE index, FX_WCHAR ch); |
| 651 | |
| 652 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); | 614 FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); |
| 653 | 615 |
| 654 void Format(const FX_WCHAR* lpszFormat, ...); | 616 void Format(const FX_WCHAR* lpszFormat, ...); |
| 655 | |
| 656 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); | 617 void FormatV(const FX_WCHAR* lpszFormat, va_list argList); |
| 657 | 618 |
| 658 void MakeLower(); | 619 void MakeLower(); |
| 659 | |
| 660 void MakeUpper(); | 620 void MakeUpper(); |
| 661 | 621 |
| 662 void TrimRight(); | 622 void TrimRight(); |
| 663 | |
| 664 void TrimRight(FX_WCHAR chTarget); | 623 void TrimRight(FX_WCHAR chTarget); |
| 665 | |
| 666 void TrimRight(const FX_WCHAR* lpszTargets); | 624 void TrimRight(const FX_WCHAR* lpszTargets); |
| 667 | 625 |
| 668 void TrimLeft(); | 626 void TrimLeft(); |
| 669 | |
| 670 void TrimLeft(FX_WCHAR chTarget); | 627 void TrimLeft(FX_WCHAR chTarget); |
| 671 | |
| 672 void TrimLeft(const FX_WCHAR* lpszTargets); | 628 void TrimLeft(const FX_WCHAR* lpszTargets); |
| 673 | 629 |
| 674 void Reserve(FX_STRSIZE len); | 630 void Reserve(FX_STRSIZE len); |
| 675 | |
| 676 FX_WCHAR* GetBuffer(FX_STRSIZE len); | 631 FX_WCHAR* GetBuffer(FX_STRSIZE len); |
| 677 | |
| 678 void ReleaseBuffer(FX_STRSIZE len = -1); | 632 void ReleaseBuffer(FX_STRSIZE len = -1); |
| 679 | 633 |
| 680 int GetInteger() const; | 634 int GetInteger() const; |
| 681 | |
| 682 FX_FLOAT GetFloat() const; | 635 FX_FLOAT GetFloat() const; |
| 683 | 636 |
| 684 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; | 637 FX_STRSIZE Find(const FX_WCHAR* lpszSub, FX_STRSIZE start = 0) const; |
| 685 | |
| 686 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; | 638 FX_STRSIZE Find(FX_WCHAR ch, FX_STRSIZE start = 0) const; |
| 687 | |
| 688 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); | 639 FX_STRSIZE Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew); |
| 689 | |
| 690 FX_STRSIZE Remove(FX_WCHAR ch); | 640 FX_STRSIZE Remove(FX_WCHAR ch); |
| 691 | 641 |
| 692 CFX_ByteString UTF8Encode() const; | 642 CFX_ByteString UTF8Encode() const; |
| 693 | |
| 694 CFX_ByteString UTF16LE_Encode() const; | 643 CFX_ByteString UTF16LE_Encode() const; |
| 695 | 644 |
| 696 protected: | 645 protected: |
| 697 class StringData { | 646 class StringData { |
| 698 public: | 647 public: |
| 699 static StringData* Create(int nLen); | 648 static StringData* Create(int nLen); |
| 700 void Retain() { ++m_nRefs; } | 649 void Retain() { ++m_nRefs; } |
| 701 void Release() { | 650 void Release() { |
| 702 if (--m_nRefs <= 0) | 651 if (--m_nRefs <= 0) |
| 703 FX_Free(this); | 652 FX_Free(this); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 810 } | 759 } |
| 811 | 760 |
| 812 FX_FLOAT FX_atof(const CFX_ByteStringC& str); | 761 FX_FLOAT FX_atof(const CFX_ByteStringC& str); |
| 813 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { | 762 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) { |
| 814 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); | 763 return FX_atof(FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength())); |
| 815 } | 764 } |
| 816 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); | 765 void FX_atonum(const CFX_ByteStringC& str, FX_BOOL& bInteger, void* pData); |
| 817 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); | 766 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); |
| 818 | 767 |
| 819 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ | 768 #endif // CORE_FXCRT_INCLUDE_FX_STRING_H_ |
| OLD | NEW |