forked from kc3hack/2024_H
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
185 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace KoeBook.Core.Utilities; | ||
|
||
public static class EnumerableEx | ||
{ | ||
public static IEnumerable<(TSource value, bool isFirst, bool isLast)> WithPosition<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
using var enumerator = source.GetEnumerator(); | ||
|
||
var hasNext = enumerator.MoveNext(); | ||
if (!hasNext) | ||
yield break; | ||
var current = enumerator.Current; | ||
hasNext = enumerator.MoveNext(); | ||
yield return (current, true, !hasNext); | ||
|
||
while (hasNext) | ||
{ | ||
current = enumerator.Current; | ||
hasNext = enumerator.MoveNext(); | ||
yield return (current, false, !hasNext); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Text; | ||
|
||
namespace KoeBook.Core.Utilities; | ||
|
||
public class SplittedLineBuilder | ||
{ | ||
private readonly StringBuilder _stringBuilder = new(); | ||
private readonly List<string> _texts = []; | ||
|
||
public void Append(string text) | ||
{ | ||
_stringBuilder.Append(text); | ||
} | ||
|
||
/// <summary> | ||
/// すでに分割済みの行を追加します | ||
/// </summary> | ||
/// <param name="lines"></param> | ||
public void Append(IEnumerable<string> lines) | ||
{ | ||
foreach (var (value, isFirst, isLast) in lines.WithPosition()) | ||
{ | ||
if (isLast) | ||
{ | ||
_stringBuilder.Append(value); | ||
|
||
} | ||
else if (isFirst) | ||
{ | ||
_stringBuilder.Append(value); | ||
_texts.Add(_stringBuilder.ToString()); | ||
_stringBuilder.Clear(); | ||
} | ||
else | ||
{ | ||
_texts.Add(value); | ||
} | ||
} | ||
} | ||
|
||
public string[] ToLinesAndClear() | ||
{ | ||
var result = _stringBuilder.Length == 0 ? _texts.ToArray() : [.. _texts, _stringBuilder.ToString()]; | ||
|
||
_stringBuilder.Clear(); | ||
_texts.Clear(); | ||
return result; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using KoeBook.Core.Utilities; | ||
|
||
namespace KoeBook.Test.Utilities; | ||
|
||
public class EnumerableExTest | ||
{ | ||
[Theory] | ||
[InlineData(new[] { 0 })] | ||
[InlineData(new[] { 0, 1 })] | ||
[InlineData(new[] { 0, 1, 2 })] | ||
[InlineData(new[] { 0, 1, 2, 3 })] | ||
public void WithPosition(int[] input) | ||
{ | ||
var result = input.WithPosition(); | ||
|
||
Assert.Equal(input, result.Select(v => v.value)); | ||
Assert.True(result.First().isFirst); | ||
Assert.All(result.Skip(1), (v) => Assert.False(v.isFirst)); | ||
Assert.True(result.Last().isLast); | ||
Assert.All(result.SkipLast(1), (v) => Assert.False(v.isLast)); | ||
} | ||
|
||
[Fact] | ||
public void WithPosition_Empty() | ||
{ | ||
var result = Array.Empty<int>().WithPosition(); | ||
|
||
Assert.Empty(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using KoeBook.Core.Utilities; | ||
|
||
namespace KoeBook.Test.Utilities | ||
{ | ||
public class SplittedLineBuilderTest | ||
{ | ||
[Fact] | ||
public void Append_Single() | ||
{ | ||
var builder = new SplittedLineBuilder(); | ||
|
||
builder.Append("test"); | ||
|
||
Assert.Equal("test", builder._stringBuilder().ToString()); | ||
Assert.Empty(builder._texts()); | ||
|
||
builder._texts().Add("test1"); | ||
|
||
builder.Append("test2"); | ||
|
||
Assert.Equal("testtest2", builder._stringBuilder().ToString()); | ||
Assert.Equal(["test1"], builder._texts()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new string[] { }, "builder", new string[] { })] | ||
[InlineData(new[] { "1" }, "builder1", new string[] { })] | ||
[InlineData(new[] { "1", "2" }, "2", new[] { "builder1" })] | ||
[InlineData(new[] { "1", "2", "3" }, "3", new[] { "builder1", "2" })] | ||
public void Append_Multiple(string[] input, string expectedBuilder, string[] expectedTexts) | ||
{ | ||
var builder = new SplittedLineBuilder(); | ||
builder._stringBuilder().Append("builder"); | ||
|
||
builder.Append(input); | ||
|
||
Assert.Equal(expectedBuilder, builder._stringBuilder().ToString()); | ||
Assert.Equal(expectedTexts, builder._texts()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("", new string[] { }, new string[] { })] | ||
[InlineData("", new[] { "test1" }, new[] { "test1" })] | ||
[InlineData("", new[] { "test1", "test2" }, new[] { "test1", "test2" })] | ||
[InlineData("", new[] { "test1", "test2", "test3" }, new[] { "test1", "test2", "test3" })] | ||
[InlineData("test0", new string[] { }, new[] { "test0" })] | ||
[InlineData("test0", new[] { "test1" }, new[] { "test1", "test0" })] | ||
[InlineData("test0", new[] { "test1", "test2" }, new[] { "test1", "test2", "test0" })] | ||
[InlineData("test0", new[] { "test1", "test2", "test3" }, new[] { "test1", "test2", "test3", "test0" })] | ||
public void ToLinesAndClear(string builderStr, string[] texts, string[] expected) | ||
{ | ||
var builder = new SplittedLineBuilder(); | ||
builder._stringBuilder().Append(builderStr); | ||
builder._texts().AddRange(texts); | ||
|
||
var result = builder.ToLinesAndClear(); | ||
|
||
// 空になったかの判定はenumerateする前に行う | ||
Assert.Empty(builder._texts()); | ||
Assert.Equal(0, builder._stringBuilder().Length); | ||
Assert.Equal(expected, result); | ||
} | ||
} | ||
} | ||
|
||
|
||
file static class Proxy | ||
{ | ||
[UnsafeAccessor(UnsafeAccessorKind.Field)] | ||
public static extern ref StringBuilder _stringBuilder(this SplittedLineBuilder builder); | ||
|
||
[UnsafeAccessor(UnsafeAccessorKind.Field)] | ||
public static extern ref List<string> _texts(this SplittedLineBuilder builder); | ||
} |