Skip to content

Commit

Permalink
Improve remove_dot_segments
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 3, 2024
1 parent 7f52a66 commit 77ba89f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
91 changes: 90 additions & 1 deletion include/jsoncons/utility/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ namespace jsoncons { namespace utility {
3. Finally, the output buffer is returned as the result of
remove_dot_segments.
*/
static std::string remove_dot_segments(jsoncons::string_view path)
static std::string remove_dot_segments2(jsoncons::string_view path)
{
std::string input{path};
std::string output;
Expand Down Expand Up @@ -898,6 +898,95 @@ namespace jsoncons { namespace utility {
return output;
}

static std::string remove_dot_segments(jsoncons::string_view path)
{
std::string input{path};
std::string output;

std::size_t rel = 0;
const std::size_t buflen = input.size();
while (rel < buflen)
{
std::size_t len = buflen - rel;
if (len >= 3 && input[rel] == '.' && input[rel+1] == '.' && input[rel+2] == '/')
{
//input.erase(0,3);
rel += 3;
}
else if (len >= 2 && input[rel] == '.' && input[rel+1] == '/')
{
//input.erase(0,2);
rel += 2;
}
else if (len >= 3 && input[rel] == '/' && input[rel+1] == '.' && input[rel+2] == '/')
{
//input.erase(0,2);
rel += 2;
input[rel] = '/';
}
else if (len == 2 && input[rel] == '/' && input[rel+1] == '.')
{
//input.erase(0,1);
++rel;
input[rel] = '/';
}
else if (len >= 4 && input[rel] == '/' && input[rel+1] == '.' && input[rel+2] == '.' && input[rel+3] == '/')
{
//input.erase(0,3);
rel += 3;
input[rel] = '/';
auto rslash = output.rfind('/');
if (rslash != std::string::npos)
{
output.erase(rslash);
}
}
else if (len >= 3 && input[rel] == '/' && input[rel+1] == '.' && input[rel+2] == '.')
{
//input.erase(0,2);
rel += 2;
input[rel] = '/';
auto rslash = output.rfind('/');
if (rslash != std::string::npos)
{
output.erase(rslash);
}
}
else if (len == 1 && input[rel] == '.')
{
//input.erase(0,1);
++rel;
}
else if (len == 2 && input[rel] == '.' && input[rel+1] == '.')
{
//input.erase(0,2);
rel += 2;
}
else
{
const auto first = input.data() + rel;
const auto last = first+(len);
auto it = std::find(first+1, last, '/');
if (it != last)
{
output.append(first, it - first);
//input.erase(0, it - first);
rel += (it - first);
}
else
{
output.append(first, len);
//input.erase(0, len);
rel += len;
}
}
}

//std::cout << "path: " << path << ", output: " << output << "\n";

return output;
}

static std::string merge_paths(const uri& base, const uri& relative)
{
std::string result;
Expand Down
3 changes: 2 additions & 1 deletion test/corelib/src/utility/uri_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,14 @@ TEST_CASE("cpp-netib uri resolve tests")
CHECK(uri.encoded_query() == jsoncons::string_view("q=1"));
CHECK("http://a/g/x/y?q=1#s" == uri.string());
}

SECTION("remove_dot_segments1")
{
jsoncons::uri reference{"./g"};
auto uri = reference.resolve(base_uri);
CHECK("http://a/b/c/g" == uri.string());
}

SECTION("base_has_path__path_is_merged_1")
{
jsoncons::uri reference{"g/"};
Expand Down Expand Up @@ -697,4 +699,3 @@ TEST_CASE("cpp-netib uri resolve tests")
CHECK("http://a/b/c/g#s/../x" == uri.string());
}
}

0 comments on commit 77ba89f

Please sign in to comment.