Skip to content

Commit

Permalink
Trim leading slashes of file URL paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisev committed Mar 29, 2017
1 parent ff97d04 commit 83f5f6c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ class url_serializer {
void save_path_string();

virtual void shorten_path();
virtual void remove_leading_path_slashes();

typedef bool (url::*PathOpFn)(std::size_t& path_end, unsigned& segment_count) const;
void append_parts(const url& src, url::PartType t1, url::PartType t2, PathOpFn pathOpFn = nullptr);
Expand Down Expand Up @@ -647,6 +648,7 @@ class url_setter : public url_serializer {
virtual void save_path_segment();
void commit_path();
virtual void shorten_path();
virtual void remove_leading_path_slashes();
virtual bool is_empty_path() const;

protected:
Expand Down Expand Up @@ -1633,7 +1635,12 @@ inline bool url_parser::url_parse(url_serializer& urls, const CharT* first, cons

parse_path(urls, pointer, end_of_path);
pointer = end_of_path;


// trim leading slashes of file URL path
if (urls.is_file_scheme()) {
urls.remove_leading_path_slashes();
}

if (pointer == last) {
return true;
} else {
Expand Down Expand Up @@ -2092,6 +2099,24 @@ inline void url_serializer::shorten_path() {
url_.norm_url_.resize(url_.part_end_[url::PATH]);
}

static inline size_t count_leading_path_slashes(const char* first, const char* last) {
return std::distance(first,
std::find_if_not(first, last, [](char c){ return c == '/'; }));
}

inline void url_serializer::remove_leading_path_slashes() {
assert(last_pt_ == url::PATH);
size_t count = count_leading_path_slashes(
url_.norm_url_.data() + url_.part_end_[url::PATH-1],
url_.norm_url_.data() + url_.part_end_[url::PATH]);
if (count > 1) {
count -= 1;
url_.norm_url_.erase(url_.part_end_[url::PATH-1], count);
url_.part_end_[url::PATH] -= count;
url_.path_segment_count_ -= count;
}
}

inline url_serializer::~url_serializer() {
switch (last_pt_) {
case url::SCHEME:
Expand Down Expand Up @@ -2330,6 +2355,15 @@ inline void url_setter::shorten_path() {
}
}

inline void url_setter::remove_leading_path_slashes() {
size_t count = count_leading_path_slashes(strp_.data(), strp_.data() + strp_.length());
if (count > 1) {
count -= 1;
strp_.erase(0, count);
path_seg_end_.erase(path_seg_end_.begin(), std::next(path_seg_end_.begin(), count));
}
}

inline bool url_setter::is_empty_path() const {
return path_seg_end_.empty();
}
Expand Down

0 comments on commit 83f5f6c

Please sign in to comment.