Skip to content

Commit 241d542

Browse files
committed
code cleanup
1 parent 7b7be8d commit 241d542

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/Shared/_Extensions.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -822,19 +822,19 @@ private static bool _IsDegeneratedTriangle(uint a, uint b, uint c)
822822

823823
#endregion
824824

825-
#region serialization
826-
827-
public static Byte[] ToUnderlayingArray(this ArraySegment<Byte> segment)
828-
{
829-
if (segment.Offset == 0 && segment.Count == segment.Array.Length) return segment.Array;
825+
#region serialization
830826

831-
return segment.ToArray();
827+
public static bool TryGetUnderlayingArray<T>(this ArraySegment<T> segment, out T[] array)
828+
{
829+
array = segment.Array ?? Array.Empty<T>();
830+
return segment.Offset == 0 && segment.Count == segment.Array.Length;
832831
}
833832

834833
public static ArraySegment<Byte> ToArraySegment(this System.IO.MemoryStream m)
835834
{
836-
if (m.TryGetBuffer(out ArraySegment<Byte> data)) return data;
837-
return new ArraySegment<byte>(m.ToArray());
835+
return m.TryGetBuffer(out ArraySegment<Byte> data)
836+
? data
837+
: new ArraySegment<byte>(m.ToArray());
838838
}
839839

840840
public static Byte[] GetPaddedContent(this Byte[] content)
@@ -902,7 +902,7 @@ public static string _EscapeStringInternal(this string uri)
902902
#pragma warning restore SYSLIB0013 // Type or member is obsolete
903903
}
904904

905-
#if NET6_0
905+
#if NET6_0
906906

907907
/// <summary>
908908
/// Creates a new instance of the <see cref="JsonNode"/>.
@@ -914,11 +914,10 @@ public static string _EscapeStringInternal(this string uri)
914914
/// </remarks>
915915
/// <param name="node">The node to clone.</param>
916916
/// <returns>A clone of <paramref name="node"/>.</returns>
917-
#if NET6_0_OR_GREATER
917+
918918
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonValue))]
919919
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonArray))]
920920
[System.Diagnostics.CodeAnalysis.DynamicDependency(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All, typeof(System.Text.Json.Nodes.JsonObject))]
921-
#endif
922921
public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.Nodes.JsonNode node)
923922
{
924923
// issue tracking both DeepClone and DeepEquals: https://github.com/dotnet/runtime/issues/56592
@@ -927,14 +926,14 @@ public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.No
927926

928927
System.Text.Json.Nodes.JsonNode clone = null;
929928

930-
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
929+
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
931930
switch(node)
932931
{
933932
case System.Text.Json.Nodes.JsonValue asValue: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonValue>(asValue); break;
934933
case System.Text.Json.Nodes.JsonArray asArray: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonArray>(asArray); break;
935934
case System.Text.Json.Nodes.JsonObject asObject: clone = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.Nodes.JsonObject>(asObject); break;
936935
}
937-
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
936+
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
938937

939938
if (clone == null) throw new NotImplementedException();
940939

@@ -943,15 +942,13 @@ public static System.Text.Json.Nodes.JsonNode DeepClone(this System.Text.Json.No
943942
return clone;
944943
}
945944

946-
#endif
945+
#endif
947946

948947
public static bool DeepEquals(this System.Text.Json.Nodes.JsonNode x, System.Text.Json.Nodes.JsonNode y, double precission)
949948
{
950-
#if !NET6_0
951-
949+
#if !NET6_0
952950
return System.Text.Json.Nodes.JsonNode.DeepEquals(x, y);
953-
954-
#else
951+
#else
955952

956953
if (x == y) return true;
957954
if (x == null) return false;
@@ -1008,7 +1005,7 @@ public static bool DeepEquals(this System.Text.Json.Nodes.JsonNode x, System.Tex
10081005

10091006
return false;
10101007

1011-
#endif
1008+
#endif
10121009
}
10131010

10141011
#endregion

src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ void _writeBytes(string rawUri, BYTES data)
5555
var path = Uri.UnescapeDataString(rawUri);
5656
path = Path.Combine(dinfo.FullName, path);
5757

58-
File.WriteAllBytes(path, data.ToUnderlayingArray());
58+
using (var s = System.IO.File.Create(path))
59+
{
60+
s.Write(data.Array, data.Offset, data.Count);
61+
}
5962
}
6063

6164
System.IO.Stream _OpenStream(string rawUri)

0 commit comments

Comments
 (0)