Skip to content

Commit

Permalink
Merge pull request #5061 from BBasile/issue-9615
Browse files Browse the repository at this point in the history
fix issue 9615, std.conv.parse!(T[]) should allow a trailing comma
  • Loading branch information
JackStouffer committed Jan 26, 2017
2 parents 8506e11 + 307675a commit 781ea57
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion std/conv.d
Expand Up @@ -3386,7 +3386,7 @@ package void skipWS(R)(ref R r)
/**
* Parses an array from a string given the left bracket (default $(D
* '[')), right bracket (default $(D ']')), and element separator (by
* default $(D ',')).
* default $(D ',')). A trailing separator is allowed.
*/
Target parse(Target, Source)(ref Source s, dchar lbracket = '[', dchar rbracket = ']', dchar comma = ',')
if (isExactSomeString!Source &&
Expand All @@ -3405,6 +3405,8 @@ Target parse(Target, Source)(ref Source s, dchar lbracket = '[', dchar rbracket
}
for (;; s.popFront(), skipWS(s))
{
if (!s.empty && s.front == rbracket)
break;
result ~= parseElement!(ElementType!Target)(s);
skipWS(s);
if (s.empty)
Expand All @@ -3417,6 +3419,27 @@ Target parse(Target, Source)(ref Source s, dchar lbracket = '[', dchar rbracket
return result;
}

@safe unittest // Bugzilla 9615
{
string s0 = "[1,2, ]";
string s1 = "[1,2, \t\v\r\n]";
string s2 = "[1,2]";
assert(s0.parse!(int[]) == [1,2]);
assert(s1.parse!(int[]) == [1,2]);
assert(s2.parse!(int[]) == [1,2]);

string s3 = `["a","b",]`;
string s4 = `["a","b"]`;
assert(s3.parse!(string[]) == ["a","b"]);
assert(s4.parse!(string[]) == ["a","b"]);

import std.exception : assertThrown;
string s5 = "[,]";
string s6 = "[, \t,]";
assertThrown!ConvException(parse!(string[])(s5));
assertThrown!ConvException(parse!(int[])(s6));
}

@safe unittest
{
int[] a = [1, 2, 3, 4, 5];
Expand Down

0 comments on commit 781ea57

Please sign in to comment.