Merge pull request #179 from vibe-d/path_optimization
Add a nogc path segment API
This commit is contained in:
commit
24a83434e4
|
@ -1,14 +1,14 @@
|
||||||
/**
|
/**
|
||||||
Contains routines for high level path handling.
|
Contains routines for high level path handling.
|
||||||
|
|
||||||
Copyright: © 2012-2018 Sönke Ludwig
|
Copyright: © 2012-2019 Sönke Ludwig
|
||||||
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
|
||||||
Authors: Sönke Ludwig
|
Authors: Sönke Ludwig
|
||||||
*/
|
*/
|
||||||
module vibe.core.path;
|
module vibe.core.path;
|
||||||
|
|
||||||
import std.algorithm.searching : commonPrefix, endsWith, startsWith;
|
import std.algorithm.searching : commonPrefix, endsWith, startsWith;
|
||||||
import std.algorithm.comparison : min;
|
import std.algorithm.comparison : equal, min;
|
||||||
import std.algorithm.iteration : map;
|
import std.algorithm.iteration : map;
|
||||||
import std.exception : enforce;
|
import std.exception : enforce;
|
||||||
import std.range : empty, front, popFront, popFrontExactly, takeExactly;
|
import std.range : empty, front, popFront, popFrontExactly, takeExactly;
|
||||||
|
@ -27,7 +27,6 @@ import std.traits : isInstanceOf;
|
||||||
Path relativeTo(Path)(Path path, Path base_path) @safe
|
Path relativeTo(Path)(Path path, Path base_path) @safe
|
||||||
if (isInstanceOf!(GenericPath, Path))
|
if (isInstanceOf!(GenericPath, Path))
|
||||||
{
|
{
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
import std.array : array, replicate;
|
import std.array : array, replicate;
|
||||||
import std.range : chain, drop, take;
|
import std.range : chain, drop, take;
|
||||||
|
|
||||||
|
@ -37,35 +36,35 @@ Path relativeTo(Path)(Path path, Path base_path) @safe
|
||||||
if (is(Path == WindowsPath)) { // FIXME: this shouldn't be a special case here!
|
if (is(Path == WindowsPath)) { // FIXME: this shouldn't be a special case here!
|
||||||
bool samePrefix(size_t n)
|
bool samePrefix(size_t n)
|
||||||
{
|
{
|
||||||
return path.bySegment.map!(n => n.name).take(n).equal(base_path.bySegment.map!(n => n.name).take(n));
|
return path.bySegment2.map!(n => n.encodedName).take(n).equal(base_path.bySegment2.map!(n => n.encodedName).take(n));
|
||||||
}
|
}
|
||||||
// a path such as ..\C:\windows is not valid, so force the path to stay absolute in this case
|
// a path such as ..\C:\windows is not valid, so force the path to stay absolute in this case
|
||||||
auto pref = path.bySegment;
|
auto pref = path.bySegment2;
|
||||||
if (!pref.empty && pref.front.name == "") {
|
if (!pref.empty && pref.front.encodedName == "") {
|
||||||
pref.popFront();
|
pref.popFront();
|
||||||
if (!pref.empty) {
|
if (!pref.empty) {
|
||||||
// different drive?
|
// different drive?
|
||||||
if (pref.front.name.endsWith(':') && !samePrefix(2))
|
if (pref.front.encodedName.endsWith(':') && !samePrefix(2))
|
||||||
return path;
|
return path;
|
||||||
// different UNC path?
|
// different UNC path?
|
||||||
if (pref.front.name == "" && !samePrefix(4))
|
if (pref.front.encodedName == "" && !samePrefix(4))
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto nodes = path.bySegment;
|
auto nodes = path.bySegment2;
|
||||||
auto base_nodes = base_path.bySegment;
|
auto base_nodes = base_path.bySegment2;
|
||||||
|
|
||||||
// skip and count common prefix
|
// skip and count common prefix
|
||||||
size_t base = 0;
|
size_t base = 0;
|
||||||
while (!nodes.empty && !base_nodes.empty && nodes.front.name == base_nodes.front.name) {
|
while (!nodes.empty && !base_nodes.empty && equal(nodes.front.name, base_nodes.front.name)) {
|
||||||
nodes.popFront();
|
nodes.popFront();
|
||||||
base_nodes.popFront();
|
base_nodes.popFront();
|
||||||
base++;
|
base++;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum up = Path.Segment("..", Path.defaultSeparator);
|
enum up = Path.Segment2("..", Path.defaultSeparator);
|
||||||
auto ret = Path(base_nodes.map!(p => up).chain(nodes));
|
auto ret = Path(base_nodes.map!(p => up).chain(nodes));
|
||||||
if (path.endsWithSlash) {
|
if (path.endsWithSlash) {
|
||||||
if (ret.empty) return Path("." ~ path.toString()[$-1]);
|
if (ret.empty) return Path("." ~ path.toString()[$-1]);
|
||||||
|
@ -214,7 +213,6 @@ struct GenericPath(F) {
|
||||||
|
|
||||||
private {
|
private {
|
||||||
string m_name;
|
string m_name;
|
||||||
string m_encodedName;
|
|
||||||
char m_separator = 0;
|
char m_separator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,7 +317,7 @@ struct GenericPath(F) {
|
||||||
|
|
||||||
private {
|
private {
|
||||||
string m_path;
|
string m_path;
|
||||||
ReturnType!(Format.decodeSegment!Segment) m_fronts;
|
Segment m_front;
|
||||||
}
|
}
|
||||||
|
|
||||||
private this(string path)
|
private this(string path)
|
||||||
|
@ -327,37 +325,236 @@ struct GenericPath(F) {
|
||||||
m_path = path;
|
m_path = path;
|
||||||
if (m_path.length) {
|
if (m_path.length) {
|
||||||
auto ap = Format.getAbsolutePrefix(m_path);
|
auto ap = Format.getAbsolutePrefix(m_path);
|
||||||
if (ap.length) {
|
if (ap.length && !Format.isSeparator(ap[0]))
|
||||||
m_fronts = Format.decodeSegment!Segment(ap);
|
m_front = Segment.fromTrustedString(null, '/');
|
||||||
m_path = m_path[ap.length .. $];
|
else readFront();
|
||||||
assert(!m_fronts.empty);
|
}
|
||||||
} else readFront();
|
|
||||||
} else assert(m_fronts.empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@property bool empty() const nothrow @nogc { return m_path.length == 0 && m_fronts.empty; }
|
@property bool empty() const nothrow @nogc { return m_path.length == 0 && m_front == Segment.init; }
|
||||||
|
|
||||||
@property PathRange save() { return this; }
|
@property PathRange save() { return this; }
|
||||||
|
|
||||||
@property Segment front() { return m_fronts.front; }
|
@property Segment front() { return m_front; }
|
||||||
|
|
||||||
void popFront()
|
void popFront()
|
||||||
nothrow {
|
nothrow {
|
||||||
assert(!m_fronts.empty);
|
assert(m_front != Segment.init);
|
||||||
m_fronts.popFront();
|
if (m_path.length) readFront();
|
||||||
if (m_fronts.empty && m_path.length)
|
else m_front = Segment.init;
|
||||||
readFront();
|
}
|
||||||
|
|
||||||
|
private void readFront()
|
||||||
|
nothrow {
|
||||||
|
import std.array : array;
|
||||||
|
|
||||||
|
auto n = Format.getFrontNode(m_path);
|
||||||
|
m_path = m_path[n.length .. $];
|
||||||
|
|
||||||
|
char sep = '\0';
|
||||||
|
if (Format.isSeparator(n[$-1])) {
|
||||||
|
sep = n[$-1];
|
||||||
|
n = n[0 .. $-1];
|
||||||
|
}
|
||||||
|
static if (is(typeof(Format.decodeSingleSegment(n)) == string))
|
||||||
|
string ndec = Format.decodeSingleSegment(n);
|
||||||
|
else
|
||||||
|
string ndec = Format.decodeSingleSegment(n).array;
|
||||||
|
m_front = Segment.fromTrustedString(ndec, sep);
|
||||||
|
assert(m_front != Segment.init);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A single path segment.
|
||||||
|
*/
|
||||||
|
static struct Segment2 {
|
||||||
|
@safe:
|
||||||
|
|
||||||
|
private {
|
||||||
|
string m_encodedName;
|
||||||
|
char m_separator = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Constructs a new path segment including an optional trailing
|
||||||
|
separator.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
name = The raw (unencoded) name of the path segment
|
||||||
|
separator = Optional trailing path separator (e.g. `'/'`)
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
A `PathValidationException` is thrown if the name contains
|
||||||
|
characters that are invalid for the path type. In particular,
|
||||||
|
any path separator characters may not be part of the name.
|
||||||
|
*/
|
||||||
|
this(string name, char separator = '\0')
|
||||||
|
{
|
||||||
|
import std.algorithm.searching : any;
|
||||||
|
|
||||||
|
enforce!PathValidationException(separator == '\0' || Format.isSeparator(separator),
|
||||||
|
"Invalid path separator.");
|
||||||
|
auto err = Format.validateDecodedSegment(name);
|
||||||
|
enforce!PathValidationException(err is null, err);
|
||||||
|
|
||||||
|
m_encodedName = Format.encodeSegment(name);
|
||||||
|
m_separator = separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Constructs a path segment without performing validation.
|
||||||
|
|
||||||
|
Note that in debug builds, there are still assertions in place
|
||||||
|
that verify that the provided values are valid.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
name = The raw (unencoded) name of the path segment
|
||||||
|
separator = Optional trailing path separator (e.g. `'/'`)
|
||||||
|
*/
|
||||||
|
static Segment2 fromTrustedString(string name, char separator = '\0')
|
||||||
|
nothrow pure {
|
||||||
|
import std.algorithm.searching : any;
|
||||||
|
assert(separator == '\0' || Format.isSeparator(separator));
|
||||||
|
assert(Format.validateDecodedSegment(name) is null, "Invalid path segment.");
|
||||||
|
return fromTrustedEncodedString(Format.encodeSegment(name), separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Constructs a path segment without performing validation.
|
||||||
|
|
||||||
|
Note that in debug builds, there are still assertions in place
|
||||||
|
that verify that the provided values are valid.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
name = The encoded name of the path segment
|
||||||
|
separator = Optional trailing path separator (e.g. `'/'`)
|
||||||
|
*/
|
||||||
|
static Segment2 fromTrustedEncodedString(string encoded_name, char separator = '\0')
|
||||||
|
nothrow @nogc pure {
|
||||||
|
import std.algorithm.searching : any;
|
||||||
|
import std.utf : byCodeUnit;
|
||||||
|
|
||||||
|
assert(separator == '\0' || Format.isSeparator(separator));
|
||||||
|
assert(!encoded_name.byCodeUnit.any!(c => Format.isSeparator(c)));
|
||||||
|
assert(Format.validatePath(encoded_name) is null, "Invalid path segment.");
|
||||||
|
|
||||||
|
Segment2 ret;
|
||||||
|
ret.m_encodedName = encoded_name;
|
||||||
|
ret.m_separator = separator;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The (file/directory) name of the path segment.
|
||||||
|
|
||||||
|
Note: Depending on the path type, this may return a generic range
|
||||||
|
type instead of `string`. Use `name.to!string` in that
|
||||||
|
case if you need an actual `string`.
|
||||||
|
*/
|
||||||
|
@property auto name() const nothrow @nogc { return Format.decodeSingleSegment(m_encodedName); }
|
||||||
|
/// The encoded representation of the path segment name
|
||||||
|
@property string encodedName() const nothrow @nogc { return m_encodedName; }
|
||||||
|
/// The trailing separator (e.g. `'/'`) or `'\0'`.
|
||||||
|
@property char separator() const nothrow @nogc { return m_separator; }
|
||||||
|
/// ditto
|
||||||
|
@property void separator(char ch) {
|
||||||
|
enforce!PathValidationException(ch == '\0' || Format.isSeparator(ch),
|
||||||
|
"Character is not a valid path separator.");
|
||||||
|
m_separator = ch;
|
||||||
|
}
|
||||||
|
/// Returns `true` $(I iff) the segment has a trailing path separator.
|
||||||
|
@property bool hasSeparator() const nothrow @nogc { return m_separator != '\0'; }
|
||||||
|
|
||||||
|
/** Converts the segment to another path type.
|
||||||
|
|
||||||
|
The segment name will be re-validated during the conversion. The
|
||||||
|
separator, if any, will be adopted or replaced by the default
|
||||||
|
separator of the target path type.
|
||||||
|
|
||||||
|
Throws:
|
||||||
|
A `PathValidationException` is thrown if the segment name cannot
|
||||||
|
be represented in the target path format.
|
||||||
|
*/
|
||||||
|
GenericPath!F.Segment2 opCast(T : GenericPath!F.Segment2, F)()
|
||||||
|
{
|
||||||
|
import std.array : array;
|
||||||
|
|
||||||
|
char dsep = '\0';
|
||||||
|
if (m_separator) {
|
||||||
|
if (F.isSeparator(m_separator)) dsep = m_separator;
|
||||||
|
else dsep = F.defaultSeparator;
|
||||||
|
}
|
||||||
|
static if (is(typeof(this.name) == string))
|
||||||
|
string n = this.name;
|
||||||
|
else
|
||||||
|
string n = this.name.array;
|
||||||
|
return GenericPath!F.Segment2(n, dsep);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compares two path segment names
|
||||||
|
bool opEquals(Segment2 other)
|
||||||
|
const nothrow @nogc {
|
||||||
|
try return equal(this.name, other.name) && this.hasSeparator == other.hasSeparator;
|
||||||
|
catch (Exception e) assert(false, e.msg);
|
||||||
|
}
|
||||||
|
/// ditto
|
||||||
|
bool opEquals(string name)
|
||||||
|
const nothrow @nogc {
|
||||||
|
import std.utf : byCodeUnit;
|
||||||
|
try return equal(this.name, name.byCodeUnit);
|
||||||
|
catch (Exception e) assert(false, e.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Represents a path as an forward range of `Segment2`s.
|
||||||
|
|
||||||
|
Note that in contrast to `PathRange`, this range allows pure `@nogc`
|
||||||
|
iteration over encoded path segments.
|
||||||
|
*/
|
||||||
|
static struct PathRange2 {
|
||||||
|
import std.traits : ReturnType;
|
||||||
|
|
||||||
|
private {
|
||||||
|
string m_path;
|
||||||
|
Segment2 m_front;
|
||||||
|
}
|
||||||
|
|
||||||
|
private this(string path)
|
||||||
|
{
|
||||||
|
m_path = path;
|
||||||
|
if (m_path.length) {
|
||||||
|
auto ap = Format.getAbsolutePrefix(m_path);
|
||||||
|
if (ap.length && !Format.isSeparator(ap[0]))
|
||||||
|
m_front = Segment2.fromTrustedEncodedString(null, '/');
|
||||||
|
else readFront();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() const nothrow @nogc { return m_path.length == 0 && m_front == Segment2.init; }
|
||||||
|
|
||||||
|
@property PathRange2 save() { return this; }
|
||||||
|
|
||||||
|
@property Segment2 front() { return m_front; }
|
||||||
|
|
||||||
|
void popFront()
|
||||||
|
nothrow {
|
||||||
|
assert(m_front != Segment2.init);
|
||||||
|
if (m_path.length) readFront();
|
||||||
|
else m_front = Segment2.init;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readFront()
|
private void readFront()
|
||||||
{
|
{
|
||||||
auto n = Format.getFrontNode(m_path);
|
auto n = Format.getFrontNode(m_path);
|
||||||
m_fronts = Format.decodeSegment!Segment(n);
|
|
||||||
m_path = m_path[n.length .. $];
|
m_path = m_path[n.length .. $];
|
||||||
assert(!m_fronts.empty);
|
|
||||||
|
char sep = '\0';
|
||||||
|
if (Format.isSeparator(n[$-1])) {
|
||||||
|
sep = n[$-1];
|
||||||
|
n = n[0 .. $-1];
|
||||||
|
}
|
||||||
|
m_front = Segment2.fromTrustedEncodedString(n, sep);
|
||||||
|
assert(m_front != Segment2.init);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private {
|
private {
|
||||||
string m_path;
|
string m_path;
|
||||||
}
|
}
|
||||||
|
@ -388,6 +585,12 @@ struct GenericPath(F) {
|
||||||
import std.range : only;
|
import std.range : only;
|
||||||
this(only(segment));
|
this(only(segment));
|
||||||
}
|
}
|
||||||
|
/// ditto
|
||||||
|
this(Segment2 segment)
|
||||||
|
{
|
||||||
|
import std.range : only;
|
||||||
|
this(only(segment));
|
||||||
|
}
|
||||||
|
|
||||||
/** Constructs a path from an input range of `Segment`s.
|
/** Constructs a path from an input range of `Segment`s.
|
||||||
|
|
||||||
|
@ -403,6 +606,15 @@ struct GenericPath(F) {
|
||||||
Format.toString(segments, dst);
|
Format.toString(segments, dst);
|
||||||
m_path = dst.data;
|
m_path = dst.data;
|
||||||
}
|
}
|
||||||
|
/// ditto
|
||||||
|
this(R)(R segments)
|
||||||
|
if (isInputRange!R && is(ElementType!R : Segment2))
|
||||||
|
{
|
||||||
|
import std.array : appender;
|
||||||
|
auto dst = appender!string;
|
||||||
|
Format.toString(segments, dst);
|
||||||
|
m_path = dst.data;
|
||||||
|
}
|
||||||
|
|
||||||
/** Constructs a path from its string representation.
|
/** Constructs a path from its string representation.
|
||||||
|
|
||||||
|
@ -449,16 +661,39 @@ struct GenericPath(F) {
|
||||||
/// Iterates over the path by `Segment`.
|
/// Iterates over the path by `Segment`.
|
||||||
@property PathRange bySegment() const { return PathRange(m_path); }
|
@property PathRange bySegment() const { return PathRange(m_path); }
|
||||||
|
|
||||||
|
/// Iterates over the path by `Segment`.
|
||||||
|
@property PathRange2 bySegment2() const { return PathRange2(m_path); }
|
||||||
|
|
||||||
/// Returns the trailing segment of the path.
|
/// Returns the trailing segment of the path.
|
||||||
@property Segment head()
|
@property Segment head()
|
||||||
const {
|
const {
|
||||||
auto s = Format.decodeSegment!Segment(Format.getBackNode(m_path));
|
import std.array : array;
|
||||||
auto ret = s.front;
|
|
||||||
while (!s.empty) {
|
auto n = Format.getBackNode(m_path);
|
||||||
s.popFront();
|
char sep = '\0';
|
||||||
if (!s.empty) ret = s.front;
|
if (n.length > 0 && Format.isSeparator(n[$-1])) {
|
||||||
|
sep = n[$-1];
|
||||||
|
n = n[0 .. $-1];
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
|
static if (is(typeof(Format.decodeSingleSegment(n)) == string))
|
||||||
|
string ndec = Format.decodeSingleSegment(n);
|
||||||
|
else
|
||||||
|
string ndec = Format.decodeSingleSegment(n).array;
|
||||||
|
|
||||||
|
return Segment.fromTrustedString(ndec, sep);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the trailing segment of the path.
|
||||||
|
@property Segment2 head2()
|
||||||
|
const @nogc {
|
||||||
|
auto n = Format.getBackNode(m_path);
|
||||||
|
char sep = '\0';
|
||||||
|
if (n.length > 0 && Format.isSeparator(n[$-1])) {
|
||||||
|
sep = n[$-1];
|
||||||
|
n = n[0 .. $-1];
|
||||||
|
}
|
||||||
|
return Segment2.fromTrustedEncodedString(n, sep);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Determines if the `parentPath` property is valid.
|
/** Determines if the `parentPath` property is valid.
|
||||||
|
@ -503,20 +738,20 @@ struct GenericPath(F) {
|
||||||
{
|
{
|
||||||
import std.array : appender, join;
|
import std.array : appender, join;
|
||||||
|
|
||||||
Segment[] newnodes;
|
Segment2[] newnodes;
|
||||||
bool got_non_sep = false;
|
bool got_non_sep = false;
|
||||||
foreach (n; this.bySegment) {
|
foreach (n; this.bySegment2) {
|
||||||
if (n.hasSeparator) n.separator = Format.defaultSeparator;
|
if (n.hasSeparator) n.separator = Format.defaultSeparator;
|
||||||
if (!got_non_sep) {
|
if (!got_non_sep) {
|
||||||
if (n.name == "") newnodes ~= n;
|
if (n.encodedName == "") newnodes ~= n;
|
||||||
else got_non_sep = true;
|
else got_non_sep = true;
|
||||||
}
|
}
|
||||||
switch (n.name) {
|
switch (n.encodedName) {
|
||||||
default: newnodes ~= n; break;
|
default: newnodes ~= n; break;
|
||||||
case "", ".": break;
|
case "", ".": break;
|
||||||
case "..":
|
case "..":
|
||||||
enforce(!this.absolute || newnodes.length > 0, "Path goes below root node.");
|
enforce(!this.absolute || newnodes.length > 0, "Path goes below root node.");
|
||||||
if (newnodes.length > 0 && newnodes[$-1].name != "..") newnodes = newnodes[0 .. $-1];
|
if (newnodes.length > 0 && newnodes[$-1].encodedName != "..") newnodes = newnodes[0 .. $-1];
|
||||||
else newnodes ~= n;
|
else newnodes ~= n;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -567,7 +802,7 @@ struct GenericPath(F) {
|
||||||
*/
|
*/
|
||||||
P opCast(P)() const if (isInstanceOf!(.GenericPath, P)) {
|
P opCast(P)() const if (isInstanceOf!(.GenericPath, P)) {
|
||||||
static if (is(P == GenericPath)) return this;
|
static if (is(P == GenericPath)) return this;
|
||||||
else return P(this.bySegment.map!(n => cast(P.Segment)n));
|
else return P(this.bySegment2.map!(n => cast(P.Segment2)n));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Concatenates two paths.
|
/** Concatenates two paths.
|
||||||
|
@ -578,8 +813,12 @@ struct GenericPath(F) {
|
||||||
/// ditto
|
/// ditto
|
||||||
GenericPath opBinary(string op : "~")(Segment subpath) const { return this ~ GenericPath(subpath); }
|
GenericPath opBinary(string op : "~")(Segment subpath) const { return this ~ GenericPath(subpath); }
|
||||||
/// ditto
|
/// ditto
|
||||||
|
GenericPath opBinary(string op : "~")(Segment2 subpath) const { return this ~ GenericPath(subpath); }
|
||||||
|
/// ditto
|
||||||
GenericPath opBinary(string op : "~", F)(GenericPath!F.Segment subpath) const { return this ~ cast(Segment)(subpath); }
|
GenericPath opBinary(string op : "~", F)(GenericPath!F.Segment subpath) const { return this ~ cast(Segment)(subpath); }
|
||||||
/// ditto
|
/// ditto
|
||||||
|
GenericPath opBinary(string op : "~", F)(GenericPath!F.Segment2 subpath) const { return this ~ cast(Segment2)(subpath); }
|
||||||
|
/// ditto
|
||||||
GenericPath opBinary(string op : "~")(GenericPath subpath) const nothrow {
|
GenericPath opBinary(string op : "~")(GenericPath subpath) const nothrow {
|
||||||
assert(!subpath.absolute || m_path.length == 0, "Cannot append absolute path.");
|
assert(!subpath.absolute || m_path.length == 0, "Cannot append absolute path.");
|
||||||
if (endsWithSlash || empty) return GenericPath.fromTrustedString(m_path ~ subpath.m_path);
|
if (endsWithSlash || empty) return GenericPath.fromTrustedString(m_path ~ subpath.m_path);
|
||||||
|
@ -593,6 +832,12 @@ struct GenericPath(F) {
|
||||||
{
|
{
|
||||||
return this ~ GenericPath(entries);
|
return this ~ GenericPath(entries);
|
||||||
}
|
}
|
||||||
|
/// ditto
|
||||||
|
GenericPath opBinary(string op : "~", R)(R entries) const nothrow
|
||||||
|
if (isInputRange!R && is(ElementType!R : Segment2))
|
||||||
|
{
|
||||||
|
return this ~ GenericPath(entries);
|
||||||
|
}
|
||||||
|
|
||||||
/// Appends a relative path to this path.
|
/// Appends a relative path to this path.
|
||||||
void opOpAssign(string op : "~", T)(T op) { this = this ~ op; }
|
void opOpAssign(string op : "~", T)(T op) { this = this ~ op; }
|
||||||
|
@ -603,13 +848,11 @@ struct GenericPath(F) {
|
||||||
*/
|
*/
|
||||||
bool startsWith(GenericPath prefix)
|
bool startsWith(GenericPath prefix)
|
||||||
const nothrow {
|
const nothrow {
|
||||||
return bySegment.map!(n => n.name).startsWith(prefix.bySegment.map!(n => n.name));
|
return bySegment2.map!(n => n.name).startsWith(prefix.bySegment2.map!(n => n.name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
assert(PosixPath("hello/world").bySegment.equal([PosixPath.Segment("hello",'/'), PosixPath.Segment("world")]));
|
assert(PosixPath("hello/world").bySegment.equal([PosixPath.Segment("hello",'/'), PosixPath.Segment("world")]));
|
||||||
assert(PosixPath("/hello/world/").bySegment.equal([PosixPath.Segment("",'/'), PosixPath.Segment("hello",'/'), PosixPath.Segment("world",'/')]));
|
assert(PosixPath("/hello/world/").bySegment.equal([PosixPath.Segment("",'/'), PosixPath.Segment("hello",'/'), PosixPath.Segment("world",'/')]));
|
||||||
assert(PosixPath("hello\\world").bySegment.equal([PosixPath.Segment("hello\\world")]));
|
assert(PosixPath("hello\\world").bySegment.equal([PosixPath.Segment("hello\\world")]));
|
||||||
|
@ -619,10 +862,18 @@ unittest {
|
||||||
assert(WindowsPath("hello/w\\orld").bySegment.equal([WindowsPath.Segment("hello",'/'), WindowsPath.Segment("w",'\\'), WindowsPath.Segment("orld")]));
|
assert(WindowsPath("hello/w\\orld").bySegment.equal([WindowsPath.Segment("hello",'/'), WindowsPath.Segment("w",'\\'), WindowsPath.Segment("orld")]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
assert(PosixPath("hello/world").bySegment2.equal([PosixPath.Segment2("hello",'/'), PosixPath.Segment2("world")]));
|
||||||
|
assert(PosixPath("/hello/world/").bySegment2.equal([PosixPath.Segment2("",'/'), PosixPath.Segment2("hello",'/'), PosixPath.Segment2("world",'/')]));
|
||||||
|
assert(PosixPath("hello\\world").bySegment2.equal([PosixPath.Segment2("hello\\world")]));
|
||||||
|
assert(WindowsPath("hello/world").bySegment2.equal([WindowsPath.Segment2("hello",'/'), WindowsPath.Segment2("world")]));
|
||||||
|
assert(WindowsPath("/hello/world/").bySegment2.equal([WindowsPath.Segment2("",'/'), WindowsPath.Segment2("hello",'/'), WindowsPath.Segment2("world",'/')]));
|
||||||
|
assert(WindowsPath("hello\\w/orld").bySegment2.equal([WindowsPath.Segment2("hello",'\\'), WindowsPath.Segment2("w",'/'), WindowsPath.Segment2("orld")]));
|
||||||
|
assert(WindowsPath("hello/w\\orld").bySegment2.equal([WindowsPath.Segment2("hello",'/'), WindowsPath.Segment2("w",'\\'), WindowsPath.Segment2("orld")]));
|
||||||
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto unc = "\\\\server\\share\\path";
|
auto unc = "\\\\server\\share\\path";
|
||||||
auto uncp = WindowsPath(unc);
|
auto uncp = WindowsPath(unc);
|
||||||
|
@ -702,6 +953,55 @@ unittest
|
||||||
assert(PosixPath("foo/") ~ NativePath("bar") == PosixPath("foo/bar"));
|
assert(PosixPath("foo/") ~ NativePath("bar") == PosixPath("foo/bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
{
|
||||||
|
auto unc = "\\\\server\\share\\path";
|
||||||
|
auto uncp = WindowsPath(unc);
|
||||||
|
assert(uncp.absolute);
|
||||||
|
uncp.normalize();
|
||||||
|
version(Windows) assert(uncp.toNativeString() == unc);
|
||||||
|
assert(uncp.absolute);
|
||||||
|
assert(!uncp.endsWithSlash);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto abspath = "/test/path/";
|
||||||
|
auto abspathp = PosixPath(abspath);
|
||||||
|
assert(abspathp.toString() == abspath);
|
||||||
|
version(Windows) {} else assert(abspathp.toNativeString() == abspath);
|
||||||
|
assert(abspathp.absolute);
|
||||||
|
assert(abspathp.endsWithSlash);
|
||||||
|
alias S = PosixPath.Segment2;
|
||||||
|
assert(abspathp.bySegment2.equal([S("", '/'), S("test", '/'), S("path", '/')]));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto relpath = "test/path/";
|
||||||
|
auto relpathp = PosixPath(relpath);
|
||||||
|
assert(relpathp.toString() == relpath);
|
||||||
|
version(Windows) assert(relpathp.toNativeString() == "test/path/");
|
||||||
|
else assert(relpathp.toNativeString() == relpath);
|
||||||
|
assert(!relpathp.absolute);
|
||||||
|
assert(relpathp.endsWithSlash);
|
||||||
|
alias S = PosixPath.Segment2;
|
||||||
|
assert(relpathp.bySegment2.equal([S("test", '/'), S("path", '/')]));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto winpath = "C:\\windows\\test";
|
||||||
|
auto winpathp = WindowsPath(winpath);
|
||||||
|
assert(winpathp.toString() == "C:\\windows\\test");
|
||||||
|
assert((cast(PosixPath)winpathp).toString() == "/C:/windows/test", (cast(PosixPath)winpathp).toString());
|
||||||
|
version(Windows) assert(winpathp.toNativeString() == winpath);
|
||||||
|
else assert(winpathp.toNativeString() == "/C:/windows/test");
|
||||||
|
assert(winpathp.absolute);
|
||||||
|
assert(!winpathp.endsWithSlash);
|
||||||
|
alias S = WindowsPath.Segment2;
|
||||||
|
assert(winpathp.bySegment2.equal([S("", '/'), S("C:", '\\'), S("windows", '\\'), S("test")]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@safe unittest {
|
@safe unittest {
|
||||||
import std.array : appender;
|
import std.array : appender;
|
||||||
auto app = appender!(PosixPath[]);
|
auto app = appender!(PosixPath[]);
|
||||||
|
@ -712,7 +1012,6 @@ unittest
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
import std.exception : assertThrown, assertNotThrown;
|
import std.exception : assertThrown, assertNotThrown;
|
||||||
|
|
||||||
assertThrown!PathValidationException(WindowsPath.Segment("foo/bar"));
|
assertThrown!PathValidationException(WindowsPath.Segment("foo/bar"));
|
||||||
|
@ -725,6 +1024,20 @@ unittest {
|
||||||
assert(p.toString() == "/foo%2fbar/baz%2Fbam", p.toString);
|
assert(p.toString() == "/foo%2fbar/baz%2Fbam", p.toString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
import std.exception : assertThrown, assertNotThrown;
|
||||||
|
|
||||||
|
assertThrown!PathValidationException(WindowsPath.Segment2("foo/bar"));
|
||||||
|
assertThrown!PathValidationException(PosixPath.Segment2("foo/bar"));
|
||||||
|
assertNotThrown!PathValidationException(InetPath.Segment2("foo/bar"));
|
||||||
|
|
||||||
|
auto p = InetPath("/foo%2fbar/");
|
||||||
|
import std.conv : to;
|
||||||
|
assert(p.bySegment2.equal([InetPath.Segment2("",'/'), InetPath.Segment2("foo/bar",'/')]), p.bySegment2.to!string);
|
||||||
|
p ~= InetPath.Segment2("baz/bam");
|
||||||
|
assert(p.toString() == "/foo%2fbar/baz%2Fbam", p.toString);
|
||||||
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
assert(!PosixPath("").hasParentPath);
|
assert(!PosixPath("").hasParentPath);
|
||||||
assert(!PosixPath("/").hasParentPath);
|
assert(!PosixPath("/").hasParentPath);
|
||||||
|
@ -752,6 +1065,10 @@ unittest {
|
||||||
assert(WindowsPath([WindowsPath.Segment("foo"), WindowsPath.Segment("bar")]).toString() == "foo\\bar");
|
assert(WindowsPath([WindowsPath.Segment("foo"), WindowsPath.Segment("bar")]).toString() == "foo\\bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
assert(WindowsPath([WindowsPath.Segment2("foo"), WindowsPath.Segment2("bar")]).toString() == "foo\\bar");
|
||||||
|
}
|
||||||
|
|
||||||
/// Thrown when an invalid string representation of a path is detected.
|
/// Thrown when an invalid string representation of a path is detected.
|
||||||
class PathValidationException : Exception {
|
class PathValidationException : Exception {
|
||||||
this(string text, string file = __FILE__, size_t line = cast(size_t)__LINE__, Throwable next = null)
|
this(string text, string file = __FILE__, size_t line = cast(size_t)__LINE__, Throwable next = null)
|
||||||
|
@ -893,6 +1210,7 @@ struct WindowsPathFormat {
|
||||||
assert(getBackNode("foo\\") == "foo\\");
|
assert(getBackNode("foo\\") == "foo\\");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deprecated("Use decodeSingleSegment instead.")
|
||||||
static auto decodeSegment(S)(string segment)
|
static auto decodeSegment(S)(string segment)
|
||||||
{
|
{
|
||||||
static struct R {
|
static struct R {
|
||||||
|
@ -922,7 +1240,6 @@ struct WindowsPathFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
||||||
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
||||||
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
||||||
|
@ -931,6 +1248,20 @@ struct WindowsPathFormat {
|
||||||
assert(decodeSegment!Segment("bar:\\").equal([Segment("",'/'), Segment("bar:", '\\')]));
|
assert(decodeSegment!Segment("bar:\\").equal([Segment("",'/'), Segment("bar:", '\\')]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string decodeSingleSegment(string segment)
|
||||||
|
@nogc {
|
||||||
|
assert(segment.length == 0 || segment[$-1] != '/');
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
||||||
|
assert(decodeSingleSegment("foo") == "foo");
|
||||||
|
assert(decodeSingleSegment("fo%20o") == "fo%20o");
|
||||||
|
assert(decodeSingleSegment("C:") == "C:");
|
||||||
|
assert(decodeSingleSegment("bar:") == "bar:");
|
||||||
|
}
|
||||||
|
|
||||||
static string validatePath(string path)
|
static string validatePath(string path)
|
||||||
@nogc {
|
@nogc {
|
||||||
import std.algorithm.comparison : among;
|
import std.algorithm.comparison : among;
|
||||||
|
@ -981,6 +1312,12 @@ struct WindowsPathFormat {
|
||||||
assert(validatePath("foo\\b:ar") !is null);
|
assert(validatePath("foo\\b:ar") !is null);
|
||||||
assert(validatePath("foo\\bar:\\baz") !is null);
|
assert(validatePath("foo\\bar:\\baz") !is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string encodeSegment(string segment)
|
||||||
|
{
|
||||||
|
assert(segment.length == 0 || segment[$-1] != '/');
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1095,6 +1432,7 @@ struct PosixPathFormat {
|
||||||
assert(validatePath("foo\0bar") !is null);
|
assert(validatePath("foo\0bar") !is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deprecated("Use decodeSingleSegment instead.")
|
||||||
static auto decodeSegment(S)(string segment)
|
static auto decodeSegment(S)(string segment)
|
||||||
{
|
{
|
||||||
assert(segment.length > 0, "Path segment string must not be empty.");
|
assert(segment.length > 0, "Path segment string must not be empty.");
|
||||||
|
@ -1106,12 +1444,29 @@ struct PosixPathFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
||||||
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
||||||
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
||||||
assert(decodeSegment!Segment("fo%20o\\").equal([Segment("fo%20o\\")]));
|
assert(decodeSegment!Segment("fo%20o\\").equal([Segment("fo%20o\\")]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string decodeSingleSegment(string segment)
|
||||||
|
@nogc {
|
||||||
|
assert(segment.length == 0 || segment[$-1] != '/');
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
||||||
|
assert(decodeSingleSegment("foo") == "foo");
|
||||||
|
assert(decodeSingleSegment("fo%20o\\") == "fo%20o\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
static string encodeSegment(string segment)
|
||||||
|
{
|
||||||
|
assert(segment.length == 0 || segment[$-1] != '/');
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1122,28 +1477,14 @@ struct PosixPathFormat {
|
||||||
struct InetPathFormat {
|
struct InetPathFormat {
|
||||||
static void toString(I, O)(I segments, O dst)
|
static void toString(I, O)(I segments, O dst)
|
||||||
{
|
{
|
||||||
import std.format : formattedWrite;
|
|
||||||
|
|
||||||
char lastsep = '\0';
|
char lastsep = '\0';
|
||||||
bool first = true;
|
bool first = true;
|
||||||
foreach (e; segments) {
|
foreach (e; segments) {
|
||||||
if (!first || lastsep) dst.put('/');
|
if (!first || lastsep) dst.put('/');
|
||||||
else first = false;
|
else first = false;
|
||||||
foreach (char c; e.name) {
|
static if (is(typeof(e.encodedName)))
|
||||||
switch (c) {
|
dst.put(e.encodedName);
|
||||||
default:
|
else encodeSegment(dst, e.name);
|
||||||
dst.formattedWrite("%%%02X", c);
|
|
||||||
break;
|
|
||||||
case 'a': .. case 'z':
|
|
||||||
case 'A': .. case 'Z':
|
|
||||||
case '0': .. case '9':
|
|
||||||
case '-', '.', '_', '~':
|
|
||||||
case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
|
|
||||||
case ':', '@':
|
|
||||||
dst.put(c);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lastsep = e.separator;
|
lastsep = e.separator;
|
||||||
}
|
}
|
||||||
if (lastsep) dst.put('/');
|
if (lastsep) dst.put('/');
|
||||||
|
@ -1203,10 +1544,13 @@ struct InetPathFormat {
|
||||||
|
|
||||||
static string getBackNode(string path)
|
static string getBackNode(string path)
|
||||||
@nogc {
|
@nogc {
|
||||||
|
import std.string : lastIndexOf;
|
||||||
|
|
||||||
if (!path.length) return path;
|
if (!path.length) return path;
|
||||||
foreach_reverse (i; 0 .. path.length-1)
|
ptrdiff_t idx;
|
||||||
if (path[i] == '/')
|
try idx = path[0 .. $-1].lastIndexOf('/');
|
||||||
return path[i+1 .. $];
|
catch (Exception e) assert(false, e.msg);
|
||||||
|
if (idx >= 0) return path[idx+1 .. $];
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1273,11 +1617,38 @@ struct InetPathFormat {
|
||||||
assert(validatePath("föö") !is null);
|
assert(validatePath("föö") !is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deprecated("Use decodeSingleSegment instead.")
|
||||||
static auto decodeSegment(S)(string segment)
|
static auto decodeSegment(S)(string segment)
|
||||||
{
|
{
|
||||||
import std.array : appender;
|
import std.algorithm.searching : any;
|
||||||
import std.format : formattedRead;
|
import std.array : array;
|
||||||
|
import std.exception : assumeUnique;
|
||||||
import std.range : only;
|
import std.range : only;
|
||||||
|
import std.utf : byCodeUnit;
|
||||||
|
|
||||||
|
if (!segment.length) return only(S.fromTrustedString(null));
|
||||||
|
char sep = '\0';
|
||||||
|
if (segment[$-1] == '/') {
|
||||||
|
sep = '/';
|
||||||
|
segment = segment[0 .. $-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!segment.byCodeUnit.any!(c => c == '%'))
|
||||||
|
return only(S(segment, sep));
|
||||||
|
string n = decodeSingleSegment(segment).array;
|
||||||
|
return only(S(n, sep));
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
||||||
|
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
||||||
|
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
||||||
|
assert(decodeSegment!Segment("fo%20o\\").equal([Segment("fo o\\")]));
|
||||||
|
assert(decodeSegment!Segment("foo%20").equal([Segment("foo ")]));
|
||||||
|
}
|
||||||
|
|
||||||
|
static auto decodeSingleSegment(string segment)
|
||||||
|
@nogc {
|
||||||
import std.string : indexOf;
|
import std.string : indexOf;
|
||||||
|
|
||||||
static int hexDigit(char ch) @safe nothrow @nogc {
|
static int hexDigit(char ch) @safe nothrow @nogc {
|
||||||
|
@ -1287,40 +1658,105 @@ struct InetPathFormat {
|
||||||
else return ch - 'A' + 10;
|
else return ch - 'A' + 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string urlDecode(string s) @safe nothrow {
|
static struct R {
|
||||||
auto idx = s.indexOf('%');
|
@safe pure nothrow @nogc:
|
||||||
if (idx < 0) return s;
|
|
||||||
|
|
||||||
auto ret = appender!string;
|
private {
|
||||||
ret.put(s[0 .. idx]);
|
string m_str;
|
||||||
|
size_t m_index;
|
||||||
for (size_t i = idx; i < s.length; i++) {
|
|
||||||
if (s[i] == '%') {
|
|
||||||
assert(i+2 < s.length, "segment string not validated!?");
|
|
||||||
ret.put(cast(char)(hexDigit(s[i+1]) << 4 | hexDigit(s[i+2])));
|
|
||||||
i += 2;
|
|
||||||
} else ret.put(s[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret.data;
|
this(string s)
|
||||||
|
{
|
||||||
|
m_str = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property bool empty() const { return m_index >= m_str.length; }
|
||||||
|
|
||||||
|
@property char front()
|
||||||
|
const {
|
||||||
|
auto ch = m_str[m_index];
|
||||||
|
if (ch != '%') return ch;
|
||||||
|
|
||||||
|
auto a = m_str[m_index+1];
|
||||||
|
auto b = m_str[m_index+2];
|
||||||
|
return cast(char)(16 * hexDigit(a) + hexDigit(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
@property void popFront()
|
||||||
|
{
|
||||||
|
assert(!empty);
|
||||||
|
if (m_str[m_index] == '%') m_index += 3;
|
||||||
|
else m_index++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!segment.length) return only(S.fromTrustedString(null));
|
return R(segment);
|
||||||
if (segment[$-1] == '/')
|
|
||||||
return only(S.fromTrustedString(urlDecode(segment[0 .. $-1]), '/'));
|
|
||||||
return only(S.fromTrustedString(urlDecode(segment)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
import std.algorithm.comparison : equal;
|
scope (failure) assert(false);
|
||||||
struct Segment { string name; char separator = 0; static Segment fromTrustedString(string str, char sep = 0) pure nothrow @nogc { return Segment(str, sep); }}
|
|
||||||
assert(decodeSegment!Segment("foo").equal([Segment("foo")]));
|
assert(decodeSingleSegment("foo").equal("foo"));
|
||||||
assert(decodeSegment!Segment("foo/").equal([Segment("foo", '/')]));
|
assert(decodeSingleSegment("fo%20o\\").equal("fo o\\"));
|
||||||
assert(decodeSegment!Segment("fo%20o\\").equal([Segment("fo o\\")]));
|
assert(decodeSingleSegment("foo%20").equal("foo "));
|
||||||
assert(decodeSegment!Segment("foo%20").equal([Segment("foo ")]));
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static string encodeSegment(string segment)
|
||||||
|
{
|
||||||
|
import std.array : appender;
|
||||||
|
|
||||||
|
foreach (i, char c; segment) {
|
||||||
|
switch (c) {
|
||||||
|
default:
|
||||||
|
auto ret = appender!string;
|
||||||
|
ret.put(segment[0 .. i]);
|
||||||
|
encodeSegment(ret, segment[i .. $]);
|
||||||
|
return ret.data;
|
||||||
|
case 'a': .. case 'z':
|
||||||
|
case 'A': .. case 'Z':
|
||||||
|
case '0': .. case '9':
|
||||||
|
case '-', '.', '_', '~':
|
||||||
|
case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
|
||||||
|
case ':', '@':
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
assert(encodeSegment("foo") == "foo");
|
||||||
|
assert(encodeSegment("foo bar") == "foo%20bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void encodeSegment(R)(ref R dst, string segment)
|
||||||
|
{
|
||||||
|
static immutable char[16] digit = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
||||||
|
|
||||||
|
foreach (char c; segment) {
|
||||||
|
switch (c) {
|
||||||
|
default:
|
||||||
|
dst.put('%');
|
||||||
|
dst.put(digit[uint(c) / 16]);
|
||||||
|
dst.put(digit[uint(c) % 16]);
|
||||||
|
break;
|
||||||
|
case 'a': .. case 'z':
|
||||||
|
case 'A': .. case 'Z':
|
||||||
|
case '0': .. case '9':
|
||||||
|
case '-', '.', '_', '~':
|
||||||
|
case '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
|
||||||
|
case ':', '@':
|
||||||
|
dst.put(c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest { // regression tests
|
unittest { // regression tests
|
||||||
assert(NativePath("").bySegment.empty);
|
assert(NativePath("").bySegment.empty);
|
||||||
|
assert(NativePath("").bySegment2.empty);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue