Skip to content

Commit

Permalink
Merge pull request #68 from hadashiA/ku/try-read
Browse files Browse the repository at this point in the history
Add TryRead* method families
  • Loading branch information
hadashiA authored Dec 9, 2023
2 parents 1301b79 + 1983fc2 commit 77b9ea1
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 0 deletions.
108 changes: 108 additions & 0 deletions VYaml.Core/Parser/YamlParser.TryGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,96 @@ public double ReadScalarAsDouble()
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsString(out string? result)
{
if (CurrentEventType != ParseEventType.Scalar)
{
result = default;
return false;
}
result = currentScalar?.ToString();
ReadWithVerify(ParseEventType.Scalar);
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsBool(out bool result)
{
if (TryGetScalarAsBool(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsInt32(out int result)
{
if (TryGetScalarAsInt32(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsInt64(out long result)
{
if (TryGetScalarAsInt64(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsUInt32(out uint result)
{
if (TryGetScalarAsUInt32(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsUInt64(out ulong result)
{
if (TryGetScalarAsUInt64(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsFloat(out float result)
{
if (TryGetScalarAsFloat(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsDouble(out double result)
{
if (TryGetScalarAsDouble(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsString(out string? value)
{
Expand Down Expand Up @@ -225,6 +315,15 @@ public readonly bool TryGetScalarAsInt32(out int value)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsUInt32(out uint value)
{
if (currentScalar is { } scalar)
return scalar.TryGetUInt32(out value);
value = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsInt64(out long value)
{
Expand All @@ -234,6 +333,15 @@ public readonly bool TryGetScalarAsInt64(out long value)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsUInt64(out ulong value)
{
if (currentScalar is { } scalar)
return scalar.TryGetUInt64(out value);
value = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsFloat(out float value)
{
Expand Down
108 changes: 108 additions & 0 deletions VYaml.Unity/Assets/VYaml/Runtime/Parser/YamlParser.TryGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,96 @@ public double ReadScalarAsDouble()
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsString(out string? result)
{
if (CurrentEventType != ParseEventType.Scalar)
{
result = default;
return false;
}
result = currentScalar?.ToString();
ReadWithVerify(ParseEventType.Scalar);
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsBool(out bool result)
{
if (TryGetScalarAsBool(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsInt32(out int result)
{
if (TryGetScalarAsInt32(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsInt64(out long result)
{
if (TryGetScalarAsInt64(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsUInt32(out uint result)
{
if (TryGetScalarAsUInt32(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsUInt64(out ulong result)
{
if (TryGetScalarAsUInt64(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsFloat(out float result)
{
if (TryGetScalarAsFloat(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryReadScalarAsDouble(out double result)
{
if (TryGetScalarAsDouble(out result))
{
ReadWithVerify(ParseEventType.Scalar);
return true;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsString(out string? value)
{
Expand Down Expand Up @@ -225,6 +315,15 @@ public readonly bool TryGetScalarAsInt32(out int value)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsUInt32(out uint value)
{
if (currentScalar is { } scalar)
return scalar.TryGetUInt32(out value);
value = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsInt64(out long value)
{
Expand All @@ -234,6 +333,15 @@ public readonly bool TryGetScalarAsInt64(out long value)
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsUInt64(out ulong value)
{
if (currentScalar is { } scalar)
return scalar.TryGetUInt64(out value);
value = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryGetScalarAsFloat(out float value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System.Buffers;
using NUnit.Framework;
using VYaml.Emitter;
Expand Down

0 comments on commit 77b9ea1

Please sign in to comment.