Skip to content

Commit 12b8a07

Browse files
Code style cleanup: expression-bodied members and formatting
1 parent ba0c815 commit 12b8a07

23 files changed

Lines changed: 345 additions & 582 deletions

Benchmarks/AllocationBaselineBenchmark.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using BenchmarkDotNet.Diagnosers;
55
using BenchmarkDotNet.Jobs;
66
using BenchmarkDotNet.Order;
7-
using Open.Text;
87
using ZLinq;
98

109
namespace Open.Text.Benchmarks;
@@ -65,10 +64,7 @@ public int Small_ToArray()
6564
}
6665

6766
[Benchmark(Description = "Small - Count()")]
68-
public int Small_Count()
69-
{
70-
return SmallString.AsSegment().SplitAsSegments(',').Count();
71-
}
67+
public int Small_Count() => SmallString.AsSegment().SplitAsSegments(',').Count();
7268

7369
// ========== Medium String Tests ==========
7470

@@ -85,21 +81,17 @@ public int Medium_ForeachOnly()
8581

8682
[Benchmark(Description = "Medium - LINQ Where")]
8783
public int Medium_LinqWhere()
88-
{
89-
return MediumString.AsSegment()
84+
=> MediumString.AsSegment()
9085
.SplitAsSegments(',')
9186
.Where(s => s.Length > 5)
9287
.Count();
93-
}
9488

9589
[Benchmark(Description = "Medium - LINQ Select")]
9690
public int Medium_LinqSelect()
97-
{
98-
return MediumString.AsSegment()
91+
=> MediumString.AsSegment()
9992
.SplitAsSegments(',')
10093
.Select(s => s.Length)
10194
.Sum();
102-
}
10395

10496
// ========== Large String Tests ==========
10597

Benchmarks/SplitAsSegmentsQuickTest.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Diagnostics;
2-
using Open.Text;
31
using ZLinq;
42

53
namespace Open.Text.Benchmarks;
@@ -20,7 +18,7 @@ public static void Run()
2018
Console.WriteLine("Test 1: Simple foreach (only counting)");
2119
long gen0Before = GC.CollectionCount(0);
2220
long memBefore = GC.GetTotalMemory(forceFullCollection: true);
23-
21+
2422
int count = 0;
2523
for (int i = 0; i < 10000; i++)
2624
{
@@ -29,10 +27,10 @@ public static void Run()
2927
count += segment.Length;
3028
}
3129
}
32-
30+
3331
long memAfter = GC.GetTotalMemory(forceFullCollection: false);
3432
long gen0After = GC.CollectionCount(0);
35-
33+
3634
Console.WriteLine($" Result: {count}");
3735
Console.WriteLine($" Memory allocated: ~{(memAfter - memBefore) / 1024}KB");
3836
Console.WriteLine($" Gen0 collections: {gen0After - gen0Before}");
@@ -42,16 +40,16 @@ public static void Run()
4240
Console.WriteLine("Test 2: With LINQ Count()");
4341
gen0Before = GC.CollectionCount(0);
4442
memBefore = GC.GetTotalMemory(forceFullCollection: true);
45-
43+
4644
int totalCount = 0;
4745
for (int i = 0; i < 10000; i++)
4846
{
4947
totalCount += testString.AsSegment().SplitAsSegments(',').Count();
5048
}
51-
49+
5250
memAfter = GC.GetTotalMemory(forceFullCollection: false);
5351
gen0After = GC.CollectionCount(0);
54-
52+
5553
Console.WriteLine($" Result: {totalCount}");
5654
Console.WriteLine($" Memory allocated: ~{(memAfter - memBefore) / 1024}KB");
5755
Console.WriteLine($" Gen0 collections: {gen0After - gen0Before}");
@@ -61,17 +59,17 @@ public static void Run()
6159
Console.WriteLine("Test 3: With ToArray()");
6260
gen0Before = GC.CollectionCount(0);
6361
memBefore = GC.GetTotalMemory(forceFullCollection: true);
64-
62+
6563
int arrayCount = 0;
6664
for (int i = 0; i < 10000; i++)
6765
{
6866
var arr = testString.AsSegment().SplitAsSegments(',').ToArray();
6967
arrayCount += arr.Length;
7068
}
71-
69+
7270
memAfter = GC.GetTotalMemory(forceFullCollection: false);
7371
gen0After = GC.CollectionCount(0);
74-
72+
7573
Console.WriteLine($" Result: {arrayCount}");
7674
Console.WriteLine($" Memory allocated: ~{(memAfter - memBefore) / 1024}KB");
7775
Console.WriteLine($" Gen0 collections: {gen0After - gen0Before}");

