made lexical_cast<int> and lexical_cast_default<int> much faster
by adding template specializations for the most used cases.
This commit is contained in:
parent
1dacb3ff94
commit
b5270e41b9
4 changed files with 83 additions and 39 deletions
|
@ -100,6 +100,7 @@ wesnoth_SOURCES = \
|
|||
unit.cpp \
|
||||
unit_display.cpp \
|
||||
unit_types.cpp \
|
||||
util.cpp \
|
||||
variable.cpp \
|
||||
video.cpp \
|
||||
wassert.cpp \
|
||||
|
@ -290,6 +291,7 @@ wesnoth_editor_SOURCES = \
|
|||
unit.cpp \
|
||||
unit_display.cpp \
|
||||
unit_types.cpp \
|
||||
util.cpp \
|
||||
variable.cpp \
|
||||
video.cpp \
|
||||
serialization/binary_or_text.cpp \
|
||||
|
@ -375,6 +377,7 @@ wesnoth_editor_SOURCES = \
|
|||
unit.hpp \
|
||||
unit_display.hpp \
|
||||
unit_types.hpp \
|
||||
util.hpp \
|
||||
variable.hpp \
|
||||
video.hpp \
|
||||
serialization/binary_or_text.hpp \
|
||||
|
|
|
@ -40,45 +40,6 @@ namespace {
|
|||
|
||||
std::vector<language_def> known_languages;
|
||||
|
||||
#if 0
|
||||
language_def known_languages[] = {
|
||||
language_def("", N_("System default language")),
|
||||
language_def("af_ZA", "Afrikaans"),
|
||||
language_def("bg_BG", "Български (Bulgarski)"),
|
||||
language_def("ca_ES", "Català"),
|
||||
language_def("cs_CZ", "Čeština"),
|
||||
language_def("da_DK", "Dansk"),
|
||||
language_def("de_DE", "Deutsch"),
|
||||
language_def("el_GR", "Ελληνικά (Ellinika)"),
|
||||
language_def("et_EE", "Eesti"),
|
||||
language_def("en_GB", "English [GB]"),
|
||||
language_def("C", "English [US]"),
|
||||
language_def("es_ES", "Español"),
|
||||
language_def("eu_ES", "Euskera"),
|
||||
language_def("fr_FR", "Français"),
|
||||
language_def("it_IT", "Italiano"),
|
||||
language_def("la_IT", "Latina"),
|
||||
language_def("hu_HU", "Magyar"),
|
||||
language_def("nl_NL", "Nederlands"),
|
||||
language_def("ja_JP", "日本語 (Nihongo)"),
|
||||
language_def("no_NO", "Norsk"),
|
||||
language_def("oc_FR", "Occitan"),
|
||||
language_def("pl_PL", "Polski"),
|
||||
language_def("pt_BR", "Português do Brasil"),
|
||||
language_def("ru_RU", "Русский (Russkij)"),
|
||||
language_def("sk_SK", "Slovenčina"),
|
||||
language_def("sl_SI", "Slovenščina"),
|
||||
language_def("sr_CS", "Srpski"),
|
||||
language_def("tr_TR", "Türkçe"),
|
||||
language_def("fi_FI", "Suomi"),
|
||||
language_def("sv_SE", "Svenska"),
|
||||
language_def("zh_CN", "中文 (Zhongwen)"),
|
||||
|
||||
// end of list marker, do not remove
|
||||
language_def("", "")
|
||||
};
|
||||
#endif
|
||||
|
||||
std::string languagedef_name (const language_def& def)
|
||||
{
|
||||
return def.language;
|
||||
|
|
68
src/util.cpp
Normal file
68
src/util.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/* $Id$ */
|
||||
/*
|
||||
Copyright (C) 2005 by Philippe Plantier <ayin@anathas.org>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "util.hpp"
|
||||
#include <cstdlib>
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a.c_str(), &endptr, 10);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a, &endptr, 10);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const std::string&>(const std::string& a, int def)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a.c_str(), &endptr, 10);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
return def;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const char*>(const char* a, int def)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a, &endptr, 10);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
return def;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
12
src/util.hpp
12
src/util.hpp
|
@ -73,6 +73,18 @@ To lexical_cast_default(From a, To def=To())
|
|||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const std::string&>(const std::string& a, int def);
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const char*>(const char* a, int def);
|
||||
|
||||
template<typename From>
|
||||
std::string str_cast(From a)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue