Skip to content

Commit

Permalink
Fix null warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
FlatlinerDOA committed Mar 30, 2024
1 parent 8106ca1 commit ac7f678
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/Rope/Rope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Rope;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// A rope is an immutable sequence built using a b-tree style data structure that is useful for efficiently applying and storing edits, most commonly to text, but any list or sequence can be edited.
Expand Down Expand Up @@ -100,11 +101,13 @@ public Rope(Rope<T> left, Rope<T> right)

public T this[int index] => this.ElementAt(index);

public Rope<T> Left => this.left;
public Rope<T>? Left => this.left;

public Rope<T> Right => this.right;
public Rope<T>? Right => this.right;

public bool IsNode => this.left != null;
[MemberNotNullWhen(true, nameof(this.left))]
[MemberNotNullWhen(true, nameof(this.right))]
public bool IsNode => this.left != null && this.right != null;

public int Weight => this.left?.Length ?? this.data.Length;

Expand All @@ -119,12 +122,12 @@ public T ElementAt(int index)
return this.data.Slice(index).Span[0];
}

if (this.Weight <= index && this.right.Length != 0)
if (this.Weight <= index && this.right is not null && this.right.Length != 0)
{
return this.right.ElementAt(index - this.Weight);
}

if (this.left.Length != 0)
if (this.left is not null && this.left.Length != 0)
{
return this.left.ElementAt(index);
}
Expand Down Expand Up @@ -188,13 +191,13 @@ public IEnumerable<Rope<T>> Split(ReadOnlyMemory<T> separator)
}
else if (i <= this.Weight)
{
var (a, b) = this.left.SplitAt(i);
return (a, new Rope<T>(b, this.right));
var (a, b) = (this.left ?? Rope<T>.Empty).SplitAt(i);
return (a, new Rope<T>(b, this.right ?? Rope<T>.Empty));
}
else
{
var (a, b) = this.right.SplitAt(i - this.Weight);
return (new Rope<T>(this.left, a), b);
var (a, b) = (this.right ?? Rope<T>.Empty).SplitAt(i - this.Weight);
return (new Rope<T>(this.left ?? Rope<T>.Empty, a), b);
}
}

Expand Down Expand Up @@ -334,7 +337,7 @@ public int CommonSuffixLength(Rope<char> other)

public T[] ToArray()
{
if (this.left == null)
if (this.left is null || this.right is null)
{
return this.data.ToArray();
}
Expand All @@ -347,7 +350,7 @@ public T[] ToArray()

public void CopyTo(Memory<T> other)
{
if (this.left == null)
if (this.left is null || this.right is null)
{
// Leaf node so copy memory.
this.data.CopyTo(other);
Expand Down Expand Up @@ -378,12 +381,7 @@ public int IndexOf(Rope<T> find, int offset)

public int IndexOf(ReadOnlyMemory<T> find)
{
if (this.left == null)
{
// Leaf
return this.data.Span.IndexOf(find.Span);
}
else
if (this.IsNode)
{
// Node
var i = this.left.IndexOf(find);
Expand All @@ -398,18 +396,18 @@ public int IndexOf(ReadOnlyMemory<T> find)
return this.left.Length + i;
}
}
else
{
// Leaf
return this.data.Span.IndexOf(find.Span);
}

return -1;
}

public int IndexOf(T find)
{
if (this.left == null)
{
// Leaf
return this.data.Span.IndexOf(find);
}
else
if (this.IsNode)
{
// Node
var i = this.left.IndexOf(find);
Expand All @@ -424,6 +422,11 @@ public int IndexOf(T find)
return this.left.Length + i;
}
}
else
{
// Leaf
return this.data.Span.IndexOf(find);
}

return -1;
}
Expand Down Expand Up @@ -609,7 +612,7 @@ IEnumerator IEnumerable.GetEnumerator()

private struct Enumerator : IEnumerator<T>
{
private Rope<T> rope;
private readonly Rope<T> rope;
private int index;
public Enumerator(Rope<T> rope)
{
Expand All @@ -623,13 +626,12 @@ public Enumerator(Rope<T> rope)

public void Dispose()
{
this.index = -1;
this.rope = null;
this.index = -2;
}

public bool MoveNext()
{
if (this.rope == null)
if (this.index == -2)
{
throw new ObjectDisposedException(nameof(Enumerator));
}
Expand Down

0 comments on commit ac7f678

Please sign in to comment.