Benchmarks/ZLinqImprovementsBenchmark.cs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using BenchmarkDotNet.Diagnosers;
55
using BenchmarkDotNet.Jobs;
66
using BenchmarkDotNet.Order;
7-
using Microsoft.Extensions.Primitives;
8-
using Open.Text;
97
using System.Text.RegularExpressions;
108
using ZLinq;
119

@@ -38,7 +36,7 @@ public Config()
3836
private const string SmallCsv = "apple,banana,cherry,date,elderberry";
3937
private const string MediumCsv = "apple,banana,cherry,date,elderberry,fig,grape,honeydew,kiwi,lemon,mango,nectarine,orange,papaya,quince";
4038
private static readonly string LargeCsv = string.Join(",", Enumerable.Range(1, 1000).Select(i => $"item{i}"));
41-
39+
4240
private static readonly Regex CommaRegex = new(",", RegexOptions.Compiled);
4341
private static readonly Regex WordRegex = new(@"\w+", RegexOptions.Compiled);
4442

@@ -70,15 +68,15 @@ public int CharSplit_Foreach_ZLinq()
7068

7169
[BenchmarkCategory("CharSplit-Count"), Benchmark(Baseline = true, Description = "BCL Split + LINQ Count")]
7270
public int CharSplit_Count_BCL()
73-
{
74-
return SmallCsv.Split(',').Count();
75-
}
71+
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
72+
#pragma warning disable RCS1077 // Optimize LINQ method call
73+
=> SmallCsv.Split(',').Count();
74+
#pragma warning restore RCS1077 // Optimize LINQ method call
75+
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
7676

7777
[BenchmarkCategory("CharSplit-Count"), Benchmark(Description = "SplitAsSegments + ZLinq Count")]
7878
public int CharSplit_Count_ZLinq()
79-
{
80-
return SmallCsv.SplitAsSegments(',').Count();
81-
}
79+
=> SmallCsv.SplitAsSegments(',').Count();
8280

8381
// =====================================================================
8482
// CATEGORY: Char Split - Large String Foreach
@@ -173,52 +171,37 @@ public int RegexMatch_ZLinq()
173171
// =====================================================================
174172

175173
[BenchmarkCategory("Replace"), Benchmark(Baseline = true, Description = "BCL String.Replace")]
176-
public string Replace_BCL()
177-
{
178-
return MediumCsv.Replace(",", " | ");
179-
}
174+
public string Replace_BCL() => MediumCsv.Replace(",", " | ");
180175

181176
[BenchmarkCategory("Replace"), Benchmark(Description = "ReplaceToString (ZLinq)")]
182-
public string Replace_ZLinq()
183-
{
184-
return MediumCsv.AsSegment().ReplaceToString(",", " | ");
185-
}
177+
public string Replace_ZLinq() => MediumCsv.AsSegment().ReplaceToString(",", " | ");
186178

187179
// =====================================================================
188180
// CATEGORY: LINQ Chain - Where + Select + Count
189181
// =====================================================================
190182

191183
[BenchmarkCategory("LinqChain"), Benchmark(Baseline = true, Description = "BCL + LINQ Chain")]
192184
public int LinqChain_BCL()
193-
{
194-
return SmallCsv.Split(',')
185+
=> SmallCsv.Split(',')
195186
.Where(s => s.Length > 4)
196-
.Select(s => s.Length)
197-
.Sum();
198-
}
187+
.Sum(s => s.Length);
199188

