NBEngine/engine/NBCore/include/NBCore/StringUtils.hpp
2026-08-20 08:08:20 -05:00

60 lines
1.4 KiB
C++

#pragma once
#ifndef _NB_STRING_UTILS
#define _NB_STRING_UTILS
#include <string>
#include <string_view>
#include <NBCore/TypeTraits.hpp>
namespace nb {
#ifdef _NB_TARGET_WINDOWS
inline static const std::string NEWLINE = "\n";
inline static const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX
inline const std::string NEWLINE = "\n";
inline const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_LINUX
const std::string TABOVER = " ";
template <typename T = char>
std::basic_string<T> find_and_replace(
ExplicitType_t<std::basic_string<T>> original,
ExplicitType_t<std::basic_string_view<T>> find,
ExplicitType_t<std::basic_string_view<T>> replace
) {
using StringType = std::basic_string<T>;
StringType ret(original);
std::size_t find_len = find.length();
std::size_t replace_len = replace.length();
std::size_t currpos = 0;
while(true) {
currpos = ret.find(find, currpos);
if (currpos == StringType::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace);
currpos += replace_len;
}
return ret;
}
std::string indent_strblock(
std::string block,
std::string prepend,
std::string topIndent
);
std::string indent_strblock(
std::string block,
std::string prepend
);
} // namespace nb
#endif // _NB_STRING_UTILS