mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(tools): replace name_space parser with ResourceLocation
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
inline std::vector<std::string_view> parse_namespace(std::string_view str) {
|
||||
std::vector<std::string_view> space;
|
||||
space.reserve(4);
|
||||
std::size_t p = str.find(':');
|
||||
std::size_t start = 0;
|
||||
while (p != std::string_view::npos) {
|
||||
space.emplace_back(str.substr(start, p));
|
||||
start = p + 1;
|
||||
p = str.find(':', p + 1);
|
||||
}
|
||||
space.emplace_back(str.substr(start));
|
||||
return space;
|
||||
}
|
||||
} // namespace Cubed
|
||||
36
include/Cubed/tools/resource_location.hpp
Normal file
36
include/Cubed/tools/resource_location.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
namespace Cubed {
|
||||
struct ResourceLocation {
|
||||
static constexpr std::string_view DEFAULT_NAMESPACE = "cubed";
|
||||
std::string ns = std::string(DEFAULT_NAMESPACE);
|
||||
std::string path;
|
||||
std::string to_string() const { return ns + ":" + path; }
|
||||
|
||||
// Parses "ns:path"; ns defaults to "cubed" when no colon present.
|
||||
static std::optional<ResourceLocation> parse(std::string_view str) {
|
||||
std::regex pattern(R"([a-zA-Z0-9._:/-]+)");
|
||||
if (!std::regex_match(str.begin(), str.end(), pattern)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto it = str.find(":");
|
||||
if (it == std::string_view::npos) {
|
||||
return ResourceLocation{std::string(DEFAULT_NAMESPACE),
|
||||
std::string(str)};
|
||||
}
|
||||
return ResourceLocation{std::string(str.substr(0, it)),
|
||||
std::string(str.substr(it + 1))};
|
||||
}
|
||||
|
||||
bool operator==(const ResourceLocation& o) const {
|
||||
return (ns == o.ns) && (path == o.path);
|
||||
}
|
||||
std::size_t hash() const { return std::hash<std::string>()(to_string()); }
|
||||
};
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user