mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:45:42 +00:00
Add Rune.Repeat
This commit is contained in:
parent
346fc72d5e
commit
154a5566a8
@ -33,6 +33,7 @@
|
||||
- Added `Random.NextUnitVector2()`
|
||||
- Added `Random.NextUnitVector3()`
|
||||
- Added `Random.NextRotation()`
|
||||
- Added `Rune.Repeat(int)`
|
||||
- Added `bool.GetBytes()`
|
||||
- Added `byte.GetBytes()`
|
||||
- Added `byte.IsEven()`
|
||||
|
39
X10D.Tests/src/Text/RuneTests.cs
Normal file
39
X10D.Tests/src/Text/RuneTests.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using X10D.Text;
|
||||
|
||||
namespace X10D.Tests.Text;
|
||||
|
||||
[TestClass]
|
||||
public class RuneTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void RepeatShouldBeCorrect()
|
||||
{
|
||||
const string expected = "aaaaaaaaaa";
|
||||
var rune = new Rune('a');
|
||||
string actual = rune.Repeat(10);
|
||||
|
||||
Assert.AreEqual(expected, actual);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RepeatOneCountShouldBeLength1String()
|
||||
{
|
||||
string repeated = new Rune('a').Repeat(1);
|
||||
Assert.AreEqual(1, repeated.Length);
|
||||
Assert.AreEqual("a", repeated);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RepeatZeroCountShouldBeEmpty()
|
||||
{
|
||||
Assert.AreEqual(string.Empty, new Rune('a').Repeat(0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RepeatNegativeCountShouldThrow()
|
||||
{
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Rune('a').Repeat(-1));
|
||||
}
|
||||
}
|
47
X10D/src/Text/RuneExtensions.cs
Normal file
47
X10D/src/Text/RuneExtensions.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System.Text;
|
||||
|
||||
namespace X10D.Text;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="Rune" />.
|
||||
/// </summary>
|
||||
public static class RuneExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a string composed of the current rune repeated a specified number of times.
|
||||
/// </summary>
|
||||
/// <param name="value">The rune to repeat.</param>
|
||||
/// <param name="count">The number of times to repeat.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="string" /> composed of <paramref name="value" /> repeated <paramref name="count" /> times.
|
||||
/// </returns>
|
||||
public static string Repeat(this Rune value, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count), ExceptionMessages.CountMustBeGreaterThanOrEqualTo0);
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (count == 1)
|
||||
{
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
int utf8SequenceLength = value.Utf8SequenceLength;
|
||||
Span<byte> utf8 = stackalloc byte[utf8SequenceLength];
|
||||
value.EncodeToUtf8(utf8);
|
||||
|
||||
Span<byte> buffer = stackalloc byte[utf8.Length * count];
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
utf8.CopyTo(buffer.Slice(index * utf8.Length, utf8.Length));
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user