Skip to content

Commit

Permalink
Merge pull request #25 from creeperlv/develop
Browse files Browse the repository at this point in the history
RefString is now IEnumerable.
  • Loading branch information
creeperlv authored Nov 12, 2023
2 parents 5d323ca + f2ea44d commit 368d59b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LibCLCC.NET/Collections/ReactableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public ReactableList() { }
/// Clear the list.
/// </summary>
public new void Clear() {
base.Clear();
ClearReactChain.Invoke(this);
base.Clear();
}
/// <summary>
/// Add an item.
Expand Down
80 changes: 65 additions & 15 deletions LibCLCC.NET/Data/RefString.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -9,7 +10,7 @@ namespace LibCLCC.NET.Data
/// Reference String.
/// Use for reduce memory consumption.
/// </summary>
public struct RefString
public struct RefString : IEnumerable<char>, IEnumerable
{
/// <summary>
/// The NULL reference.
Expand All @@ -34,7 +35,7 @@ public struct RefString
/// <param name="offset"></param>
/// <param name="length"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public RefString(string string_value, int offset, int length)
public RefString(string string_value , int offset , int length)
{
Ref = string_value;
Offset = offset;
Expand All @@ -45,12 +46,12 @@ public RefString(string string_value, int offset, int length)
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public char this[int index]
public char this [ int index ]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Ref[index + Offset];
return Ref [ index + Offset ];
}
}

Expand All @@ -65,7 +66,7 @@ public char this[int index]
/// </summary>
/// <param name="s"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator RefString(string s) => new RefString { Ref = s, Offset = 0, Length = s.Length };
public static implicit operator RefString(string s) => new RefString { Ref = s , Offset = 0 , Length = s.Length };
/// <summary>
/// Convert a RefString to ReadOnlySpan
/// </summary>
Expand All @@ -77,7 +78,7 @@ public static implicit operator ReadOnlySpan<char>(RefString s)
{
throw new IndexOutOfRangeException();
}
return s.Ref.AsSpan(s.Offset,s.Length);
return s.Ref.AsSpan(s.Offset , s.Length);
}
/// <summary>
/// If the referred string is null.
Expand All @@ -100,7 +101,7 @@ public static implicit operator ReadOnlySpan<char>(RefString s)
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RefString operator +(RefString L, int R)
public static RefString operator +(RefString L , int R)
{
RefString result = L;
result.Offset += R;
Expand All @@ -116,7 +117,7 @@ public static implicit operator ReadOnlySpan<char>(RefString s)
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RefString operator -(RefString L, int R)
public static RefString operator -(RefString L , int R)
{
RefString result = L;
result.Offset -= R;
Expand All @@ -141,7 +142,7 @@ public readonly RefString Substring(int StartIndex)
/// <param name="SubStringLength"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly RefString Substring(int StartIndex, int SubStringLength)
public readonly RefString Substring(int StartIndex , int SubStringLength)
{
var r = this + StartIndex;
r.Length = SubStringLength;
Expand All @@ -156,7 +157,7 @@ public readonly RefString Substring(int StartIndex, int SubStringLength)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(char c)
{
return Ref.IndexOf(c, Offset);
return Ref.IndexOf(c , Offset , Length);
}
/// <summary>
/// Returns a zero-based index of the first appearance of a given string within this sturct.
Expand All @@ -167,7 +168,7 @@ public readonly int IndexOf(char c)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(string str)
{
return Ref.IndexOf(str, Offset);
return Ref.IndexOf(str , Offset , Length);
}
/// <summary>
/// Returns a zero-based index of the first appearance of a given char within this sturct.
Expand All @@ -178,9 +179,36 @@ public readonly int IndexOf(string str)
/// <param name="offset"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(char c, int offset)
public readonly int IndexOf(char c , int offset)
{
return Ref.IndexOf(c, Offset + offset);
return Ref.IndexOf(c , Offset + offset , Length);
}
/// <summary>
/// Returns a zero-based index of the first appearance of a given char within this sturct.
/// The method returns -1 if the target is not found in this instance.
/// The search starts at a given position.
/// </summary>
/// <param name="c"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(char c , int offset , int count)
{
return Ref.IndexOf(c , Offset + offset , Math.Min(Length , count));
}
/// <summary>
/// Returns a zero-based index of the first appearance of a given string within this sturct.
/// The method returns -1 if the target is not found in this instance.
/// The search starts at a given position.
/// </summary>
/// <param name="str"></param>
/// <param name="offset"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(string str , int offset)
{
return Ref.IndexOf(str , Offset + offset, Length);
}
/// <summary>
/// Returns a zero-based index of the first appearance of a given string within this sturct.
Expand All @@ -189,11 +217,33 @@ public readonly int IndexOf(char c, int offset)
/// </summary>
/// <param name="str"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(string str, int offset)
public readonly int IndexOf(string str, int offset , int count)
{
return Ref.IndexOf(str , Offset + offset , Math.Min(Length , count));
}
/// <summary>
/// Iterate through characters in range [offset..offset+length].
/// </summary>
/// <returns></returns>

public IEnumerator<char> GetEnumerator()
{
return iterate();
}
IEnumerator<char> iterate()
{
for (int i = Offset ; i < Length ; i++)
{
yield return Ref [ i ];
}
yield break;
}
IEnumerator IEnumerable.GetEnumerator()
{
return Ref.IndexOf(str, Offset + offset);
return iterate();
}
}
}
2 changes: 1 addition & 1 deletion LibCLCC.NET/LibCLCC.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>$(VersionPrefix)1.22.23.0</Version>
<Version>$(VersionPrefix)1.23.24.0</Version>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down

0 comments on commit 368d59b

Please sign in to comment.