Commit dc8149fc authored by Jon Skeet's avatar Jon Skeet

Optimize FromBase64String to return Empty when presented with an empty string.

This fixes issue 61.
parent f86edcbb
......@@ -34,6 +34,7 @@
#endregion
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
......@@ -127,5 +128,21 @@ namespace Google.ProtocolBuffers
ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
}
[TestMethod]
public void FromBase64_WithText()
{
byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
string base64 = Convert.ToBase64String(data);
ByteString bs = ByteString.FromBase64(base64);
TestUtil.AssertBytesEqual(data, bs.ToByteArray());
}
[TestMethod]
public void FromBase64_Empty()
{
// Optimization which also fixes issue 61.
Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
}
}
}
\ No newline at end of file
......@@ -129,7 +129,9 @@ namespace Google.ProtocolBuffers
/// </summary>
public static ByteString FromBase64(string bytes)
{
return new ByteString(Convert.FromBase64String(bytes));
// By handling the empty string explicitly, we not only optimize but we fix a
// problem on CF 2.0. See issue 61 for details.
return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes));
}
/// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment