-
-
Notifications
You must be signed in to change notification settings - Fork 746
Added std.string.outdent (try #2) #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
std/string.d
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a problem if C is mutable. It'll work on Windows but not on Linux (since string literals are actually immutable on Linux; I'm not sure why they aren't on Windows).
|
I may be missing something, but this implementation is more complicated than it should. If I understand the intent correctly, it should be able to do its job in two passes:
There should be no need for e.g. an array of indents and other artifacts. What am I missing? |
|
I'd suspected the implementation is a fair amount more complicated than it should, and the way I read it it has issues with multibyte characters. So I wrote this: String[] outdent(String)(String[] lines) if (isSomeString!String)
{
size_t minLeadingSpaces = size_t.max;
foreach (line; lines)
{
size_t leadingSpaces = 0;
foreach (dchar c; line)
{
if (std.uni.isSpace(c))
{
++leadingSpaces;
}
else
{
if (leadingSpaces < minLeadingSpaces)
{
minLeadingSpaces = leadingSpaces;
}
break;
}
}
}
if (!minLeadingSpaces || minLeadingSpaces == size_t.max)
{
return lines;
}
foreach (ref line; lines)
{
line.popFrontN(minLeadingSpaces);
}
return lines;
}
String outdent(String)(String s) if (isSomeString!String)
{
auto lines = outdent(splitLines(s));
s = join(lines, "\n");
if (s.endsWith('\n')) s ~= '\n';
return s;
}You may want to adapt it for this submission. Thanks. |
|
I've cleaned up the (S str) version as per your suggestions. I've also eliminated the array of indents from the the (S[] lines) version. I don't think it can be significantly simplified further without introducing incorrect behavior upon inconsistent indentations and/or whitespace-only lines. I also cleaned up the unittests. I've looked carefully through, and I don't see anything that looks like a potential multibyte-char problem. Is there anything specific you see? |
|
If you intend to accept #278, then that should probably be done before this is accepted so I can make use of it in outdent without needing to do it in a followup pull request. |
…obos into outdent Conflicts: std/string.d
|
Ok, I'm done with the changes I was planning. Any further issues/comments? If not, should I make it all a single commit again? |
|
Looks good to me, so please do rebase so that it's one commit. |
|
Superceded by #282 |
Updated makefile to use dmd/generated/os/release/model/dmd
This puts everything from pull request #156 into one commit.