Skip to content

Commit

Permalink
Merge pull request #2237 from monarchdodra/outdent
Browse files Browse the repository at this point in the history
Upkeep in std.string.outdent
  • Loading branch information
jmdavis committed Jun 10, 2014
2 parents 240a11e + 9680b8a commit 04d4617
Showing 1 changed file with 29 additions and 59 deletions.
88 changes: 29 additions & 59 deletions std/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -4149,29 +4149,7 @@ unittest
* the input from being outdented.
*
* Works at compile-time.
*
* Example:
* ---
* writeln(q{
* import std.stdio;
* void main() {
* writeln("Hello");
* }
* }.outdent());
* ---
*
* Output:
* ---
*
* import std.stdio;
* void main() {
* writeln("Hello");
* }
*
* ---
*
*/

S outdent(S)(S str) @safe pure if(isSomeString!S)
{
return str.splitLines(KeepTerminator.yes).outdent().join();
Expand All @@ -4187,17 +4165,17 @@ S[] outdent(S)(S[] lines) @safe pure if(isSomeString!S)

static S leadingWhiteOf(S str)
{
return str[ 0 .. $-find!(not!(std.uni.isWhite))(str).length ];
return str[ 0 .. $ - stripLeft(str).length ];
}

S shortestIndent;
foreach (i, line; lines)
foreach (ref line; lines)
{
auto stripped = __ctfe? line.ctfe_strip() : line.strip();
auto stripped = line.stripLeft();

if (stripped.empty)
{
lines[i] = line[line.chomp().length..$];
line = line[line.chomp().length .. $];
}
else
{
Expand All @@ -4214,53 +4192,45 @@ S[] outdent(S)(S[] lines) @safe pure if(isSomeString!S)
}
}

foreach (i; 0..lines.length)
foreach (ref line; lines)
{
auto stripped = __ctfe? lines[i].ctfe_strip() : lines[i].strip();
auto stripped = line.stripLeft();

if (stripped.empty)
{
// Do nothing
}
else if (lines[i].startsWith(shortestIndent))
else if (line.startsWith(shortestIndent))
{
lines[i] = lines[i][shortestIndent.length..$];
line = line[shortestIndent.length .. $];
}
else
{
if (__ctfe)
assert(false, "outdent: Inconsistent indentation");
else
throw new StringException("outdent: Inconsistent indentation");
throw new StringException("outdent: Inconsistent indentation");
}
}

return lines;
}

// TODO: Remove this and use std.string.strip when retro() becomes ctfe-able.
private S ctfe_strip(S)(S str) if(isSomeString!(Unqual!S))
{
return str.stripLeft().ctfe_stripRight();
}

// TODO: Remove this and use std.string.strip when retro() becomes ctfe-able.
private S ctfe_stripRight(S)(S str) if(isSomeString!(Unqual!S))
///
unittest
{
size_t endIndex = 0;
size_t prevIndex = str.length;
enum pretty = q{
import std.stdio;
void main() {
writeln("Hello");
}
}.outdent();

foreach_reverse (i, dchar ch; str)
{
if (!std.uni.isWhite(ch))
{
endIndex = prevIndex;
break;
}
prevIndex = i;
}
enum ugly = q{
import std.stdio;
void main() {
writeln("Hello");
}
};

return str[0..endIndex];
assert(pretty == ugly);
}

unittest
Expand Down Expand Up @@ -4293,11 +4263,6 @@ unittest

assertCTFEable!(
{
static assert(ctfe_strip(" \tHi \r\n") == "Hi");
static assert(ctfe_strip(" \tHi©\u2028 \r\n") == "Hi©");
static assert(ctfe_strip("Hi") == "Hi");
static assert(ctfe_strip(" \t \r\n") == "");
static assert(ctfe_strip("") == "");

foreach (S; TypeTuple!(string, wstring, dstring))
{
Expand Down Expand Up @@ -4351,6 +4316,11 @@ unittest
enum expected6 = "\r\n\r\n\u2028\u2029";
assert(testStr6.outdent() == expected6);
static assert(testStr6.outdent() == expected6);

enum testStr7 = " a \n b ";
enum expected7 = "a \nb ";
assert(testStr7.outdent() == expected7);
static assert(testStr7.outdent() == expected7);
}
});
}

0 comments on commit 04d4617

Please sign in to comment.