| @@ -158,6 +158,42 @@ public: | |||||
| return -1; | 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 | inline String operator +(String const &s) const | ||||
| { | { | ||||
| String ret(*this); | String ret(*this); | ||||
| @@ -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 fract(T x) { return x - lol::floor(x); } \ | ||||
| static inline T min(T x, T y) { return std::min(x, y); } \ | 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 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(uint8_t) | ||||
| LOL_GENERIC_FUNC(int8_t) | LOL_GENERIC_FUNC(int8_t) | ||||
| LOL_GENERIC_FUNC(uint16_t) | LOL_GENERIC_FUNC(uint16_t) | ||||
| @@ -218,7 +218,19 @@ LOLUNIT_FIXTURE(StringTest) | |||||
| LOLUNIT_ASSERT(i7 == 0); | LOLUNIT_ASSERT(i7 == 0); | ||||
| LOLUNIT_ASSERT(i8 == -1); | LOLUNIT_ASSERT(i8 == -1); | ||||
| LOLUNIT_ASSERT(i9 == 9); | 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 */ | } /* namespace lol */ | ||||