diff options
author | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2007-10-15 22:06:10 +0000 |
---|---|---|
committer | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2007-10-15 22:06:10 +0000 |
commit | 8aa4df885e0d2f36ce737cf48aa3e2e7d7325107 (patch) | |
tree | 0fb90784c8777496e26ae48e6edd156fc6ae939c /src | |
parent | bc804f5eeb9fc0d8b14430c9c6de9621165a22a4 (diff) |
Move InspIRCd::Duration from command_parse to helperfuncs
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8211 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r-- | src/helperfuncs.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index a90dd0105..63f599f75 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -548,3 +548,48 @@ void InspIRCd::SendWhoisLine(User* user, User* dest, int numeric, const char* fo this->SendWhoisLine(user, dest, numeric, std::string(textbuffer)); } + +/** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array + * lookups and pointer maths. + */ +long InspIRCd::Duration(const std::string &str) +{ + unsigned char multiplier = 0; + long total = 0; + long times = 1; + long subtotal = 0; + + /* Iterate each item in the string, looking for number or multiplier */ + for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i) + { + /* Found a number, queue it onto the current number */ + if ((*i >= '0') && (*i <= '9')) + { + subtotal = subtotal + ((*i - '0') * times); + times = times * 10; + } + else + { + /* Found something thats not a number, find out how much + * it multiplies the built up number by, multiply the total + * and reset the built up number. + */ + if (subtotal) + total += subtotal * duration_multi[multiplier]; + + /* Next subtotal please */ + subtotal = 0; + multiplier = *i; + times = 1; + } + } + if (multiplier) + { + total += subtotal * duration_multi[multiplier]; + subtotal = 0; + } + /* Any trailing values built up are treated as raw seconds */ + return total + subtotal; +} + + |