Skip to content

Commit

Permalink
Add replacements for boost::algorithm::replace_all[_copy].
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 17, 2017
1 parent a25a3fb commit ca3a589
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
42 changes: 42 additions & 0 deletions libs/string/replace.h
@@ -0,0 +1,42 @@
#pragma once

#include <string>

namespace string
{

/**
* Replaces all occurrences of the given search string in the subject
* with the given replacement, in-place.
*/
inline void replace_all(std::string& subject, const std::string& search, const std::string& replacement)
{
std::size_t pos = 0;

while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replacement);
pos += replacement.length();
}
}

/**
* Replaces all occurrences of of the given search string with
* the given replacement and returns a new string instance
* containing the result. The incoming subject is passed by value such
* that the original string is not altered.
*/
inline std::string replace_all_copy(std::string subject, const std::string& search, const std::string& replacement)
{
std::size_t pos = 0;

while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replacement);
pos += replacement.length();
}

return subject;
}

}
1 change: 0 additions & 1 deletion radiant/log/Console.cpp
Expand Up @@ -11,7 +11,6 @@
#include "StringLogDevice.h"

#include <functional>
#include <boost/algorithm/string/replace.hpp>

namespace ui {

Expand Down
6 changes: 3 additions & 3 deletions radiant/log/PIDFile.h
Expand Up @@ -5,7 +5,7 @@
#include "modulesystem/ModuleRegistry.h"

#include "os/file.h"
#include <boost/algorithm/string/replace.hpp>
#include "string/replace.h"

#define PID_FILENAME "darkradiant.pid"

Expand Down Expand Up @@ -39,8 +39,8 @@ class PIDFile
fs::path path = registry.getApplicationContext().getSettingsPath();
path /= "darkradiant.log";
std::string logPath = path.string();
boost::algorithm::replace_all(logPath, "\\\\", "\\");
boost::algorithm::replace_all(logPath, "//", "/");
string::replace_all(logPath, "\\\\", "\\");
string::replace_all(logPath, "//", "/");

std::string msg("Radiant failed to start properly the last time it was run.\n");
msg += "If this is happening again, you might want to check the log file in\n";
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -200,6 +200,7 @@
<ClInclude Include="..\..\libs\stream\TextFileInputStream.h" />
<ClInclude Include="..\..\libs\stream\utils.h" />
<ClInclude Include="..\..\libs\string\convert.h" />
<ClInclude Include="..\..\libs\string\replace.h" />
<ClInclude Include="..\..\libs\string\string.h" />
<ClInclude Include="..\..\libs\SurfaceShader.h" />
<ClInclude Include="..\..\libs\texturelib.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -160,6 +160,9 @@
<ClInclude Include="..\..\libs\util\Noncopyable.h">
<Filter>util</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\string\replace.h">
<Filter>string</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit ca3a589

Please sign in to comment.