mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 23:20:29 +03:00
std.string.detab_alias_fix
make detab and detabber accept custom types with an alias this to a string member of member function rainers improvment more rainer fixes and more testing more rainer
This commit is contained in:
parent
8d9d606ef8
commit
ca724eb0f0
1 changed files with 78 additions and 8 deletions
86
std/string.d
86
std/string.d
|
@ -3857,16 +3857,50 @@ unittest
|
||||||
Returns:
|
Returns:
|
||||||
GC allocated string with tabs replaced with spaces
|
GC allocated string with tabs replaced with spaces
|
||||||
+/
|
+/
|
||||||
S detab(S)(S s, size_t tabSize = 8) pure
|
auto detab(Range)(auto ref Range s, size_t tabSize = 8) pure
|
||||||
if (isSomeString!S)
|
if ((isForwardRange!Range && isSomeChar!(ElementEncodingType!Range))
|
||||||
|
|| __traits(compiles, StringTypeOf!Range))
|
||||||
{
|
{
|
||||||
import std.array;
|
import std.array;
|
||||||
return detabber(s, tabSize).array;
|
return detabber(s, tabSize).array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@trusted pure unittest
|
||||||
|
{
|
||||||
|
import std.array;
|
||||||
|
|
||||||
|
assert(detab(" \n\tx", 9) == " \n x");
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
static struct TestStruct
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
alias s this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct TestStruct2
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
alias s this;
|
||||||
|
@disable this(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
string s = " \n\tx";
|
||||||
|
string cmp = " \n x";
|
||||||
|
auto t = TestStruct(s);
|
||||||
|
assert(detab(t, 9) == cmp);
|
||||||
|
assert(detab(TestStruct(s), 9) == cmp);
|
||||||
|
assert(detab(TestStruct(s), 9) == detab(TestStruct(s), 9));
|
||||||
|
assert(detab(TestStruct2(s), 9) == detab(TestStruct2(s), 9));
|
||||||
|
assert(detab(TestStruct2(s), 9) == cmp);
|
||||||
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
Replace each tab character in $(D r) with the number of spaces necessary
|
Replace each tab character in $(D r) with the number of spaces
|
||||||
to align the following character at the next tab stop.
|
necessary to align the following character at the next tab stop.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
r = string or forward range
|
r = string or forward range
|
||||||
|
@ -3882,7 +3916,8 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
import std.utf : codeUnitLimit, decodeFront;
|
import std.utf : codeUnitLimit, decodeFront;
|
||||||
|
|
||||||
assert(tabSize > 0);
|
assert(tabSize > 0);
|
||||||
alias C = Unqual!(ElementEncodingType!Range);
|
|
||||||
|
alias C = Unqual!(ElementEncodingType!(Range));
|
||||||
|
|
||||||
static struct Result
|
static struct Result
|
||||||
{
|
{
|
||||||
|
@ -3901,7 +3936,7 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
_tabSize = tabSize;
|
_tabSize = tabSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
static if (isInfinite!Range)
|
static if (isInfinite!(Range))
|
||||||
{
|
{
|
||||||
enum bool empty = false;
|
enum bool empty = false;
|
||||||
}
|
}
|
||||||
|
@ -3917,7 +3952,7 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
{
|
{
|
||||||
if (nspaces)
|
if (nspaces)
|
||||||
return ' ';
|
return ' ';
|
||||||
static if (isSomeString!Range)
|
static if (isSomeString!(Range))
|
||||||
C c = _input[0];
|
C c = _input[0];
|
||||||
else
|
else
|
||||||
C c = _input.front;
|
C c = _input.front;
|
||||||
|
@ -3965,7 +4000,7 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
--nspaces;
|
--nspaces;
|
||||||
if (!nspaces)
|
if (!nspaces)
|
||||||
{
|
{
|
||||||
static if (isSomeString!Range)
|
static if (isSomeString!(Range))
|
||||||
_input = _input[1 .. $];
|
_input = _input[1 .. $];
|
||||||
else
|
else
|
||||||
_input.popFront();
|
_input.popFront();
|
||||||
|
@ -3984,6 +4019,14 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
return Result(r, tabSize);
|
return Result(r, tabSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
auto detabber(Range)(auto ref Range s, size_t tabSize = 8) pure
|
||||||
|
if (!(isForwardRange!Range && isSomeChar!(ElementEncodingType!Range))
|
||||||
|
&& is(StringTypeOf!Range))
|
||||||
|
{
|
||||||
|
return detabber(cast(StringTypeOf!Range)s, tabSize);
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@trusted pure unittest
|
@trusted pure unittest
|
||||||
{
|
{
|
||||||
|
@ -3992,6 +4035,33 @@ auto detabber(Range)(Range r, size_t tabSize = 8)
|
||||||
assert(detabber(" \n\tx", 9).array == " \n x");
|
assert(detabber(" \n\tx", 9).array == " \n x");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.array : array;
|
||||||
|
|
||||||
|
static struct TestStruct
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
alias s this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct TestStruct2
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
alias s this;
|
||||||
|
@disable this(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
string s = " \n\tx";
|
||||||
|
string cmp = " \n x";
|
||||||
|
auto t = TestStruct(s);
|
||||||
|
assert(detabber(t, 9).array == cmp);
|
||||||
|
assert(detabber(TestStruct(s), 9).array == cmp);
|
||||||
|
assert(detabber(TestStruct(s), 9).array == detab(TestStruct(s), 9));
|
||||||
|
assert(detabber(TestStruct2(s), 9).array == detab(TestStruct2(s), 9));
|
||||||
|
assert(detabber(TestStruct2(s), 9).array == cmp);
|
||||||
|
}
|
||||||
|
|
||||||
@trusted pure unittest
|
@trusted pure unittest
|
||||||
{
|
{
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue