Changeset 26753 in vbox for trunk/include/iprt
- Timestamp:
- Feb 24, 2010 4:24:33 PM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 58004
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/ministring.h
r26603 r26753 45 45 * else except IPRT memory management functions. Semantics are like in 46 46 * std::string, except it can do a lot less. 47 * 48 * 49 * Note that MiniString does not differentiate between NULL strings and 50 * empty strings. In other words, MiniString("") and MiniString(NULL) 51 * behave the same. In both cases, MiniString allocates no memory, reports 52 * a zero length and zero allocated bytes for both, and returns an empty 53 * C string from c_str(). 47 54 */ 48 55 #ifdef VBOX … … 71 78 * Creates a copy of another MiniString. 72 79 * 73 * This allocates s.length() + 1 bytes for the new instance .80 * This allocates s.length() + 1 bytes for the new instance, unless s is empty. 74 81 * 75 82 * @param s The source string. … … 83 90 84 91 /** 85 * Creates a copy of a nother MiniString.86 * 87 * This allocates strlen(pcsz) + 1 bytes for the new instance .92 * Creates a copy of a C string. 93 * 94 * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty. 88 95 * 89 96 * @param pcsz The source string. … … 123 130 * 124 131 * Returns the number of bytes allocated in the internal string buffer, which is 125 * at least length() + 1 if length() > 0 .132 * at least length() + 1 if length() > 0; for an empty string, this returns 0. 126 133 * 127 134 * @returns m_cbAllocated. … … 172 179 173 180 /** 181 * Assigns a copy of pcsz to "this". 182 * 183 * @param pcsz The source string. 184 * 185 * @throws std::bad_alloc On allocation failure. The object is left describing 186 * a NULL string. 187 * 188 * @returns Reference to the object. 189 */ 190 MiniString &operator=(const char *pcsz) 191 { 192 if (m_psz != pcsz) 193 { 194 cleanup(); 195 copyFrom(pcsz); 196 } 197 return *this; 198 } 199 200 /** 201 * Assigns a copy of s to "this". 202 * 203 * @param s The source string. 204 * 205 * @throws std::bad_alloc On allocation failure. The object is left describing 206 * a NULL string. 207 * 208 * @returns Reference to the object. 209 */ 210 MiniString &operator=(const MiniString &s) 211 { 212 if (this != &s) 213 { 214 cleanup(); 215 copyFrom(s); 216 } 217 return *this; 218 } 219 220 /** 221 * Appends the string "that" to "this". 222 * 223 * @param that The string to append. 224 * 225 * @throws std::bad_alloc On allocation error. The object is left unchanged. 226 * 227 * @returns Reference to the object. 228 */ 229 MiniString &append(const MiniString &that); 230 231 /** 232 * Appends the given character to "this". 233 * 234 * @param c The character to append. 235 * 236 * @throws std::bad_alloc On allocation error. The object is left unchanged. 237 * 238 * @returns Reference to the object. 239 */ 240 MiniString &append(char c); 241 242 /** 243 * Index operator. 244 * 245 * Returns the byte at the given index, or a null byte if the index is not 246 * smaller than length(). This does _not_ count codepoints but simply points 247 * into the member C string. 248 * 249 * @param i The index into the string buffer. 250 * @returns char at the index or null. 251 */ 252 inline char operator[](size_t i) const 253 { 254 if (i < length()) 255 return m_psz[i]; 256 return '\0'; 257 } 258 259 /** 260 * Returns the contained string as a C-style const char* pointer. 261 * This never returns NULL; if the string is empty, this returns a 262 * pointer to static null byte. 263 * 264 * @returns const pointer to C-style string. 265 */ 266 inline const char *c_str() const 267 { 268 return (m_psz) ? m_psz : ""; 269 } 270 271 /** 272 * Like c_str(), for compatibility with lots of VirtualBox Main code. 273 * 274 * @returns const pointer to C-style string. 275 */ 276 inline const char *raw() const 277 { 278 return (m_psz) ? m_psz : ""; 279 } 280 281 /** 174 282 * Returns a non-const raw pointer that allows to modify the string directly. 283 * As opposed to c_str() and raw(), this DOES return NULL for an empty string 284 * because we cannot return a non-const pointer to a static "" global. 175 285 * 176 286 * @warning … … 209 319 210 320 /** 211 * Assigns a copy of pcsz to "this". 212 * 213 * @param pcsz The source string. 214 * 215 * @throws std::bad_alloc On allocation failure. The object is left describing 216 * a NULL string. 217 * 218 * @returns Reference to the object. 219 */ 220 MiniString &operator=(const char *pcsz) 221 { 222 if (m_psz != pcsz) 223 { 224 cleanup(); 225 copyFrom(pcsz); 226 } 227 return *this; 228 } 229 230 /** 231 * Assigns a copy of s to "this". 232 * 233 * @param s The source string. 234 * 235 * @throws std::bad_alloc On allocation failure. The object is left describing 236 * a NULL string. 237 * 238 * @returns Reference to the object. 239 */ 240 MiniString &operator=(const MiniString &s) 241 { 242 if (this != &s) 243 { 244 cleanup(); 245 copyFrom(s); 246 } 247 return *this; 248 } 249 250 /** 251 * Appends the string "that" to "this". 252 * 253 * @param that The string to append. 254 * 255 * @throws std::bad_alloc On allocation error. The object is left unchanged. 256 * 257 * @returns Reference to the object. 258 */ 259 MiniString &append(const MiniString &that); 260 261 /** 262 * Appends the given character to "this". 263 * 264 * @param c The character to append. 265 * 266 * @throws std::bad_alloc On allocation error. The object is left unchanged. 267 * 268 * @returns Reference to the object. 269 */ 270 MiniString &append(char c); 271 272 /** 273 * Index operator. 274 * 275 * Returns the byte at the given index, or a null byte if the index is not 276 * smaller than length(). This does _not_ count codepoints but simply points 277 * into the member C string. 278 * 279 * @param i The index into the string buffer. 280 * @returns char at the index or null. 281 */ 282 inline char operator[](size_t i) const 283 { 284 if (i < length()) 285 return m_psz[i]; 286 return '\0'; 287 } 288 289 /** 290 * Returns the contained string as a C-style const char* pointer. 291 * 292 * @returns const pointer to C-style string. 293 */ 294 inline const char *c_str() const 295 { 296 return m_psz; 297 } 298 299 /** 300 * Like c_str(), for compatibility with lots of VirtualBox Main code. 301 * 302 * @returns const pointer to C-style string. 303 */ 304 inline const char *raw() const 305 { 306 return m_psz; 307 } 308 309 /** 310 * Emptry string or not? 311 * 312 * Returns true if the member string has no length. This states nothing about 313 * how much memory might be allocated. 321 * Returns true if the member string has no length. 322 * This is true for instances created from both NULL and "" input strings. 323 * 324 * This states nothing about how much memory might be allocated. 314 325 * 315 326 * @returns true if empty, false if not. … … 474 485 * Hide operator bool() to force people to use isEmpty() explicitly. 475 486 */ 476 operator bool() const { return false; }487 operator bool() const; 477 488 478 489 /** … … 492 503 493 504 /** 494 * Protected internal helper for copy a string that completely ignors the495 * current object state.505 * Protected internal helper to copy a string. This ignores the previous object 506 * state, so either call this from a constructor or call cleanup() first. 496 507 * 497 508 * copyFrom() unconditionally sets the members to a copy of the given other … … 501 512 * 502 513 * This variant copies from another MiniString and is fast since 503 * the length of source string is known.514 * the length of the source string is known. 504 515 * 505 516 * @param s The source string. … … 533 544 534 545 /** 535 * Protected internal helper for copy a string that completely ignors the536 * current object state.546 * Protected internal helper to copy a string. This ignores the previous object 547 * state, so either call this from a constructor or call cleanup() first. 537 548 * 538 549 * See copyFrom() above. … … 548 559 void copyFrom(const char *pcsz) 549 560 { 550 if (pcsz )561 if (pcsz && *pcsz) 551 562 { 552 563 m_cbLength = strlen(pcsz);
Note:
See TracChangeset
for help on using the changeset viewer.