mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
working with address impl
This commit is contained in:
@@ -10,4 +10,6 @@
|
||||
|
||||
namespace e2d { namespace address
|
||||
{
|
||||
str parent(str_view address);
|
||||
str nested(str_view address);
|
||||
}}
|
||||
|
||||
@@ -6,6 +6,34 @@
|
||||
|
||||
#include <enduro2d/high/address.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace e2d;
|
||||
|
||||
const str_view address_separator = ":/";
|
||||
|
||||
str_view::iterator str_view_search(str_view str, str_view s_str) noexcept {
|
||||
return std::search(str.begin(), str.end(), s_str.begin(), s_str.end());
|
||||
}
|
||||
}
|
||||
|
||||
namespace e2d { namespace address
|
||||
{
|
||||
str parent(str_view address) {
|
||||
const auto sep_e = str_view_search(address, address_separator);
|
||||
if ( sep_e == address.end() ) {
|
||||
return address;
|
||||
}
|
||||
const auto sep_d = std::distance(address.begin(), sep_e);
|
||||
return address.substr(0, static_cast<std::size_t>(sep_d));
|
||||
}
|
||||
|
||||
str nested(str_view address) {
|
||||
const auto sep_e = str_view_search(address, address_separator);
|
||||
if ( sep_e == address.end() ) {
|
||||
return str();
|
||||
}
|
||||
const auto sep_d = std::distance(address.begin(), sep_e);
|
||||
return address.substr(static_cast<std::size_t>(sep_d) + address_separator.size());
|
||||
}
|
||||
}}
|
||||
|
||||
@@ -8,4 +8,24 @@
|
||||
using namespace e2d;
|
||||
|
||||
TEST_CASE("address") {
|
||||
SECTION("parent") {
|
||||
REQUIRE(address::parent("") == "");
|
||||
REQUIRE(address::parent(":/") == "");
|
||||
REQUIRE(address::parent(":/child") == "");
|
||||
REQUIRE(address::parent("at/las.json") == "at/las.json");
|
||||
REQUIRE(address::parent("at/las.json:/") == "at/las.json");
|
||||
REQUIRE(address::parent("at/las.json:/spr/ite.png") == "at/las.json");
|
||||
REQUIRE(address::parent("at/las.json:/spr/ite.png:/") == "at/las.json");
|
||||
REQUIRE(address::parent("at/las.json:/spr/ite.png:/child") == "at/las.json");
|
||||
}
|
||||
SECTION("nested") {
|
||||
REQUIRE(address::nested("") == "");
|
||||
REQUIRE(address::nested(":/") == "");
|
||||
REQUIRE(address::nested(":/chi/ld") == "chi/ld");
|
||||
REQUIRE(address::nested("at/las.json") == "");
|
||||
REQUIRE(address::nested("at/las.json:/") == "");
|
||||
REQUIRE(address::nested("at/las.json:/spr/ite.png") == "spr/ite.png");
|
||||
REQUIRE(address::nested("at/las.json:/spr/ite.png:/") == "spr/ite.png:/");
|
||||
REQUIRE(address::nested("at/las.json:/spr/ite.png:/chi/ld") == "spr/ite.png:/chi/ld");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user