Skip to content

Commit

Permalink
refactor: unsafe calls
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Brault <[email protected]>
  • Loading branch information
xaevik committed Dec 8, 2023
1 parent c05e9a2 commit dac3612
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
69 changes: 26 additions & 43 deletions src/cuid.net/Cuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,38 +278,24 @@ public override int GetHashCode()
/// <returns>The value of this <see cref="Cuid" />.</returns>
public override string ToString()
{
return string.Create(25, ( _t: _timestamp, _c: _counter, _f: _fingerprint, _r: _random ), (dest, buffer) =>
{
Prefix
.WriteTo(ref
dest);
Utils
.Encode((ulong)
buffer
._t)
.WriteTo(ref
dest);
Utils
.Encode(buffer
._c)
.TrimPad(BlockSize)
.WriteTo(ref
dest);
buffer._f
.WriteTo(ref
dest);
Utils
.Encode(buffer
._r)
.TrimPad(BlockSize *
2)
.WriteTo(ref
dest);
});
return string.Create(25, ( _t: _timestamp, _c: _counter, _f: _fingerprint, _r: _random ),
(dest, buffer) =>
{
Prefix.WriteTo(ref dest);
Utils.Encode((ulong) buffer._t)
.WriteTo(ref dest);
Utils.Encode(buffer._c)
.TrimPad(BlockSize)
.WriteTo(ref dest);
buffer._f.WriteTo(ref dest);
Utils.Encode(buffer._r)
.TrimPad(BlockSize * 2)
.WriteTo(ref dest);
});
}

private static bool IsAlphaNum(ReadOnlySpan<char> input)
Expand Down Expand Up @@ -357,27 +343,24 @@ private static bool TryParseCuid(ReadOnlySpan<char> cuidString, bool throwExcept
return null;
}

unsafe void IXmlSerializable.ReadXml(XmlReader reader)
/// <inheritdoc />
public void ReadXml(XmlReader reader)
{
reader.Read();

CuidResult result = new();

_ = TryParseCuid(reader.Value, true, ref result);

#pragma warning disable CS8500
fixed ( Cuid* c = &this )
{
*c = result.ToCuid();
}
#pragma warning restore CS8500
Unsafe.AsRef(in this) = result.ToCuid();
}

void IXmlSerializable.WriteXml(XmlWriter writer)
/// <inheritdoc />
public void WriteXml(XmlWriter writer)
{
writer.WriteString(ToString());
}

[SuppressMessage("ReSharper", "InconsistentNaming")]
[StructLayout(LayoutKind.Explicit)]
private struct CuidResult
Expand Down
1 change: 0 additions & 1 deletion src/cuid.net/cuid.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Nullable>enable</Nullable>
<RootNamespace>Visus.Cuid</RootNamespace>
<Copyright>Copyright (c) 2023 Visus Development Team</Copyright>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
public bool Equals(Visus.Cuid.Cuid other) { }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public void ReadXml(System.Xml.XmlReader reader) { }
public override string ToString() { }
public void WriteXml(System.Xml.XmlWriter writer) { }
public static Visus.Cuid.Cuid NewCuid() { }
public static Visus.Cuid.Cuid Parse(System.ReadOnlySpan<char> input) { }
public static Visus.Cuid.Cuid Parse(string input) { }
Expand Down

0 comments on commit dac3612

Please sign in to comment.