Skip to content

Commit

Permalink
Use ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
NN--- committed Jul 13, 2021
1 parent 1c63a5a commit 1e8380e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CodeJam.Blocks/Metadata/MetaTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AttributeInfo[] GetAttribute(Type type) =>
_attributes.Where(a => a.Name == type.FullName).Concat(
_attributes.Where(a => a.Name == type.Name).Concat(
type.Name.EndsWith("Attribute", StringComparison.Ordinal)
? _attributes.Where(a => a.Name == type.Name.Substring(0, type.Name.Length - "Attribute".Length))
? _attributes.Where(a => a.Name == type.Name[..^"Attribute".Length])
: Enumerable.Empty<AttributeInfo>())
).ToArray();
}
Expand Down
2 changes: 1 addition & 1 deletion CodeJam.Blocks/TableData/CsvFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static string EscapeValue(string value)
else
escaped.Append(chr);
else if (IsEscapeRequired(chr))
escaped = new StringBuilder(chr == '"' ? value.Substring(0, i) + "\"\"" : value.Substring(0, i + 1));
escaped = new StringBuilder(chr == '"' ? value[..i] + "\"\"" : value[..(i + 1)]);
}
return escaped?.ToString() ?? value;
}
Expand Down
2 changes: 1 addition & 1 deletion CodeJam.Blocks/TableData/FixedWidthFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void Print(
var width = widths[i];
writer.Write(
val.Length > width
? val.Substring(0, width)
? val[..width]
: val.PadRight(width));
}
}
Expand Down
8 changes: 4 additions & 4 deletions CodeJam.Main.Tests/Collections/SuffixTree/SuffixTreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public void Test17Contains()
st.Add(s);
for (var i = 0; i < s.Length; ++i)
{
var suffix = s.Substring(i);
var suffix = s[i..];
suffixes.Add(suffix);
if (suffix.Length != 1)
{
properSubstrings.Add(suffix.Substring(0, suffix.Length - 1));
properSubstrings.Add(suffix[..^1]);
}
}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ private static void TestStartingWith(string[] strings)
{
break;
}
var suffix = s.Substring(pos);
var suffix = s[pos..];
expectedSuffixes.Add(suffix);
expectedSources[suffix].Add(i);
++pos;
Expand Down Expand Up @@ -221,7 +221,7 @@ private static void VerifyAllSuffixes(string[] strings, IEnumerable<Suffix> resu
var s = strings[i];
for (var j = 0; j < s.Length; ++j)
{
var suffix = s.Substring(j);
var suffix = s[j..];
expectedSuffixes.Add(suffix);
expectedSources[suffix].Add(i);
}
Expand Down
22 changes: 10 additions & 12 deletions CodeJam.Main.Tests/Ranges/CompositeRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ private static CompositeRange<T> ParseCompositeRange<T>(
var boundaryAndRange = value.Split(new[] { CompositeRangeInternal.PrefixString }, 2, StringSplitOptions.None);
var boundary = ParseRange(boundaryAndRange[0], parseValueCallback);

var result = boundaryAndRange[1]
.Substring(0, boundaryAndRange[1].Length - CompositeRangeInternal.SuffixString.Length)
var result = boundaryAndRange[1][..^CompositeRangeInternal.SuffixString.Length]
.Split(new[] { CompositeRangeInternal.SeparatorString }, StringSplitOptions.None)
.Select(s => ParseRange(s, parseValueCallback))
.ToCompositeRange();
Expand All @@ -53,8 +52,7 @@ private static CompositeRange<T, TKey> ParseCompositeRange<T, TKey>(
var boundaryAndRange = value.Split(new[] { CompositeRangeInternal.PrefixString }, 2, StringSplitOptions.None);
var boundary = ParseRange(boundaryAndRange[0], parseValueCallback);

var result = boundaryAndRange[1]
.Substring(0, boundaryAndRange[1].Length - CompositeRangeInternal.SuffixString.Length)
var result = boundaryAndRange[1][..^CompositeRangeInternal.SuffixString.Length]
.Split(new[] { CompositeRangeInternal.SeparatorString }, StringSplitOptions.None)
.Select(s => ParseRange(s, parseValueCallback, parseKeyCallback))
.ToCompositeRange();
Expand Down Expand Up @@ -454,11 +452,11 @@ public static void TestCompositeRangeWithValues()
var compositeRange2 = compositeRange.WithValues(i => "A" + i);
AreEqual(compositeRange2.ToString(CultureInfo.InvariantCulture), "[A0..A3]: { '1':[A0..A2]; '2':[A1..A3] }");

var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i.Substring(1));
var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i[1..]);
AreEqual(compositeRange3.ToString(CultureInfo.InvariantCulture), "[A0..B3]: { '1':[A0..B2]; '2':[A1..B3] }");

var compositeRange4 =
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i.Substring(1), CultureInfo.InvariantCulture));
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i[1..], CultureInfo.InvariantCulture));
AreEqual(compositeRange4.ToString(CultureInfo.InvariantCulture), "[0..3]: { [0..2]; [1..3] }");

