Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
fix unescapeTabs with escaped values
Browse files Browse the repository at this point in the history
previously a string like \\n would have emitted a backslash plus newline
this fixes ddoc containing these strings in auto completion
  • Loading branch information
WebFreak001 committed Jan 13, 2020
1 parent 6e089dd commit c52b600
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion source/workspaced/com/dcd.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import std.file : tempDir;

import core.thread;
import std.algorithm;
import std.array;
import std.ascii;
import std.conv;
import std.datetime;
Expand Down Expand Up @@ -731,7 +732,54 @@ unittest

private string unescapeTabs(string val)
{
return val.replace("\\t", "\t").replace("\\n", "\n").replace("\\\\", "\\");
if (!val.length)
return val;

auto ret = appender!string;
size_t i = 0;
while (i < val.length)
{
size_t index = val.indexOf('\\', i);
if (index == -1 || cast(int) index == cast(int) val.length - 1)
{
if (!ret.data.length)
{
return val;
}
else
{
ret.put(val[i .. $]);
break;
}
}
else
{
char c = val[index + 1];
switch (c)
{
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
default:
break;
}
ret.put(val[i .. index]);
ret.put(c);
i = index + 2;
}
}
return ret.data;
}

unittest
{
shouldEqual("hello world", "hello world".unescapeTabs);
shouldEqual("hello\nworld", "hello\\nworld".unescapeTabs);
shouldEqual("hello\\nworld", "hello\\\\nworld".unescapeTabs);
shouldEqual("hello\\\nworld", "hello\\\\\\nworld".unescapeTabs);
}

/// Returned by findDeclaration
Expand Down

0 comments on commit c52b600

Please sign in to comment.