Skip to content

Commit

Permalink
Add string trim function replacements.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 18, 2017
1 parent 4a92cc1 commit d4a41cc
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
128 changes: 128 additions & 0 deletions libs/string/trim.h
@@ -0,0 +1,128 @@
#pragma once

#include <string>

namespace string
{

/**
* Removes all characters from the beginning of the string matching the given predicate,
* modifying the given string in-place.
*/
template<typename Predicate>
inline void trim_left_if(std::string& subject, Predicate predicate)
{
// Erase everything from the beginning up to the first character
// that is not matching the predicate (e.g. is not a space)
subject.erase(subject.begin(),
std::find_if(subject.begin(), subject.end(), [&](int ch) { return !predicate(ch); }));
}

/**
* Removes all characters from the end of the string matching the given predicate,
* modifying the given string in-place.
*/
template<typename Predicate>
inline void trim_right_if(std::string& subject, Predicate predicate)
{
// Erase everything from the end up to the nearest-to-last character
// that is not matching the predicate (e.g. is not a space)
subject.erase(
std::find_if(subject.rbegin(), subject.rend(), [&](int ch) { return !predicate(ch); }).base(),
subject.end());
}

/**
* Removes all characters from the beginning and the end of the string
* matching the given predicate, returning a new string containing of the result.
*/
template<typename Predicate>
inline std::string trim_copy_if(std::string subject, Predicate predicate)
{
trim_left_if(subject, predicate);
trim_right_if(subject, predicate);

return subject;
}

/**
* Removes all space characters from the beginning and the end of the string, in-place.
*/
inline void trim(std::string& subject)
{
trim_left_if(subject, ::isspace);
trim_right_if(subject, ::isspace);
}

/**
* Removes all space characters from the beginning and the end of the string
* and returns a new string containing of the result.
*/
inline std::string trim_copy(std::string subject)
{
trim_left_if(subject, ::isspace);
trim_right_if(subject, ::isspace);

return subject;
}

/**
* Removes all of the given characters from the beginning and the end of the subject, in-place.
*/
inline void trim(std::string& subject, const std::string& charsToBeRemoved)
{
trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the beginning and the end of the subject,
* returning a new string containing the result.
*/
inline std::string trim_copy(std::string subject, const std::string& charsToBeRemoved)
{
trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

return subject;
}

/**
* Removes all of the given characters from the beginning of the subject, in-place
*/
inline void trim_left(std::string& subject, const std::string& charsToBeRemoved)
{
trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the beginning of the subject,
* returning a new string containing the result.
*/
inline std::string trim_left_copy(std::string subject, const std::string& charsToBeRemoved)
{
trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

return subject;
}

/**
* Removes all of the given characters from the end of the subject, in-place
*/
inline void trim_right(std::string& subject, const std::string& charsToBeRemoved)
{
trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the end of the subject,
* returning a new string containing the result.
*/
inline std::string trim_right_copy(std::string subject, const std::string& charsToBeRemoved)
{
trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

return subject;
}

}
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -204,6 +204,7 @@
<ClInclude Include="..\..\libs\string\replace.h" />
<ClInclude Include="..\..\libs\string\split.h" />
<ClInclude Include="..\..\libs\string\string.h" />
<ClInclude Include="..\..\libs\string\trim.h" />
<ClInclude Include="..\..\libs\SurfaceShader.h" />
<ClInclude Include="..\..\libs\texturelib.h" />
<ClInclude Include="..\..\libs\ThreadedDefLoader.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -169,6 +169,9 @@
<ClInclude Include="..\..\libs\string\split.h">
<Filter>string</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\string\trim.h">
<Filter>string</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit d4a41cc

Please sign in to comment.