Skip to content

Commit

Permalink
Custom string split methods now take the array pool directly
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed Aug 14, 2023
1 parent b99b13a commit deaf784
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions FMScanner/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ private bool StringToDate(string dateString, bool checkForAmbiguity, [NotNullWhe
return true;
}

string[] nums = dateString.Split_Char(CA_DateSeparators, StringSplitOptions.RemoveEmptyEntries, _sevenZipContext);
string[] nums = dateString.Split_Char(CA_DateSeparators, StringSplitOptions.RemoveEmptyEntries, _sevenZipContext.IntArrayPool);
if (nums.Length == 3)
{
bool unambiguousYearFound = false;
Expand Down Expand Up @@ -1899,7 +1899,7 @@ private void SetMiscTag(ScannedFMData fmData, string tag)
{
if (fmData.TagsString.IsWhiteSpace()) fmData.TagsString = "";

List<string> list = fmData.TagsString.Split_Char(CA_CommaSemicolon, StringSplitOptions.None, _sevenZipContext).ToList();
List<string> list = fmData.TagsString.Split_Char(CA_CommaSemicolon, StringSplitOptions.None, _sevenZipContext.IntArrayPool).ToList();
bool tagFound = false;
for (int i = 0; i < list.Count; i++)
{
Expand Down Expand Up @@ -2685,7 +2685,7 @@ static bool BaseDirScriptFileExtensions(List<NameAndIndex> baseDirFiles, string[

if ((_scanOptions.ScanTags || _scanOptions.ScanAuthor) && !fmIni.Tags.IsEmpty())
{
string[] tagsArray = fmIni.Tags.Split_Char(CA_CommaSemicolon, StringSplitOptions.RemoveEmptyEntries, _sevenZipContext);
string[] tagsArray = fmIni.Tags.Split_Char(CA_CommaSemicolon, StringSplitOptions.RemoveEmptyEntries, _sevenZipContext.IntArrayPool);

string authorString = "";
for (int i = 0, authorsFound = 0; i < tagsArray.Length; i++)
Expand Down Expand Up @@ -2959,7 +2959,7 @@ extremely large (due to often containing images), so I'm erring on the side of c
if (success)
{
last.Text = text;
last.Lines.ClearAndAdd(text.Split_String(CRLF_CR_LF, StringSplitOptions.None, _sevenZipContext));
last.Lines.ClearAndAdd(text.Split_String(CRLF_CR_LF, StringSplitOptions.None, _sevenZipContext.IntArrayPool));
}
}
else
Expand All @@ -2975,7 +2975,7 @@ extremely large (due to often containing images), so I'm erring on the side of c
last.Text = last.IsGlml
? Utility.GLMLToPlainText(ReadAllText(readmeStream, Encoding.UTF8), Utf32CharBuffer)
: ReadAllTextE(readmeStream);
last.Lines.ClearAndAdd(last.Text.Split_String(CRLF_CR_LF, StringSplitOptions.None, _sevenZipContext));
last.Lines.ClearAndAdd(last.Text.Split_String(CRLF_CR_LF, StringSplitOptions.None, _sevenZipContext.IntArrayPool));
}
}
finally
Expand Down Expand Up @@ -3671,7 +3671,7 @@ private string CleanupTitle(string value)
{
if (value.Contains(" "))
{
string[] titleWords = value.Split_String(SA_DoubleSpaces, StringSplitOptions.None, _sevenZipContext);
string[] titleWords = value.Split_String(SA_DoubleSpaces, StringSplitOptions.None, _sevenZipContext.IntArrayPool);
for (int i = 0; i < titleWords.Length; i++)
{
titleWords[i] = titleWords[i].Replace(" ", "");
Expand Down
20 changes: 10 additions & 10 deletions FMScanner/Core/StringSplit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using SharpCompress.Archives.SevenZip;
using System.Buffers;

namespace FMScanner;

Expand Down Expand Up @@ -30,22 +30,22 @@ public RentedArray()

// Framework 4.8 versions but with the ability to rent the arrays

internal static string[] Split_String(this string str, string[] separator, StringSplitOptions options, SevenZipContext context)
internal static string[] Split_String(this string str, string[] separator, StringSplitOptions options, ArrayPool<int> arrayPool)
{
bool omitEmptyEntries = options == StringSplitOptions.RemoveEmptyEntries;

if (separator.Length == 0)
{
return Split_Char(str, null, options, context);
return Split_Char(str, null, options, arrayPool);
}

if (omitEmptyEntries && str.Length == 0)
{
return Array.Empty<string>();
}

var sepListRA = new RentedArray<int>(context.IntArrayPool.Rent(str.Length), str.Length);
var lengthListRA = new RentedArray<int>(context.IntArrayPool.Rent(str.Length), str.Length);
var sepListRA = new RentedArray<int>(arrayPool.Rent(str.Length), str.Length);
var lengthListRA = new RentedArray<int>(arrayPool.Rent(str.Length), str.Length);
try
{
int numReplaces = MakeSeparatorList(str, separator, ref sepListRA, ref lengthListRA);
Expand All @@ -69,12 +69,12 @@ internal static string[] Split_String(this string str, string[] separator, Strin
}
finally
{
context.IntArrayPool.Return(sepListRA.Array);
context.IntArrayPool.Return(lengthListRA.Array);
arrayPool.Return(sepListRA.Array);
arrayPool.Return(lengthListRA.Array);
}
}

internal static string[] Split_Char(this string str, char[]? separator, StringSplitOptions options, SevenZipContext context)
internal static string[] Split_Char(this string str, char[]? separator, StringSplitOptions options, ArrayPool<int> arrayPool)
{
bool omitEmptyEntries = options == StringSplitOptions.RemoveEmptyEntries;

Expand All @@ -83,7 +83,7 @@ internal static string[] Split_Char(this string str, char[]? separator, StringSp
return Array.Empty<string>();
}

var sepListRA = new RentedArray<int>(context.IntArrayPool.Rent(str.Length), str.Length);
var sepListRA = new RentedArray<int>(arrayPool.Rent(str.Length), str.Length);
try
{
int numReplaces = MakeSeparatorList(str, separator, ref sepListRA);
Expand All @@ -107,7 +107,7 @@ internal static string[] Split_Char(this string str, char[]? separator, StringSp
}
finally
{
context.IntArrayPool.Return(sepListRA.Array);
arrayPool.Return(sepListRA.Array);
}
}

Expand Down

0 comments on commit deaf784

Please sign in to comment.