200189
[BenchmarkCategory("LinqChain"), Benchmark(Description = "ZLinq Chain")]
201190
public int LinqChain_ZLinq()
202-
{
203-
return SmallCsv.SplitAsSegments(',')
191+
=> SmallCsv.SplitAsSegments(',')
204192
.Where(s => s.Length > 4)
205193
.Select(s => s.Length)
206194
.Sum();
207-
}
208195

209196
// =====================================================================
210197
// CATEGORY: ToArray Materialization
211198
// =====================================================================
212199

213200
[BenchmarkCategory("ToArray"), Benchmark(Baseline = true, Description = "BCL Split (already array)")]
214201
public int ToArray_BCL()
215-
{
216-
return SmallCsv.Split(',').Length;
217-
}
202+
=> SmallCsv.Split(',').Length;
218203

219204
[BenchmarkCategory("ToArray"), Benchmark(Description = "SplitAsSegments.ToArray()")]
220205
public int ToArray_ZLinq()
221-
{
222-
return SmallCsv.SplitAsSegments(',').ToArray().Length;
223-
}
206+
=> SmallCsv.SplitAsSegments(',').ToArray().Length;
224207
}

Source/EnumValue.cs

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ internal static Entry[]?[] Lookup
234234
/// Indicates whether this instance matches the <typeparamref name="TEnum"/> value of <paramref name="other"/>.
235235
/// </summary>
236236
/// <returns>true if <paramref name="other"/>'s <typeparamref name="TEnum"/> value and this instance's <typeparamref name="TEnum"/> value are the same; otherwise false.</returns>
237-
public bool Equals(EnumValue<TEnum> other)
238-
=> Value.Equals(other.Value);
237+
public bool Equals(EnumValue<TEnum> other) => Value.Equals(other.Value);
239238

240239
/// <summary>
241240
/// Compares an <see cref="EnumValue{TEnum}"/> and <see cref="EnumValueCaseIgnored{TEnum}"/> for <typeparamref name="TEnum"/> equality.
@@ -250,8 +249,7 @@ public bool Equals(EnumValue<TEnum> other)
250249
=> !left.Value.Equals(right.Value);
251250

252251
/// <inheritdoc cref="Equals(EnumValue{TEnum})"/>
253-
public bool Equals(EnumValueCaseIgnored<TEnum> other)
254-
=> Value.Equals(other.Value);
252+
public bool Equals(EnumValueCaseIgnored<TEnum> other) => Value.Equals(other.Value);
255253

256254
/// <summary>
257255
/// Compares two <see cref="EnumValue{TEnum}"/> for <typeparamref name="TEnum"/> equality.
@@ -269,8 +267,7 @@ public bool Equals(EnumValueCaseIgnored<TEnum> other)
269267
/// Indicates whether this instance matches the provided enum <paramref name="other"/>.
270268
/// </summary>
271269
/// <returns>true if <paramref name="other"/> and this instance's enum value are the same; otherwise false.</returns>
272-
public bool Equals(TEnum? other)
273-
=> Value.Equals(other);
270+
public bool Equals(TEnum? other) => Value.Equals(other);
274271

275272
/// <summary>
276273
/// Compares an <see cref="EnumValue{TEnum}"/> and a <typeparamref name="TEnum"/> value forequality.
@@ -285,10 +282,9 @@ public bool Equals(TEnum? other)
285282
=> !left.Value.Equals(right);
286283

287284
/// <inheritdoc />
288-
public override bool Equals(object? obj)
289-
=> obj is TEnum e && Value.Equals(e)
290-
|| obj is EnumValue<TEnum> v1 && Value.Equals(v1.Value)
291-
|| obj is EnumValueCaseIgnored<TEnum> v2 && Value.Equals(v2.Value);
285+
public override bool Equals(object? obj) => obj is TEnum e && Value.Equals(e)
286+
|| obj is EnumValue<TEnum> v1 && Value.Equals(v1.Value)
287+
|| obj is EnumValueCaseIgnored<TEnum> v2 && Value.Equals(v2.Value);
292288

