ocarina/libsaria/format.cpp
Bryan Schumaker 664ad0dad2 libsaria: Cache words generated from strings
I now look up a list of words from a map (if it exists), rather than
having to iterate over the same string potentially many times.
2011-12-27 18:48:57 -05:00

76 lines
1.4 KiB
C++

#include <libsaria/format.h>
#include <map>
#include <list>
using namespace std;
static map<string, list<string> > format_cache;
list<string> *do_format(string &text)
{
string word;
list<string> word_list;
pair< map<string, list<string> >::iterator, bool > ret;
char c, diff = 'a' - 'A';
for (unsigned int i = 0; i < text.size(); i++) {
c = text[i];
// Character already lower case
if ( (c >= 'a') && (c <= 'z') )
word += c;
// Convert uppercase to lowercase
else if ( (c >= 'A') && (c <= 'Z') )
word += (c + diff);
// Keep numbers
else if ( (c >= '0') && (c <= '9') )
word += c;
else {
// These characters indicate a new word
switch (c) {
case '-':
case '\\':
case '/':
case ',':
case ';':
case '(':
case ')':
case '_':
case '~':
case '+':
case '"':
case ' ':
if (word != "")
word_list.push_back(word);
word = "";
break;
default:
break;
};
}
}
if (word != "")
word_list.push_back(word);
ret = format_cache.insert( pair<string, list<string> >(text, word_list) );
return &(ret.first->second);
}
namespace libsaria
{
list<string> *format_text(string &text)
{
map<string, list<string> >::iterator it;
it = format_cache.find(text);
/* Not found in cache... */
if (it == format_cache.end())
return do_format(text);
else
return &(it->second);
}
} /* Namespace: libsaria */