AreEqual(compositeRange4, compositeRange.WithoutKeys());
Expand Down Expand Up @@ -492,11 +490,11 @@ public static void TestCompositeRangeExclusiveWithValues()
var compositeRange2 = compositeRange.WithValues(i => "A" + i);
AreEqual(compositeRange2.ToString(CultureInfo.InvariantCulture), "(A0..A3): { '1':(A0..A2); '2':(A1..A3) }");

var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i.Substring(1));
var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i[1..]);
AreEqual(compositeRange3.ToString(CultureInfo.InvariantCulture), "(A0..B3): { '1':(A0..B2); '2':(A1..B3) }");

var compositeRange4 =
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i.Substring(1), CultureInfo.InvariantCulture));
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i[1..], CultureInfo.InvariantCulture));
AreEqual(compositeRange4.ToString(CultureInfo.InvariantCulture), "(0..3): { (0..2); (1..3) }");

AreEqual(compositeRange4, compositeRange.WithoutKeys());
Expand Down Expand Up @@ -530,11 +528,11 @@ public static void TestCompositeRangeExclusiveFromWithValues()
var compositeRange2 = compositeRange.WithValues(i => "A" + i);
AreEqual(compositeRange2.ToString(CultureInfo.InvariantCulture), "(A0..A3]: { '1':(A0..A2]; '2':(A1..A3] }");

var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i.Substring(1));
var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i[1..]);
AreEqual(compositeRange3.ToString(CultureInfo.InvariantCulture), "(A0..B3]: { '1':(A0..B2]; '2':(A1..B3] }");

var compositeRange4 =
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i.Substring(1), CultureInfo.InvariantCulture));
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i[1..], CultureInfo.InvariantCulture));
AreEqual(compositeRange4.ToString(CultureInfo.InvariantCulture), "(0..3]: { (0..2]; (1..3] }");

AreEqual(compositeRange4, compositeRange.WithoutKeys());
Expand Down Expand Up @@ -568,11 +566,11 @@ public static void TestCompositeRangeExclusiveToWithValues()
var compositeRange2 = compositeRange.WithValues(i => "A" + i);
AreEqual(compositeRange2.ToString(CultureInfo.InvariantCulture), "[A0..A3): { '1':[A0..A2); '2':[A1..A3) }");

var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i.Substring(1));
var compositeRange3 = compositeRange2.WithValues(i => i, i => "B" + i[1..]);
AreEqual(compositeRange3.ToString(CultureInfo.InvariantCulture), "[A0..B3): { '1':[A0..B2); '2':[A1..B3) }");

var compositeRange4 =
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i.Substring(1), CultureInfo.InvariantCulture));
compositeRange3.WithoutKeys().WithValues(i => int.Parse(i[1..], CultureInfo.InvariantCulture));
AreEqual(compositeRange4.ToString(CultureInfo.InvariantCulture), "[0..3): { [0..2); [1..3) }");

