| 1715 | } |
| 1716 | |
| 1717 | PUGI__FN bool strcpy_insitu(char_t*& dest, uintptr_t& header, uintptr_t header_mask, const char_t* source) |
| 1718 | { |
| 1719 | size_t source_length = strlength(source); |
| 1720 | |
| 1721 | if (source_length == 0) |
| 1722 | { |
| 1723 | // empty string and null pointer are equivalent, so just deallocate old memory |
| 1724 | xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator; |
| 1725 | |
| 1726 | if (header & header_mask) |
| 1727 | alloc->deallocate_string(dest); |
| 1728 | |
| 1729 | // mark the string as not allocated |
| 1730 | dest = 0; |
| 1731 | header &= ~header_mask; |
| 1732 | |
| 1733 | return true; |
| 1734 | } |
| 1735 | else if (dest && strcpy_insitu_allow(source_length, header & header_mask, dest)) |
| 1736 | { |
| 1737 | // we can reuse old buffer, so just copy the new data (including zero terminator) |
| 1738 | memcpy(dest, source, (source_length + 1) * sizeof(char_t)); |
| 1739 | |
| 1740 | return true; |
| 1741 | } |
| 1742 | else |
| 1743 | { |
| 1744 | xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator; |
| 1745 | |
| 1746 | // allocate new buffer |
| 1747 | char_t* buf = alloc->allocate_string(source_length + 1); |
| 1748 | if (!buf) |
| 1749 | return false; |
| 1750 | |
| 1751 | // copy the string (including zero terminator) |
| 1752 | memcpy(buf, source, (source_length + 1) * sizeof(char_t)); |
| 1753 | |
| 1754 | // deallocate old buffer (*after* the above to protect against overlapping memory and/or allocation failures) |
| 1755 | if (header & header_mask) |
| 1756 | alloc->deallocate_string(dest); |
| 1757 | |
| 1758 | // the string is now allocated, so set the flag |
| 1759 | dest = buf; |
| 1760 | header |= header_mask; |
| 1761 | |
| 1762 | return true; |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | struct gap |
| 1767 | { |
no test coverage detected