mirror of https://gitlab.com/cppit/jucipp
21 changed files with 262 additions and 96 deletions
@ -1,6 +1,76 @@ |
|||||||
#include "utility.hpp" |
#include "utility.hpp" |
||||||
|
#include <cstring> |
||||||
|
|
||||||
ScopeGuard::~ScopeGuard() { |
ScopeGuard::~ScopeGuard() { |
||||||
if(on_exit) |
if(on_exit) |
||||||
on_exit(); |
on_exit(); |
||||||
} |
} |
||||||
|
|
||||||
|
bool starts_with(const char *str, const std::string &test) noexcept { |
||||||
|
for(size_t i = 0; i < test.size(); ++i) { |
||||||
|
if(*str == '\0') |
||||||
|
return false; |
||||||
|
if(*str != test[i]) |
||||||
|
return false; |
||||||
|
++str; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
bool starts_with(const char *str, const char *test) noexcept { |
||||||
|
for(; *test != '\0'; ++test) { |
||||||
|
if(*str == '\0') |
||||||
|
return false; |
||||||
|
if(*str != *test) |
||||||
|
return false; |
||||||
|
++str; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
bool starts_with(const std::string &str, const std::string &test) noexcept { |
||||||
|
return str.compare(0, test.size(), test) == 0; |
||||||
|
} |
||||||
|
|
||||||
|
bool starts_with(const std::string &str, const char *test) noexcept { |
||||||
|
for(size_t i = 0; i < str.size(); ++i) { |
||||||
|
if(*test == '\0') |
||||||
|
return true; |
||||||
|
if(str[i] != *test) |
||||||
|
return false; |
||||||
|
++test; |
||||||
|
} |
||||||
|
return *test == '\0'; |
||||||
|
} |
||||||
|
|
||||||
|
bool starts_with(const std::string &str, size_t pos, const std::string &test) noexcept { |
||||||
|
if(pos > str.size()) |
||||||
|
return false; |
||||||
|
return str.compare(pos, test.size(), test) == 0; |
||||||
|
} |
||||||
|
|
||||||
|
bool starts_with(const std::string &str, size_t pos, const char *test) noexcept { |
||||||
|
if(pos > str.size()) |
||||||
|
return false; |
||||||
|
for(size_t i = pos; i < str.size(); ++i) { |
||||||
|
if(*test == '\0') |
||||||
|
return true; |
||||||
|
if(str[i] != *test) |
||||||
|
return false; |
||||||
|
++test; |
||||||
|
} |
||||||
|
return *test == '\0'; |
||||||
|
} |
||||||
|
|
||||||
|
bool ends_with(const std::string &str, const std::string &test) noexcept { |
||||||
|
if(test.size() > str.size()) |
||||||
|
return false; |
||||||
|
return str.compare(str.size() - test.size(), test.size(), test) == 0; |
||||||
|
} |
||||||
|
|
||||||
|
bool ends_with(const std::string &str, const char *test) noexcept { |
||||||
|
auto test_size = strlen(test); |
||||||
|
if(test_size > str.size()) |
||||||
|
return false; |
||||||
|
return str.compare(str.size() - test_size, test_size, test) == 0; |
||||||
|
} |
||||||
|
|||||||
@ -1,8 +1,19 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include <functional> |
#include <functional> |
||||||
|
#include <string> |
||||||
|
|
||||||
class ScopeGuard { |
class ScopeGuard { |
||||||
public: |
public: |
||||||
std::function<void()> on_exit; |
std::function<void()> on_exit; |
||||||
~ScopeGuard(); |
~ScopeGuard(); |
||||||
}; |
}; |
||||||
|
|
||||||
|
bool starts_with(const char *str, const std::string &test) noexcept; |
||||||
|
bool starts_with(const char *str, const char *test) noexcept; |
||||||
|
bool starts_with(const std::string &str, const std::string &test) noexcept; |
||||||
|
bool starts_with(const std::string &str, const char *test) noexcept; |
||||||
|
bool starts_with(const std::string &str, size_t pos, const std::string &test) noexcept; |
||||||
|
bool starts_with(const std::string &str, size_t pos, const char *test) noexcept; |
||||||
|
|
||||||
|
bool ends_with(const std::string &str, const std::string &test) noexcept; |
||||||
|
bool ends_with(const std::string &str, const char *test) noexcept; |
||||||
|
|||||||
@ -0,0 +1,88 @@ |
|||||||
|
#include "utility.hpp" |
||||||
|
#include <glib.h> |
||||||
|
|
||||||
|
int main() { |
||||||
|
static bool scope_exit = false; |
||||||
|
{ |
||||||
|
ScopeGuard guard{[] { |
||||||
|
scope_exit = true; |
||||||
|
}}; |
||||||
|
g_assert(!scope_exit); |
||||||
|
} |
||||||
|
g_assert(scope_exit); |
||||||
|
|
||||||
|
std::string empty; |
||||||
|
std::string test("test"); |
||||||
|
std::string testtest("testtest"); |
||||||
|
|
||||||
|
g_assert(starts_with("", empty)); |
||||||
|
g_assert(starts_with("", "")); |
||||||
|
g_assert(starts_with(empty, "")); |
||||||
|
g_assert(starts_with(empty, empty)); |
||||||
|
g_assert(starts_with(empty, 0, "")); |
||||||
|
g_assert(starts_with(empty, 0, empty)); |
||||||
|
g_assert(ends_with(empty, "")); |
||||||
|
g_assert(ends_with(empty, empty)); |
||||||
|
|
||||||
|
g_assert(starts_with(test.c_str(), empty)); |
||||||
|
g_assert(starts_with(test.c_str(), "")); |
||||||
|
g_assert(starts_with(test, "")); |
||||||
|
g_assert(starts_with(test, empty)); |
||||||
|
g_assert(starts_with(test, 0, "")); |
||||||
|
g_assert(starts_with(test, 0, empty)); |
||||||
|
g_assert(ends_with(test, "")); |
||||||
|
g_assert(ends_with(test, empty)); |
||||||
|
|
||||||
|
g_assert(!starts_with(empty, 10, "")); |
||||||
|
g_assert(!starts_with(empty, 10, empty)); |
||||||
|
|
||||||
|
g_assert(!starts_with(test, 10, "")); |
||||||
|
g_assert(!starts_with(test, 10, empty)); |
||||||
|
|
||||||
|
g_assert(!starts_with(test, 10, test.c_str())); |
||||||
|
g_assert(!starts_with(test, 10, test)); |
||||||
|
|
||||||
|
g_assert(starts_with(test, 2, test.c_str() + 2)); |
||||||
|
g_assert(starts_with(test, 2, test.substr(2))); |
||||||
|
|
||||||
|
g_assert(ends_with(test, test.c_str() + 2)); |
||||||
|
g_assert(ends_with(test, test.substr(2))); |
||||||
|
|
||||||
|
g_assert(starts_with(test.c_str(), test)); |
||||||
|
g_assert(starts_with(test.c_str(), test.c_str())); |
||||||
|
g_assert(starts_with(test, test.c_str())); |
||||||
|
g_assert(starts_with(test, test)); |
||||||
|
g_assert(starts_with(test, 0, test.c_str())); |
||||||
|
g_assert(starts_with(test, 0, test)); |
||||||
|
g_assert(ends_with(test, test.c_str())); |
||||||
|
g_assert(ends_with(test, test)); |
||||||
|
|
||||||
|
g_assert(starts_with(testtest.c_str(), test)); |
||||||
|
g_assert(starts_with(testtest.c_str(), test.c_str())); |
||||||
|
g_assert(starts_with(testtest, test.c_str())); |
||||||
|
g_assert(starts_with(testtest, test)); |
||||||
|
g_assert(starts_with(testtest, 0, test.c_str())); |
||||||
|
g_assert(starts_with(testtest, 0, test)); |
||||||
|
g_assert(ends_with(testtest, test.c_str())); |
||||||
|
g_assert(ends_with(testtest, test)); |
||||||
|
g_assert(ends_with(testtest, "ttest")); |
||||||
|
g_assert(ends_with(testtest, std::string("ttest"))); |
||||||
|
|
||||||
|
g_assert(!starts_with(test.c_str(), testtest)); |
||||||
|
g_assert(!starts_with(test.c_str(), testtest.c_str())); |
||||||
|
g_assert(!starts_with(test, testtest.c_str())); |
||||||
|
g_assert(!starts_with(test, testtest)); |
||||||
|
g_assert(!starts_with(test, 0, testtest.c_str())); |
||||||
|
g_assert(!starts_with(test, 0, testtest)); |
||||||
|
g_assert(!ends_with(test, testtest.c_str())); |
||||||
|
g_assert(!ends_with(test, testtest)); |
||||||
|
|
||||||
|
g_assert(!starts_with(empty.c_str(), test)); |
||||||
|
g_assert(!starts_with(empty.c_str(), test.c_str())); |
||||||
|
g_assert(!starts_with(empty, test.c_str())); |
||||||
|
g_assert(!starts_with(empty, test)); |
||||||
|
g_assert(!starts_with(empty, 0, test.c_str())); |
||||||
|
g_assert(!starts_with(empty, 0, test)); |
||||||
|
g_assert(!ends_with(empty, test.c_str())); |
||||||
|
g_assert(!ends_with(empty, test)); |
||||||
|
} |
||||||
Loading…
Reference in new issue