Copies at most length characters from str into a newly-allocated piece of memory of size length+1. The memory is allocated with new[]. A terminating null byte is written to the memory, and a pointer to it is returned. If str is NULL, NULL is returned.
| 2136 | // A terminating null byte is written to the memory, and a pointer to it |
| 2137 | // is returned. If str is NULL, NULL is returned. |
| 2138 | static char* CloneString(const char* str, size_t length) { |
| 2139 | if (str == NULL) { |
| 2140 | return NULL; |
| 2141 | } else { |
| 2142 | char* const clone = new char[length + 1]; |
| 2143 | posix::StrNCpy(clone, str, length); |
| 2144 | clone[length] = '\0'; |
| 2145 | return clone; |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | // Clones a 0-terminated C string, allocating memory using new. The |
| 2150 | // caller is responsible for deleting[] the return value. Returns the |