From 4d25722736c1fb277fa9f1c658cd57bbfb51eccf Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 12 Feb 2014 18:36:36 +0000 Subject: [PATCH] optimised conversions from string to numerical types --- latan/Global.cpp | 18 ++++++++++++++++++ latan/Global.hpp | 12 ++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/latan/Global.cpp b/latan/Global.cpp index 61475df..5830846 100644 --- a/latan/Global.cpp +++ b/latan/Global.cpp @@ -26,3 +26,21 @@ using namespace Latan; const string Env::fullName = PACKAGE_STRING; const string Env::name = PACKAGE_NAME; const string Env::version = PACKAGE_VERSION; + +template <> +float Latan::strTo(const string &str) +{ + return strtof(str.c_str(), (char **)NULL); +} + +template <> +double Latan::strTo(const string &str) +{ + return strtod(str.c_str(), (char **)NULL); +} + +template <> +int Latan::strTo(const string &str) +{ + return (int)(strtol(str.c_str(), (char **)NULL, 10)); +} diff --git a/latan/Global.hpp b/latan/Global.hpp index 6f17898..44d1b13 100644 --- a/latan/Global.hpp +++ b/latan/Global.hpp @@ -49,7 +49,7 @@ namespace Env // string conversions template -T strTo(std::string str) +T strTo(const std::string &str) { T buf; std::istringstream stream(str); @@ -59,8 +59,16 @@ T strTo(std::string str) return buf; } +//// optimized specializations +template <> +float strTo(const std::string &str); +template <> +double strTo(const std::string &str); +template <> +int strTo(const std::string &str); + template -std::string strFrom(T x) +std::string strFrom(const T x) { std::ostringstream stream;