AreEqual(compositeRange4, compositeRange.WithoutKeys());
Expand Down
11 changes: 5 additions & 6 deletions CodeJam.Main.Tests/Ranges/RangeTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ private static RangeBoundaryFrom<T> ParseBoundaryFromCore<T>(string value, Func<
if (value.StartsWith(RangeInternal.FromInclusiveString, StringComparison.Ordinal))
{
kind = RangeBoundaryFromKind.Inclusive;
valuePart = value.Substring(RangeInternal.FromInclusiveString.Length);
valuePart = value[RangeInternal.FromInclusiveString.Length..];
}
else if (value.StartsWith(RangeInternal.FromExclusiveString, StringComparison.Ordinal))
{
kind = RangeBoundaryFromKind.Exclusive;
valuePart = value.Substring(RangeInternal.FromExclusiveString.Length);
valuePart = value[RangeInternal.FromExclusiveString.Length..];
}
else
{
Expand All @@ -47,12 +47,12 @@ private static RangeBoundaryTo<T> ParseBoundaryToCore<T>(
if (value.EndsWith(RangeInternal.ToInclusiveString, StringComparison.Ordinal))
{
kind = RangeBoundaryToKind.Inclusive;
valuePart = value.Substring(0, value.Length - RangeInternal.ToInclusiveString.Length);
valuePart = value[..^RangeInternal.ToInclusiveString.Length];
}
else if (value.EndsWith(RangeInternal.ToExclusiveString, StringComparison.Ordinal))
{
kind = RangeBoundaryToKind.Exclusive;
valuePart = value.Substring(0, value.Length - RangeInternal.ToExclusiveString.Length);
valuePart = value[..^RangeInternal.ToExclusiveString.Length];
}
else
{
Expand All @@ -78,8 +78,7 @@ public static Range<T, TKey> ParseRange<T, TKey>(
Func<string, T> parseValueCallback,
Func<string, TKey> parseKeyCallback)
{
var keyAndRange = value
.Substring(RangeInternal.KeyPrefixString.Length)
var keyAndRange = value[RangeInternal.KeyPrefixString.Length..]
.Split(new[] { RangeInternal.KeySeparatorString }, 2, StringSplitOptions.None);

if (keyAndRange[1] == RangeInternal.EmptyString)
Expand Down
2 changes: 1 addition & 1 deletion CodeJam.Main.Tests/Reflection/CreateInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static ParamInfo[] ParseParams(IEnumerable<string> paramStrs) =>
var parts = s.Split(':');
return
s.StartsWith("!", StringComparison.Ordinal)
? Param(parts[0].Substring(1), parts[1], true)
? Param(parts[0][1..], parts[1], true)
: Param(parts[0], parts[1], false);
})
.ToArray();
Expand Down
6 changes: 3 additions & 3 deletions CodeJam.Main/Strings/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static string Substring(this string str, StringOrigin origin, int length)
return
origin switch
{
StringOrigin.Begin => str.Substring(0, length),
StringOrigin.Begin => str[..length],
StringOrigin.End => str.Substring(strLen - length, length),
_ => throw CodeExceptions.Argument(nameof(origin), $"Invalid {nameof(StringOrigin)} value.")
};
Expand Down Expand Up @@ -175,7 +175,7 @@ public static string TrimPrefix(
return str;

var actPrefix = str.Prefix(prefixLen);
return !comparer.Equals(prefix, actPrefix) ? str : str.Substring(prefixLen);
return !comparer.Equals(prefix, actPrefix) ? str : str[prefixLen..];
}

/// <summary>
Expand Down Expand Up @@ -203,7 +203,7 @@ public static string TrimSuffix(this string str, string? suffix, IEqualityCompar
return str;

var actPrefix = str.Suffix(suffixLen);
return !comparer.Equals(suffix, actPrefix) ? str : str.Substring(0, strLen - suffixLen);
return !comparer.Equals(suffix, actPrefix) ? str : str[..(strLen - suffixLen)];
}

/// <summary>
Expand Down

0 comments on commit 1e8380e

Please sign in to comment.