Skip to content

Instantly share code, notes, and snippets.

@royvandam
Last active January 24, 2020 14:44
Show Gist options
  • Save royvandam/930300561dba941ea67541faa9ebbf68 to your computer and use it in GitHub Desktop.
Save royvandam/930300561dba941ea67541faa9ebbf68 to your computer and use it in GitHub Desktop.
C++ String tools
/*
Copyright (c) 2018, 2019 Roy van Dam <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <string.hpp>
#include <algorithm>
#include <sstream>
namespace String {
void Replace(std::string& str, char needle, char replacement) {
std::replace(str.begin(), str.end(), needle, replacement);
}
void
Split(const std::string& str, std::vector<std::string>& tokens) {
std::stringstream ss(str);
while (ss.good()) {
std::string token;
ss >> token;
tokens.push_back(token);
}
}
void
Split(const std::string& str, std::vector<std::string>& tokens, const std::string& delim) {
std::string::size_type pos;
std::string::size_type lastPos = 0;
std::string::size_type length = str.length();
while (lastPos < length + 1) {
pos = str.find_first_of(delim, lastPos);
if (pos == std::string::npos) {
pos = length;
}
if (pos != lastPos) {
tokens.push_back(std::string(str.data() + lastPos, pos - lastPos));
}
lastPos = pos + 1;
}
}
bool
Contains(const std::string& str, const std::string& needle) {
return str.find(needle) != std::string::npos;
}
namespace Path {
std::string
Join(std::string const& lhs, std::string const& rhs) {
return std::move(lhs + "/" + rhs);
}
std::string
Basename(std::string const& path, std::string const& delims) {
return path.substr(path.find_last_of(delims) + 1);
}
}
} // namespace String
/*
Copyright (c) 2018, 2019 Roy van Dam <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <string>
#include <vector>
namespace String {
void Replace(std::string& str, char needle, char replacement);
void Split(const std::string& str, std::vector<std::string>& tokens);
void Split(const std::string& str, std::vector<std::string>& tokens, const std::string& delim);
bool Contains(const std::string& str, const std::string& needle);
namespace Path {
std::string Join(std::string const& lhs, std::string const& rhs);
std::string Basename(std::string const& path, std::string const& delims = "/\\");
}
} // namespace String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment