summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2016-08-23 19:37:03 +0200
committerAttila Molnar <attilamolnar@hush.com>2016-08-23 19:37:03 +0200
commitbcdc3b0bb0ba957a9e99cd6db7f1242a9e587400 (patch)
treefe0c0962384f5f8aa83658fd5897aa13e29ce22e /include
parent2df0de3c9212f7ea74fcb9a0ccf20990e47e9f2e (diff)
parent0aa6c4134011006e6166c72d9c3162b054c01bbe (diff)
Merge branch 'master+ircstring'
Diffstat (limited to 'include')
-rw-r--r--include/command_parse.h2
-rw-r--r--include/hashcomp.h160
-rw-r--r--include/stdalgo.h67
-rw-r--r--include/typedefs.h2
4 files changed, 88 insertions, 143 deletions
diff --git a/include/command_parse.h b/include/command_parse.h
index 0f39d3586..c3d67af23 100644
--- a/include/command_parse.h
+++ b/include/command_parse.h
@@ -85,7 +85,7 @@ class CoreExport CommandParser
* With one list it is much simpler, and is used in NAMES, WHOIS, PRIVMSG etc.
*
* If there is only one list and there are duplicates in it, then the command handler is only called for
- * unique items. Entries are compared using "irc comparision" (see irc::string).
+ * unique items. Entries are compared using "irc comparison".
* If the usemax parameter is true (the default) the function only parses until it reaches
* ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
*
diff --git a/include/hashcomp.h b/include/hashcomp.h
index 87b2636fc..f3b1ba6e9 100644
--- a/include/hashcomp.h
+++ b/include/hashcomp.h
@@ -40,11 +40,9 @@
* treat [ identical to {, ] identical to }, and \
* as identical to |.
*
- * Our hashing functions are designed to accept
- * std::string and compare/hash them as type irc::string
- * by converting them internally. This makes them
- * backwards compatible with other code which is not
- * aware of irc::string.
+ * There are functors that accept std::string and
+ * compare/hash them as type irc::string by using
+ * mapping arrays internally.
*******************************************************/
/** Seperate from the other casemap tables so that code *can* still exclusively rely on RFC casemapping
@@ -70,43 +68,31 @@ CoreExport extern unsigned const char ascii_case_insensitive_map[256];
*/
CoreExport extern unsigned const char rfc_case_sensitive_map[256];
-template<typename T> const T& SearchAndReplace(T& text, const T& pattern, const T& replace)
-{
- T replacement;
- if ((!pattern.empty()) && (!text.empty()))
- {
- for (std::string::size_type n = 0; n != text.length(); ++n)
- {
- if (text.length() >= pattern.length() && text.substr(n, pattern.length()) == pattern)
- {
- // Found the pattern in the text, replace it, and advance
- replacement.append(replace);
- n = n + pattern.length() - 1;
- }
- else
- {
- replacement += text[n];
- }
- }
- }
- text = replacement;
- return text;
-}
-
/** The irc namespace contains a number of helper classes.
*/
namespace irc
{
+ /** Check if two IRC object (e.g. nick or channel) names are equal.
+ * This function uses national_case_insensitive_map to determine equality, which, by default does comparison
+ * according to RFC 1459, treating certain otherwise non-identical characters as identical.
+ * @param s1 First string to compare
+ * @param s2 Second string to compare
+ * @return True if the two names are equal, false otherwise
+ */
+ CoreExport bool equals(const std::string& s1, const std::string& s2);
/** This class returns true if two strings match.
* Case sensitivity is ignored, and the RFC 'character set'
* is adhered to
*/
- struct CoreExport StrHashComp
+ struct StrHashComp
{
/** The operator () does the actual comparison in hash_map
*/
- bool operator()(const std::string& s1, const std::string& s2) const;
+ bool operator()(const std::string& s1, const std::string& s2) const
+ {
+ return equals(s1, s2);
+ }
};
struct insensitive
@@ -170,14 +156,15 @@ namespace irc
/** Joins the contents of a vector to a string.
* @param sequence Zero or more items to join.
- * @separator The character to place between the items.
+ * @param separator The character to place between the items, defaults to ' ' (space).
+ * @return Joined string.
*/
std::string CoreExport stringjoiner(const std::vector<std::string>& sequence, char separator = ' ');
/** irc::sepstream allows for splitting token seperated lists.
* Each successive call to sepstream::GetToken() returns
* the next token, until none remain, at which point the method returns
- * an empty string.
+ * false.
*/
class CoreExport sepstream
{
@@ -265,12 +252,6 @@ namespace irc
*/
bool GetToken(std::string &token);
- /** Fetch the next token from the stream as an irc::string
- * @param token The next token available, or an empty string if none remain
- * @return True if tokens are left to be read, false if the last token was just retrieved.
- */
- bool GetToken(irc::string &token);
-
/** Fetch the next token from the stream as an integer
* @param token The next token available, or undefined if none remain
* @return True if tokens are left to be read, false if the last token was just retrieved.
@@ -337,107 +318,4 @@ namespace irc
*/
long GetToken();
};
-
- struct hash
- {
- /** Hash an irc::string using RFC1459 case sensitivity rules
- * @param s A string to hash
- * @return The hash value
- */
- size_t CoreExport operator()(const irc::string &s) const;
- };
-}
-
-/* Define operators for using >> and << with irc::string to an ostream on an istream. */
-/* This was endless fun. No. Really. */
-/* It was also the first core change Ommeh made, if anyone cares */
-
-/** Operator << for irc::string
- */
-inline std::ostream& operator<<(std::ostream &os, const irc::string &str) { return os << str.c_str(); }
-
-/** Operator >> for irc::string
- */
-inline std::istream& operator>>(std::istream &is, irc::string &str)
-{
- std::string tmp;
- is >> tmp;
- str = tmp.c_str();
- return is;
-}
-
-/* Define operators for + and == with irc::string to std::string for easy assignment
- * and comparison
- *
- * Operator +
- */
-inline std::string operator+ (std::string& leftval, irc::string& rightval)
-{
- return leftval + std::string(rightval.c_str());
-}
-
-/* Define operators for + and == with irc::string to std::string for easy assignment
- * and comparison
- *
- * Operator +
- */
-inline irc::string operator+ (irc::string& leftval, std::string& rightval)
-{
- return leftval + irc::string(rightval.c_str());
-}
-
-/* Define operators for + and == with irc::string to std::string for easy assignment
- * and comparison
- *
- * Operator ==
- */
-inline bool operator== (const std::string& leftval, const irc::string& rightval)
-{
- return (leftval.c_str() == rightval);
-}
-
-/* Define operators for + and == with irc::string to std::string for easy assignment
- * and comparison
- *
- * Operator ==
- */
-inline bool operator== (const irc::string& leftval, const std::string& rightval)
-{
- return (leftval == rightval.c_str());
-}
-
-/* Define operators != for irc::string to std::string for easy comparison
- */
-inline bool operator!= (const irc::string& leftval, const std::string& rightval)
-{
- return !(leftval == rightval.c_str());
-}
-
-/* Define operators != for std::string to irc::string for easy comparison
- */
-inline bool operator!= (const std::string& leftval, const irc::string& rightval)
-{
- return !(leftval.c_str() == rightval);
-}
-
-/** Assign an irc::string to a std::string.
- */
-inline std::string assign(const irc::string &other) { return other.c_str(); }
-
-/** Assign a std::string to an irc::string.
- */
-inline irc::string assign(const std::string &other) { return other.c_str(); }
-
-/** Trim the leading and trailing spaces from a std::string.
- */
-inline std::string& trim(std::string &str)
-{
- std::string::size_type start = str.find_first_not_of(" ");
- std::string::size_type end = str.find_last_not_of(" ");
- if (start == std::string::npos || end == std::string::npos)
- str = "";
- else
- str = str.substr(start, end-start+1);
-
- return str;
}
diff --git a/include/stdalgo.h b/include/stdalgo.h
index 3e00a4cdc..f4465963a 100644
--- a/include/stdalgo.h
+++ b/include/stdalgo.h
@@ -64,6 +64,73 @@ namespace stdalgo
}
}
+ namespace string
+ {
+ /** Get underlying C string of the string passed as parameter. Useful in template functions.
+ * @param str C string
+ * @return Same as input
+ */
+ inline const char* tocstr(const char* str)
+ {
+ return str;
+ }
+
+ /** Get underlying C string of the string passed as parameter. Useful in template functions.
+ * @param str std::string object
+ * @return str.c_str()
+ */
+ inline const char* tocstr(const std::string& str)
+ {
+ return str.c_str();
+ }
+
+ /** Check if two strings are equal case insensitively.
+ * @param str1 First string to compare.
+ * @param str2 Second string to compare.
+ * @return True if the strings are equal case-insensitively, false otherwise.
+ */
+ template <typename S1, typename S2>
+ inline bool equalsci(const S1& str1, const S2& str2)
+ {
+ return (!strcasecmp(tocstr(str1), tocstr(str2)));
+ }
+
+ /** Replace first occurrence of a substring ('target') in a string ('str') with another string ('replacement').
+ * @param str String to perform replacement in
+ * @param target String to replace
+ * @param replacement String to put in place of 'target'
+ * @return True if 'target' was replaced with 'replacement', false if it was not found in 'str'.
+ */
+ template<typename CharT, typename Traits, typename Alloc>
+ inline bool replace(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+ {
+ const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
+ if (p == std::basic_string<CharT, Traits, Alloc>::npos)
+ return false;
+ str.replace(p, target.size(), replacement);
+ return true;
+ }
+
+ /** Replace all occurrences of a string ('target') in a string ('str') with another string ('replacement').
+ * @param str String to perform replacement in
+ * @param target String to replace
+ * @param replacement String to put in place of 'target'
+ */
+ template<typename CharT, typename Traits, typename Alloc>
+ inline void replace_all(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+ {
+ if (target.empty())
+ return;
+
+ typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
+ while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
+ {
+ str.replace(p, target.size(), replacement);
+ p += replacement.size();
+ }
+ }
+ }
+
/**
* Deleter that uses operator delete to delete the item
*/
diff --git a/include/typedefs.h b/include/typedefs.h
index 48842ccf0..879ef0627 100644
--- a/include/typedefs.h
+++ b/include/typedefs.h
@@ -99,7 +99,7 @@ typedef std::map<std::string, XLineFactory*> XLineFactMap;
/** A map of XLines indexed by string
*/
-typedef std::map<irc::string, XLine *> XLineLookup;
+typedef std::map<std::string, XLine*, irc::insensitive_swo> XLineLookup;
/** A map of XLineLookup maps indexed by string
*/