Created
February 7, 2017 07:42
-
-
Save nekko1119/22b51acf92ecb7521d6d5a809dba810e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <regex> | |
#include <string> | |
#include <sstream> | |
#include <vector> | |
std::vector<std::string> split(std::string const& target, std::regex const& pattern) | |
{ | |
std::sregex_token_iterator first{target.begin(), target.end(), pattern, -1}; | |
std::sregex_token_iterator last; | |
return std::vector<std::string>(first, last); | |
} | |
std::string resolve(std::string const& base, std::string const& target) | |
{ | |
auto base_token = split(base, std::regex{"/"}); | |
auto target_token = split(target, std::regex{"/"}); | |
auto base_it = base_token.begin(); | |
auto target_it = target_token.begin(); | |
while (*base_it == *target_it && base_it != base_token.end() && target_it != target_token.end()) { | |
++base_it; | |
++target_it; | |
} | |
base_token.erase(base_token.begin(), base_it); | |
target_token.erase(target_token.begin(), target_it); | |
for (auto i = 0U; i < base_token.size() -1; ++i) { | |
target_token.insert(target_token.begin(), ".."); | |
} | |
std::ostringstream oss; | |
bool is_first = true; | |
for (auto const path : target_token) { | |
if (!std::exchange(is_first, false)) { | |
oss << "/"; | |
} | |
oss << path; | |
} | |
return oss.str(); | |
} | |
int main() | |
{ | |
std::cout << resolve("a/b/c/d.js", "a/e/f/g.js") << std::endl; | |
std::cout << resolve("a.js", "b/c/d/e.js") << std::endl; | |
std::cout << resolve("a/b/c/d.js", "e.js") << std::endl; | |
std::cout << resolve("a.js", "b.js") << std::endl; | |
std::cout << resolve("a/b/c.js", "d/e/f.js") << std::endl; | |
std::cout << resolve("a/b/c/d.js", "a/b/c/e.js") << std::endl; | |
std::cout << resolve("a/b/c/d.js", "a/b/e/f.js") << std::endl; | |
std::cout << resolve("a/b/c/d.js", "a/b/f.js") << std::endl; | |
std::cout << resolve("a/b/c.js", "a/b/d/e.js") << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment