aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-04-10 22:08:52 +0000
committerlloyd <[email protected]>2013-04-10 22:08:52 +0000
commit56e7e7187985f87037d032b49b6ff3699e7668f7 (patch)
tree241bd86799e2d89374c966c91db35c542a3b577b /src
parentbdb29dfb8acd66c803769238ca4b204839f5da46 (diff)
Add erase_chars and replace_chars
Diffstat (limited to 'src')
-rw-r--r--src/utils/parsing.cpp33
-rw-r--r--src/utils/parsing.h21
2 files changed, 47 insertions, 7 deletions
diff --git a/src/utils/parsing.cpp b/src/utils/parsing.cpp
index 486f5fdef..cf47e24f8 100644
--- a/src/utils/parsing.cpp
+++ b/src/utils/parsing.cpp
@@ -1,6 +1,6 @@
/*
-* Parser Functions
-* (C) 1999-2007 Jack Lloyd
+* Various string utils and parsing functions
+* (C) 1999-2007,2013 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -9,6 +9,7 @@
#include <botan/exceptn.h>
#include <botan/charset.h>
#include <botan/get_byte.h>
+#include <set>
namespace Botan {
@@ -263,9 +264,31 @@ std::string ipv4_to_string(u32bit ip)
return str;
}
-std::string replace_char(const std::string& str,
- char from_char,
- char to_char)
+std::string erase_chars(const std::string& str, const std::set<char>& chars)
+ {
+ std::string out;
+
+ for(auto c: str)
+ if(chars.count(c) == 0)
+ out += c;
+
+ return out;
+ }
+
+std::string replace_chars(const std::string& str,
+ const std::set<char>& chars,
+ char to_char)
+ {
+ std::string out = str;
+
+ for(size_t i = 0; i != out.size(); ++i)
+ if(chars.count(out[i]))
+ out[i] = to_char;
+
+ return out;
+ }
+
+std::string replace_char(const std::string& str, char from_char, char to_char)
{
std::string out = str;
diff --git a/src/utils/parsing.h b/src/utils/parsing.h
index bdf61a075..88c5a7bc0 100644
--- a/src/utils/parsing.h
+++ b/src/utils/parsing.h
@@ -1,6 +1,6 @@
/*
-* Parser Functions
-* (C) 1999-2007 Jack Lloyd
+* Various string utils and parsing functions
+* (C) 1999-2007,2013 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -11,6 +11,7 @@
#include <botan/types.h>
#include <string>
#include <vector>
+#include <set>
namespace Botan {
@@ -32,6 +33,11 @@ BOTAN_DLL std::vector<std::string> split_on(
const std::string& str, char delim);
/**
+* Erase characters from a string
+*/
+BOTAN_DLL std::string erase_chars(const std::string& str, const std::set<char>& chars);
+
+/**
* Replace a character in a string
* @param str the input string
* @param from_char the character to replace
@@ -43,6 +49,17 @@ BOTAN_DLL std::string replace_char(const std::string& str,
char to_char);
/**
+* Replace a character in a string
+* @param str the input string
+* @param from_chars the characters to replace
+* @param to_char the character to replace it with
+* @return str with all instances of from_chars replaced by to_char
+*/
+BOTAN_DLL std::string replace_chars(const std::string& str,
+ const std::set<char>& from_chars,
+ char to_char);
+
+/**
* Join a string
* @param strs strings to join
* @param delim the delimitor