From 26b903f02f8cfb9022556f0788319c4002385aa69c658759364a3717e7a3d89e Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Sat, 12 Apr 2025 04:25:55 -0400 Subject: [PATCH] Test drive using `std::string` to replace dstr. Looks like it worked within the URL code. Now we can really start to slice-and-dice. --- src/url.cc | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/url.cc b/src/url.cc index e7919e5..0feede9 100644 --- a/src/url.cc +++ b/src/url.cc @@ -51,6 +51,8 @@ #include "misc.hh" #include "msg.hh" +#include + static const char *HEX = "0123456789ABCDEF"; /* URL-field compare methods */ @@ -221,9 +223,10 @@ DilloUrl::~DilloUrl() static Dstr *Url_resolve_relative(const char *RelStr, const char *BaseStr) { - char *p, *s, *e; + const char *p, *s, *e; int i; - Dstr *SolvedUrl, *Path; + Dstr *SolvedUrl; + std::string Path; DilloUrl *RelUrl, *BaseUrl = NULL; /* parse relative URL */ @@ -235,7 +238,6 @@ static Dstr *Url_resolve_relative(const char *RelStr, } SolvedUrl = dStr_sized_new(64); - Path = dStr_sized_new(64); /* path empty && scheme and authority undefined */ if (!RelUrl->path && !RelUrl->scheme && !RelUrl->authority) { @@ -264,36 +266,39 @@ static Dstr *Url_resolve_relative(const char *RelStr, } else if (RelUrl->authority) { /* authority */ // Set the Path buffer and goto "STEP 7"; if (RelUrl->path) - dStr_append(Path, RelUrl->path); + Path+= RelUrl->path; } else { if (RelUrl->path && RelUrl->path[0] == '/') { /* absolute path */ ; /* Ignore BaseUrl path */ } else if (BaseUrl->path) { /* relative path */ - dStr_append(Path, BaseUrl->path); - for (i = Path->len; --i >= 0 && Path->str[i] != '/'; ) ; - if (i >= 0 && Path->str[i] == '/') - dStr_truncate(Path, ++i); + Path+= BaseUrl->path; + for (i = Path.size(); --i >= 0 && Path.at( i ) != '/'; ) ; + if (i >= 0 && Path.at(i) == '/') + { + ++i; + if( i < Path.size() ) Path.resize( i ); + } } if (RelUrl->path) - dStr_append(Path, RelUrl->path); + Path+= RelUrl->path; // erase "./" - while ((p=strstr(Path->str, "./")) && - (p == Path->str || p[-1] == '/')) - dStr_erase(Path, p - Path->str, 2); + while ((p=strstr(Path.c_str(), "./")) && + (p == Path.c_str() || p[-1] == '/')) + Path.erase( p - Path.c_str(), 2 ); // erase last "." - if (Path->len && Path->str[Path->len - 1] == '.' && - (Path->len == 1 || Path->str[Path->len - 2] == '/')) - dStr_truncate(Path, Path->len - 1); + if (Path.size() && Path.at(Path.size() - 1) == '.' && + (Path.size() == 1 || Path.at(Path.size() - 2) == '/')) + Path.pop_back(); // erase "/../" and "/.." - s = p = Path->str; + s = p = Path.c_str(); while ( (p = strstr(p, "/..")) != NULL ) { if (p[3] == '/' || !p[3]) { // "/../" | "/.." for (e = p + 3 ; p > s && p[-1] != '/'; --p) ; - dStr_erase(Path, p - Path->str, e - p + (p > s && *e != 0)); - p -= (p > Path->str); + Path.erase( p - Path.c_str(), e - p + (p > s && *e != 0) ); + p -= (p > Path.c_str()); } else p += 3; } @@ -319,10 +324,10 @@ static Dstr *Url_resolve_relative(const char *RelStr, /* path */ if ((RelUrl->authority || BaseUrl->authority) && - ((Path->len == 0 && (RelUrl->query || RelUrl->fragment)) || - (Path->len && Path->str[0] != '/'))) + ((Path.size() == 0 && (RelUrl->query || RelUrl->fragment)) || + (Path.size() && Path.at(0) != '/'))) dStr_append_c(SolvedUrl, '/'); /* hack? */ - dStr_append(SolvedUrl, Path->str); + dStr_append(SolvedUrl, Path.c_str()); /* query */ if (RelUrl->query) { @@ -337,7 +342,6 @@ static Dstr *Url_resolve_relative(const char *RelStr, } done: - dStr_free(Path, TRUE); delete RelUrl; delete BaseUrl; return SolvedUrl;