293289
/// <inheritdoc />
294290
public override int GetHashCode() => Value.GetHashCode();
@@ -342,15 +338,13 @@ static Underlying()
342338
/// </summary>
343339
[SuppressMessage("Roslynator", "RCS1158:Static member in generic type should use a type parameter.")]
344340
public static bool IsDefined<T>(T value)
345-
where T : notnull
346-
=> Underlying<T>.Map.ContainsKey(value);
341+
where T : notnull => Underlying<T>.Map.ContainsKey(value);
347342

348343
/// <summary>
349344
/// Returns the <typeparamref name="TEnum"/> from the <paramref name="value"/> provided if it maps directly to the underlying value.
350345
/// </summary>
351346
public static bool TryGetValue<T>(T value, out TEnum e)
352-
where T : notnull
353-
=> Underlying<T>.Map.TryGetValue(value, out e!);
347+
where T : notnull => Underlying<T>.Map.TryGetValue(value, out e!);
354348

355349
private string GetDebuggerDisplay()
356350
{
@@ -400,8 +394,7 @@ public readonly struct EnumValueCaseIgnored<TEnum>
400394
=> !left.Value.Equals(right.Value);
401395

402396
/// <inheritdoc cref="EnumValue{TEnum}.Equals(EnumValue{TEnum})"/>
403-
public bool Equals(EnumValueCaseIgnored<TEnum> other)
404-
=> Value.Equals(other.Value);
397+
public bool Equals(EnumValueCaseIgnored<TEnum> other) => Value.Equals(other.Value);
405398

406399
/// <summary>
407400
/// Compares two <see cref="EnumValueCaseIgnored{TEnum}"/> for <typeparamref name="TEnum"/> equality.
@@ -416,8 +409,7 @@ public bool Equals(EnumValueCaseIgnored<TEnum> other)
416409
=> !left.Value.Equals(right.Value);
417410

418411
/// <inheritdoc cref="EnumValue{TEnum}.Equals(TEnum)"/>
419-
public bool Equals(TEnum? other)
420-
=> Value.Equals(other);
412+
public bool Equals(TEnum? other) => Value.Equals(other);
421413

422414
/// <summary>
423415
/// Compares an <see cref="EnumValueCaseIgnored{TEnum}"/> and a <typeparamref name="TEnum"/> value for equality.
@@ -432,14 +424,12 @@ public bool Equals(TEnum? other)
432424
=> !left.Value.Equals(right);
433425

434426
/// <inheritdoc />
435-
public override bool Equals(object? obj)
436-
=> obj is TEnum e && Value.Equals(e)
437-
|| obj is EnumValueCaseIgnored<TEnum> v1 && Value.Equals(v1.Value)
438-
|| obj is EnumValue<TEnum> v2 && Value.Equals(v2.Value);
427+
public override bool Equals(object? obj) => obj is TEnum e && Value.Equals(e)
428+
|| obj is EnumValueCaseIgnored<TEnum> v1 && Value.Equals(v1.Value)
429+
|| obj is EnumValue<TEnum> v2 && Value.Equals(v2.Value);
439430

440431
/// <inheritdoc />
441-
public override int GetHashCode()
442-
=> Value.GetHashCode();
432+
public override int GetHashCode() => Value.GetHashCode();
443433

444434
/// <summary>
445435
/// Implicitly converts an <see cref="EnumValue{TEnum}"/> to an <see cref="EnumValueCaseIgnored{TEnum}"/>.
@@ -515,18 +505,16 @@ public static bool TryParse<TEnum>(string value, out TEnum e)
515505
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
516506
[MethodImpl(MethodImplOptions.AggressiveInlining)]
517507
public static TEnum Parse<TEnum>(StringSegment value)
518-
where TEnum : notnull, Enum
519-
=> TryParse<TEnum>(value, false, out TEnum? e) ? e
520-
: throw new ArgumentException(string.Format(NotFoundMessage, value), nameof(value));
508+
where TEnum : notnull, Enum => TryParse<TEnum>(value, false, out TEnum? e) ? e
509+
: throw new ArgumentException(string.Format(NotFoundMessage, value), nameof(value));
521510

522511
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
523512
[MethodImpl(MethodImplOptions.AggressiveInlining)]
524513
public static TEnum Parse<TEnum>(string value, bool ignoreCase)
525-
where TEnum : notnull, Enum
526-
=> ignoreCase
527-
? EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(value, out TEnum? e) ? e
528-
: throw new ArgumentException(string.Format(NotFoundMessage, value), nameof(value))
529-
: Parse<TEnum>(value);
514+
where TEnum : notnull, Enum => ignoreCase
515+
? EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(value, out TEnum? e) ? e
516+
: throw new ArgumentException(string.Format(NotFoundMessage, value), nameof(value))
517+
: Parse<TEnum>(value);
530518

531519
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
532520
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -543,23 +531,20 @@ public static TEnum Parse<TEnum>(StringSegment value, bool ignoreCase)
543531
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
544532
[MethodImpl(MethodImplOptions.AggressiveInlining)]
545533
public static bool TryParse<TEnum>(StringSegment value, out TEnum e)
546-
where TEnum : notnull, Enum
547-
=> TryParse(value, false, out e);
534+
where TEnum : notnull, Enum => TryParse(value, false, out e);
548535

549536
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
550537
[MethodImpl(MethodImplOptions.AggressiveInlining)]
551538
public static bool TryParse<TEnum>(string name, bool ignoreCase, out TEnum e)
552-
where TEnum : notnull, Enum
553-
=> ignoreCase
554-
? EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(name, out e!)
555-
: TryParse(name, out e);
539+
where TEnum : notnull, Enum => ignoreCase
540+
? EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(name, out e!)
541+
: TryParse(name, out e);
556542

557543
/// <inheritdoc cref="TryParse{TEnum}(StringSegment, bool, out TEnum)"/>
558544
/// <remarks>Can be slightly faster than other ignore-case methods.</remarks>
559545
[MethodImpl(MethodImplOptions.AggressiveInlining)]
560546
public static bool TryParseIgnoreCase<TEnum>(string name, out TEnum e)
561-
where TEnum : notnull, Enum
562-
=> EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(name, out e!);
547+
where TEnum : notnull, Enum => EnumValue<TEnum>.IgnoreCaseLookup.TryGetValue(name, out e!);
563548

564549
/// <summary>
565550
/// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
@@ -609,8 +594,7 @@ public static bool TryParse<TEnum>(StringSegment name, bool ignoreCase, out TEnu
609594
[MethodImpl(MethodImplOptions.AggressiveInlining)]
610595
public static bool TryGetValue<TEnum, T>(T value, out TEnum e)
611596
where TEnum : notnull, Enum
612-
where T : notnull
613-
=> EnumValue<TEnum>.TryGetValue(value, out e);
597+
where T : notnull => EnumValue<TEnum>.TryGetValue(value, out e);
614598

615599
/// <summary>
616600
/// Uses an expression tree to do an fast lookup the name of the enum value.
@@ -621,8 +605,7 @@ public static bool TryGetValue<TEnum, T>(T value, out TEnum e)
621605
/// <returns>The name of the enum.</returns>
622606
[MethodImpl(MethodImplOptions.AggressiveInlining)]
623607
public static string GetName<TEnum>(this TEnum value)
624-
where TEnum : notnull, Enum
625-
=> EnumValue<TEnum>.NameLookup(value);
608+
where TEnum : notnull, Enum => EnumValue<TEnum>.NameLookup(value);
626609

627610
/// <summary>
628611
/// Retrieves the attributes for a given enum value.

0 commit comments

Comments
 (0)