Added split_token to string utils

This commit is contained in:
Thonsew 2011-09-08 19:15:53 +00:00
parent 87b5cd1ec4
commit 87c99119d3
2 changed files with 25 additions and 1 deletions

View File

@ -25,7 +25,8 @@
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
#include "../util.hpp"
#include "utils/lru_cache.hpp"
#include <boost/array.hpp>
static lg::log_domain log_engine("engine");
@ -75,6 +76,26 @@ std::string &strip(std::string &str)
return str;
}
namespace {
static const uint CACHE_SIZE = 10000;
typedef std::pair<n_token::t_token, std::pair<char, int > > t_triad;
typedef std::vector<n_token::t_token > t_out;
struct t_calc_cache_item {
t_out const operator()(t_triad const & x){
std::vector<std::string> vstr(split(static_cast<std::string const &>(x.first), x.second.first, x.second.second));
t_out const retval(vstr.begin(), vstr.end());
return retval;
}
};
typedef n_lru_cache::t_lru_cache<t_triad, t_out, t_calc_cache_item> t_cache;
}
std::vector< n_token::t_token > split_token(n_token::t_token const &val, char c, int flags){
static t_cache my_cache(t_calc_cache_item(), CACHE_SIZE);
return my_cache.check(std::make_pair(val, std::make_pair(c,flags)));
}
std::vector< std::string > split(std::string const &val, char c, int flags)
{

View File

@ -25,6 +25,7 @@
#include <boost/next_prior.hpp>
#include "SDL_types.h"
#include "token.hpp"
/** The type we use to represent Unicode strings. */
typedef std::vector<wchar_t> wide_string;
@ -52,6 +53,7 @@ enum { REMOVE_EMPTY = 0x01, /**< REMOVE_EMPTY : remove empty elements. */
STRIP_SPACES = 0x02 /**< STRIP_SPACES : strips leading and trailing blank spaces. */
};
std::vector< n_token::t_token > split_token(n_token::t_token const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< std::string > split(std::string const &val, char c = ',', int flags = REMOVE_EMPTY | STRIP_SPACES);
/**
@ -192,6 +194,7 @@ bool isvalid_username(const std::string &login);
bool isvalid_wildcard(const std::string &login);
typedef std::map< std::string, t_string > string_map;
typedef boost::unordered_map< n_token::t_token, t_string > token_map;
/**
* Functions for converting Unicode wide-char strings to UTF-8 encoded strings,