fixed Str::replace

This commit is contained in:
Noel Berry 2021-04-09 22:57:20 -07:00
parent 78b8140f53
commit 63c2493fd3

View File

@ -404,12 +404,12 @@ Str& Str::replace(const Str& os, const Str& ns)
{
for (int i = 0; i < m_length - os.m_length + 1; i++)
{
if (strcmp(data() + i, os.data()) == 0)
if (strncmp(data() + i, os.data(), os.m_length) == 0)
{
if (ns.m_length > os.m_length)
reserve(ns.m_length - os.m_length);
memcpy(data() + i + os.m_length, data() + i + ns.m_length, m_length - i - os.m_length);
memmove(data() + i + ns.m_length, data() + i + os.m_length, m_length - i - os.m_length);
memcpy(data() + i, ns.cstr(), ns.m_length);
set_length(m_length + ns.m_length - os.m_length);
i += os.m_length - 1;