Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCleary committed Jun 23, 2023
1 parent e8bbec0 commit 68b5408
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Nito.Comparers.Core/Internals/IStringSplitter.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Nito.Comparers.Internals
{
/// <summary>
///
/// </summary>
public interface IStringSplitter
{
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="offset"></param>
/// <param name="length"></param>
/// <param name="isNumeric"></param>
/// <returns></returns>
void MoveNext(string source, ref int offset, out int length, out bool isNumeric);
}
}
12 changes: 12 additions & 0 deletions src/Nito.Comparers.Core/Internals/ISubstringComparer.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Nito.Comparers.Internals
{
public interface ISubstringComparer
{
int GetHashCode(string source, int offset, int length);
int Compare(string stringA, int offsetA, int lengthA, string stringB, int offsetB, int lengthB);
}
}
64 changes: 64 additions & 0 deletions src/Nito.Comparers.Core/Internals/NaturalStringComparison.cs
Expand Up @@ -180,6 +180,32 @@ public static int Compare(string x, string y, Func<string, int, int, string, int
};
}

private sealed class DefaultSubstringComparer : ISubstringComparer
{
private readonly Func<CultureInfo> _getCultureInfo;
private readonly CompareOptions _compareOptions;

public DefaultSubstringComparer(StringComparison comparison)
{
_getCultureInfo = comparison switch
{
StringComparison.CurrentCulture => () => CultureInfo.CurrentCulture,
StringComparison.CurrentCultureIgnoreCase => () => CultureInfo.CurrentCulture,
_ => () => CultureInfo.InvariantCulture,
};
}

public int Compare(string stringA, int inclusiveStartA, int exclusiveEndA, string stringB, int inclusiveStartB, int exclusiveEndB)
{
throw new NotImplementedException();
}

public int GetHashCode(string source, int inclusiveStart, int exclusiveEnd)
{
throw new NotImplementedException();
}
}

private ref struct SegmentParser
{
public SegmentParser(string source)
Expand Down Expand Up @@ -233,5 +259,43 @@ public void ParseNext()
static bool IsDigit(char ch) => ch >= '0' && ch <= '9';
}
}

private sealed class DefaultSplitter : IStringSplitter
{
public void MoveNext(string source, ref int offset, out int length, out bool isNumeric)
{
// Prerequisite: start < source.Length

var index = offset;
isNumeric = IsDigit(source[index++]);
if (isNumeric)
{
// Skip leading zeros, but keep one if that's the only digit.
if (source[offset] == '0')
{
do
{
++offset;
} while (offset < source.Length && source[offset] == '0');
if (offset == source.Length || !IsDigit(source[offset]))
--offset;
index = offset + 1;
}

while (index < source.Length && IsDigit(source[index]))
++index;
length = index - offset;
}
else
{
index = source.IndexOfAny(Digits, index);
if (index == -1)
index = source.Length;
length = index - offset;
}

static bool IsDigit(char ch) => ch >= '0' && ch <= '9';
}
}
}
}

0 comments on commit 68b5408

Please sign in to comment.