Skip to content

Commit

Permalink
fix(xml-element): fix InsertAttribute(), RemoveAttribute(), GetAttrib…
Browse files Browse the repository at this point in the history
…ute(), and InsertElement() in XmlElement to use new string encoding
  • Loading branch information
LSViana committed Aug 17, 2023
1 parent 23ab920 commit a6c2406
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
25 changes: 18 additions & 7 deletions YDotNet/Document/Types/XmlElements/XmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void InsertAttribute(Transaction transaction, string name, string value)

XmlElementChannel.InsertAttribute(Handle, transaction.Handle, nameHandle, valueHandle);

MemoryWriter.TryRelease(nameHandle);
MemoryWriter.TryRelease(valueHandle);
MemoryWriter.Release(nameHandle);
MemoryWriter.Release(valueHandle);
}

/// <summary>
Expand All @@ -88,7 +88,11 @@ public void InsertAttribute(Transaction transaction, string name, string value)
/// <param name="name">The name of the attribute to be removed.</param>
public void RemoveAttribute(Transaction transaction, string name)
{
XmlElementChannel.RemoveAttribute(Handle, transaction.Handle, name);
var nameHandle = MemoryWriter.WriteUtf8String(name);

XmlElementChannel.RemoveAttribute(Handle, transaction.Handle, nameHandle);

MemoryWriter.Release(nameHandle);
}

/// <summary>
Expand All @@ -99,8 +103,9 @@ public void RemoveAttribute(Transaction transaction, string name)
/// <returns>The value of the attribute or <c>null</c> if it doesn't exist.</returns>
public string? GetAttribute(Transaction transaction, string name)
{
var handle = XmlElementChannel.GetAttribute(Handle, transaction.Handle, name);
var result = Marshal.PtrToStringAnsi(handle);
var nameHandle = MemoryWriter.WriteUtf8String(name);
var handle = XmlElementChannel.GetAttribute(Handle, transaction.Handle, nameHandle);
MemoryReader.TryReadUtf8String(handle, out var result);
StringChannel.Destroy(handle);

return result;
Expand Down Expand Up @@ -155,8 +160,14 @@ public uint ChildLength(Transaction transaction)
/// <returns>The inserted <see cref="XmlText" /> at the given <see cref="index" />.</returns>
public XmlElement? InsertElement(Transaction transaction, uint index, string name)
{
return ReferenceAccessor.Access(
new XmlElement(XmlElementChannel.InsertElement(Handle, transaction.Handle, index, name)));
var nameHandle = MemoryWriter.WriteUtf8String(name);

var result = ReferenceAccessor.Access(
new XmlElement(XmlElementChannel.InsertElement(Handle, transaction.Handle, index, nameHandle)));

MemoryWriter.Release(nameHandle);

return result;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions YDotNet/Native/Types/XmlElementChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ internal static class XmlElementChannel

[DllImport(
ChannelSettings.NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "yxmlelem_remove_attr")]
public static extern void RemoveAttribute(nint handle, nint transaction, string name);
public static extern void RemoveAttribute(nint handle, nint transaction, nint name);

[DllImport(
ChannelSettings.NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "yxmlelem_get_attr")]
public static extern nint GetAttribute(nint handle, nint transaction, string name);
public static extern nint GetAttribute(nint handle, nint transaction, nint name);

[DllImport(
ChannelSettings.NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "yxmlelem_attr_iter")]
Expand All @@ -38,7 +38,7 @@ internal static class XmlElementChannel

[DllImport(
ChannelSettings.NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "yxmlelem_insert_elem")]
public static extern nint InsertElement(nint handle, nint transaction, uint index, string name);
public static extern nint InsertElement(nint handle, nint transaction, uint index, nint name);

[DllImport(
ChannelSettings.NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "yxmlelem_remove_range")]
Expand Down

0 comments on commit a6c2406

Please sign in to comment.