Like str->resize(new_size), except any new characters added to "*str" as a result of resizing may be left uninitialized, rather than being filled with '0' bytes. Typically used when code is then going to overwrite the backing store of the string with known data.
| 267 | // than being filled with '0' bytes. Typically used when code is then |
| 268 | // going to overwrite the backing store of the string with known data. |
| 269 | inline void STLStringResizeUninitialized(string* s, size_t new_size) { |
| 270 | if (sizeof(*s) == sizeof(InternalStringRepGCC4)) { |
| 271 | if (new_size > s->capacity()) { |
| 272 | s->reserve(new_size); |
| 273 | } |
| 274 | // The line below depends on the layout of 'string'. THIS IS |
| 275 | // NON-PORTABLE CODE. If our STL implementation changes, we will |
| 276 | // need to change this as well. |
| 277 | InternalStringRepGCC4* rep = reinterpret_cast<InternalStringRepGCC4*>(s); |
| 278 | assert(rep->_M_data == s->data()); |
| 279 | assert(rep->_M_string_length == s->size()); |
| 280 | |
| 281 | // We have to null-terminate the string for c_str() to work properly. |
| 282 | // So we leave the actual contents of the string uninitialized, but |
| 283 | // we set the byte one past the new end of the string to '\0' |
| 284 | const_cast<char*>(s->data())[new_size] = '\0'; |
| 285 | rep->_M_string_length = new_size; |
| 286 | } else { |
| 287 | // Slow path: have to reallocate stuff, or an unknown string rep |
| 288 | s->resize(new_size); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Returns true if the string implementation supports a resize where |
| 293 | // the new characters added to the string are left untouched. |
no test coverage detected