Pārlūkot izejas kodu

core: added StartsWith and EndsWith function for strings, and a sign function for numeric types

undefined
Benlitz Sam Hocevar <sam@hocevar.net> pirms 11 gadiem
vecāks
revīzija
e50f75403a
3 mainītis faili ar 52 papildinājumiem un 2 dzēšanām
  1. +36
    -0
      src/lol/base/string.h
  2. +3
    -1
      src/lol/math/functions.h
  3. +13
    -1
      test/unit/string.cpp

+ 36
- 0
src/lol/base/string.h Parādīt failu

@@ -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);


+ 3
- 1
src/lol/math/functions.h Parādīt failu

@@ -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)


+ 13
- 1
test/unit/string.cpp Parādīt failu

@@ -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 */


Notiek ielāde…
Atcelt
Saglabāt