Files
core_zero/tjp/core/string/replace.cpp
Timothy Prepscius 996151a56a flatten 20260225
2026-02-25 12:43:06 -05:00

38 lines
931 B
C++
Executable File

// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#if defined(TJP_CORE_HEADER_ONLY)
#pragma once
#endif
#include "../header_only/compile.h"
#include "replace.h"
#include <string>
namespace tjp::core {
TJP_CORE_HEADER_ONLY_INLINE
bool replace(String& str, const StringView& from, const StringView& to)
{
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
TJP_CORE_HEADER_ONLY_INLINE
void replaceAll(String& str, const StringView& from, const StringView& to)
{
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
} // namespace