From d8114e6cfbf7543b39983918343c97415959d868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Wed, 18 Sep 2013 19:16:40 +0000 Subject: [PATCH] String : Added ::replace(char, char, bool), ::ToLower(), ::ToUpper() Added utils.h : GetRandom(Array) : gets a random element in a template list with weight logic. T FindValue(char*) : Find the value of a safe enum from the given name (when GetName is implemented) swap(T&, T&) is now in here too. --- src/Makefile.am | 2 +- src/core.h | 1 + src/lol/base/string.h | 34 +++++++++++++++ src/lolcore.vcxproj | 1 + src/lolcore.vcxproj.filters | 3 ++ src/numeric.h | 7 ---- src/utils.h | 82 +++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/utils.h diff --git a/src/Makefile.am b/src/Makefile.am index 273fff46..c6fd0411 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ liblolcore_a_SOURCES = \ map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ - profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h numeric.h \ + profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h numeric.h utils.h \ worldentity.cpp worldentity.h gradient.cpp gradient.h gradient.lolfx \ platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \ light.cpp light.h \ diff --git a/src/core.h b/src/core.h index f30ce096..f4b73e65 100644 --- a/src/core.h +++ b/src/core.h @@ -117,6 +117,7 @@ static inline int isnan(float f) #include #include +#include "utils.h" #include "numeric.h" // Static classes diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 93eca752..395daca5 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -147,6 +147,40 @@ public: return tmp ? (int)(intptr_t)(tmp - C()) : -1; } + int Replace(const char old_token, const char new_token, bool all_occurence=false) + { + using namespace std; + + int res = 0; + char *tmp = NULL; + while ((tmp = strrchr(C(), old_token))) + { + *tmp = new_token; + res++; + if (!all_occurence) + break; + } + return res; + } + + inline String& ToLower() + { + char* p = C(); + for (int i = 0; i < Count(); ++i) + if ('A' <= p[i] && p[i] <= 'Z') + p[i] += 'a' - 'A'; + return *this; + } + + inline String& ToUpper() + { + char* p = C(); + for (int i = 0; i < Count(); ++i) + if ('a' <= p[i] && p[i] <= 'z') + p[i] += 'A' - 'a'; + return *this; + } + int LastIndexOf(char const* token) const { using namespace std; diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index ba38670c..890907fc 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -274,6 +274,7 @@ + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index e8cbe605..e40bd989 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -608,6 +608,9 @@ input + + ... + diff --git a/src/numeric.h b/src/numeric.h index 3379be56..f0cd873c 100644 --- a/src/numeric.h +++ b/src/numeric.h @@ -43,13 +43,6 @@ template static inline T1 damp(const T1 return lol::lerp(a, b, dt / (dt + x)); } -template< class T > inline void Swap( T& A, T& B ) -{ - const T Temp = A; - A = B; - B = Temp; -} - //SmoothClamp clamps x in [a - sd, b + sd] and returns a value in [a, b] that is smoothed at the borders. static inline float SmoothClamp(float &x, float a, float b, float sd) { diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 00000000..6aa4570c --- /dev/null +++ b/src/utils.h @@ -0,0 +1,82 @@ +// +// Lol Engine +// +// Copyright: (c) 2012-2013 Benjamin "Touky" Huet +// (c) 2012-2013 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +// +// Various basic convenience functions +// ------------------ +// + +#if !defined __LOL_UTILS_H__ +#define __LOL_UTILS_H__ + +#include "lol\base\string.h" + +namespace lol +{ + +//Swaps two given values. +template< class T > inline void Swap( T& A, T& B ) +{ + const T Temp = A; + A = B; + B = Temp; +} + +//TODO: random struct +//Gets a random Element from the given array, considering you have implemented what follows : +//NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored. +template< class T > inline int GetRandom(Array src) +{ + float r_total = 0.f; + float r_alpha = rand(1.f); + float r_value = 0.f; + int r_j = 0; + + int i = 0; + for (; i < src.Count(); ++i) + { + T& tmp = src[i]; + if (tmp.m_weight > .0f) + { + r_total += tmp.m_weight; + float r_tmp = r_alpha * r_total; + while (r_tmp > r_value + src[r_j].m_weight && r_j < i) + { + r_value += src[r_j].m_weight; + r_j++; + } + } + } + + return (r_total > .0f)?(r_j):(-1); +} + +//Gets the value for the given safe-enum, considering you have implemented what follows : +//NEEDS : Contructor(Value) +//NEEDS : static char const *GetName(Value v) +//NEEDS : T::Max +template< class T > inline T FindValue(const char* name) +{ + String n = name; + n.ToLower(); + for (int i = 0; i < T::Max; ++i) + { + String s = T::GetName(T::Value(i)); + if (s.ToLower().IndexOf(n.C()) > -1) + return T(T::Value(i)); + } + return T::Max; +} + +} /* namespace lol */ + +#endif /* __LOL_UTILS_H__ */ +