diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 765339d4..93eca752 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -158,6 +158,42 @@ public: return -1; } + bool StartsWith(char const* token) const + { + const char* p = C(); + while (*token != '\0') + { + if (*p != *token) + return false; + + ++p; + ++token; + } + + return true; + } + + bool EndsWith(char const* token) const + { + const char* p = C(); + int token_idx = strlen(token) - 1; + int c_idx = strlen(p) - 1; + + if (c_idx < token_idx) + return false; + + while (token_idx >= 0) + { + if (token[token_idx] != p[c_idx]) + return false; + + --token_idx; + --c_idx; + } + + return true; + } + inline String operator +(String const &s) const { String ret(*this); diff --git a/src/lol/math/functions.h b/src/lol/math/functions.h index e10ab6bd..ace1f180 100644 --- a/src/lol/math/functions.h +++ b/src/lol/math/functions.h @@ -171,7 +171,9 @@ static inline ldouble ceil(ldouble x) { return std::ceil(x); } static inline T fract(T x) { return x - lol::floor(x); } \ static inline T min(T x, T y) { return std::min(x, y); } \ static inline T max(T x, T y) { return std::max(x, y); } \ - static inline T clamp(T x, T y, T z) { return min(max(x, y), z); } + static inline T clamp(T x, T y, T z) { return min(max(x, y), z); } \ + static inline T sign(T x) { return (T)(((T)0 < x) - (x < (T)0)); } + LOL_GENERIC_FUNC(uint8_t) LOL_GENERIC_FUNC(int8_t) LOL_GENERIC_FUNC(uint16_t) diff --git a/test/unit/string.cpp b/test/unit/string.cpp index 496514b9..059893ad 100644 --- a/test/unit/string.cpp +++ b/test/unit/string.cpp @@ -218,7 +218,19 @@ LOLUNIT_FIXTURE(StringTest) LOLUNIT_ASSERT(i7 == 0); LOLUNIT_ASSERT(i8 == -1); LOLUNIT_ASSERT(i9 == 9); - }}; + } + + LOLUNIT_TEST(StartsEndsWith) + { + String s = "lolilol"; + LOLUNIT_ASSERT(s.StartsWith("loli")); + LOLUNIT_ASSERT(!s.StartsWith("lolo")); + LOLUNIT_ASSERT(!s.StartsWith("lolilolilol")); + LOLUNIT_ASSERT(s.EndsWith("ilol")); + LOLUNIT_ASSERT(!s.EndsWith("olol")); + LOLUNIT_ASSERT(!s.EndsWith("lolilolilol")); + } +}; } /* namespace lol */