Commit 8b2a5cee authored by Marc Gravell's avatar Marc Gravell

Merge pull request #142 from kevin-montrose/prepend-append

expose Prepend and Append methods on RedisKey, so we can avoid some string concats in common cases
parents d994b7ad 5bd748a1
......@@ -66,5 +66,40 @@ public void Zeros()
}
}
[Test]
public void PrependAppend()
{
{
// simple
RedisKey key = "world";
var ret = key.Prepend("hello");
Assert.AreEqual("helloworld", (string)ret);
}
{
RedisKey key1 = "world";
RedisKey key2 = Encoding.UTF8.GetBytes("hello");
var key3 = key1.Prepend(key2);
Assert.IsTrue(object.ReferenceEquals(key1.KeyValue, key3.KeyValue));
Assert.IsTrue(object.ReferenceEquals(key2.KeyValue, key3.KeyPrefix));
Assert.AreEqual("helloworld", (string)key3);
}
{
RedisKey key = "hello";
var ret = key.Append("world");
Assert.AreEqual("helloworld", (string)ret);
}
{
RedisKey key1 = Encoding.UTF8.GetBytes("hello");
RedisKey key2 = "world";
var key3 = key1.Append(key2);
Assert.IsTrue(object.ReferenceEquals(key2.KeyValue, key3.KeyValue));
Assert.IsTrue(object.ReferenceEquals(key1.KeyValue, key3.KeyPrefix));
Assert.AreEqual("helloworld", (string)key3);
}
}
}
}
......@@ -295,5 +295,27 @@ internal static byte[] ConcatenateBytes(byte[] a, object b, byte[] c)
if (cLen != 0) Buffer.BlockCopy(c, 0, result, aLen + bLen, cLen);
return result;
}
/// <summary>
/// Prepends p to this RedisKey, returning a new RedisKey.
///
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// it less possible.
/// </summary>
public RedisKey Prepend(RedisKey p)
{
return WithPrefix(p, this);
}
/// <summary>
/// Appends p to this RedisKey, returning a new RedisKey.
///
/// Avoids some allocations if possible, repeated Prepend/Appends make
/// it less possible.
/// </summary>
public RedisKey Append(RedisKey p)
{
return WithPrefix(this, p);
}
}
}
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