Coder Social home page Coder Social logo

RawString append. about u8xmlparser HOT 3 CLOSED

ikorin24 avatar ikorin24 commented on September 4, 2024
RawString append.

from u8xmlparser.

Comments (3)

ikorin24 avatar ikorin24 commented on September 4, 2024 1

Hi, @juliolitwin

The easiest way to combine RawString is calling RawString.ToString() and treating them as string.

RawString foo = ...;
RawString bar = ...;

string combined = $"{foo} and {bar}";

If you also need to take care of performance, you can use RawString.AsSpan() to copy it to byte[] and treat it as utf-8.

However, I have not provided any classes or methods to do this. If you want to do this easily,
a library such as ZString might be suitable.

RawString foo = ...;
RawString bar = ...;

using (var sb = Cysharp.Text.ZString.CreateUtf8StringBuilder())
{
    sb.AppendLiteral(foo.AsSpan());
    sb.Append(" and ");
    sb.AppendLiteral(bar.AsSpan());
    string combined = sb.ToString();
}

from u8xmlparser.

juliolitwin avatar juliolitwin commented on September 4, 2024

Hey @ikorin24, thanks for answering me.

I understand. The performance issue is really important for me, so the first suggestion is not so viable. I know ZString, but I wanted to be able to keep RawString, it's amazing and has a lot of power. I can make an append, but unfortunately the problem is freeing the memory after use.

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        internal RawString(int length)
        {
            Debug.Assert(length >= 0);
            _ptr = Marshal.AllocHGlobal(length);
            AllocationSafety.Add(length);
            _length = length;
        }

        public RawString Append(RawString value)
        {
            // Get the new length from the two strings.
            var totalLength = Length + value.Length;

            // Initialize the new buffer.
            var appendString = new RawString(totalLength);

            fixed (byte* ptr = this.AsSpan())
            fixed (byte* ptr2 = value.AsSpan())
            {
                // Copy the first string for the new buffer.
                SpanHelper.CreateReadOnlySpan<byte>(ptr, Length).CopyTo(SpanHelper.CreateSpan<byte>(appendString.GetPtr(), appendString.Length)[..Length]);
                
                // Copy the second string for the new buffer.
                SpanHelper.CreateReadOnlySpan<byte>(ptr2, value.Length).CopyTo(SpanHelper.CreateSpan<byte>(appendString.GetPtr(), appendString.Length).Slice(Length, value.Length));
            }

            return new RawString((byte*)appendString.Ptr, totalLength);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Dispose()
        {
            Marshal.FreeHGlobal(_ptr);
            AllocationSafety.Remove(_length);
            Unsafe.AsRef(_ptr) = default;
            Unsafe.AsRef(_length) = 0;
        }

from u8xmlparser.

ikorin24 avatar ikorin24 commented on September 4, 2024

@juliolitwin OK, I'll tell you implementation of RawString.

RawString does not have ownership of its memory. It is similar to ReadOnlySpan<byte>.

All RawString memories are owned by the XmlObject, which is a pointer to a buffer of bytes read from xml.
This is released when XmlObject.Dispose() is called.
That means the only way to create a new RawString is slicing the buffer memory read from xml.
And I think that I will not provide any other way to create RawString instance.

So, for high performance combining RawString, we should write to Span<byte> you allocate somewhere.

public static bool TryCombine(RawString str1, RawString str2, Span<byte> destination)
{
    if (destination.Length < str1.Length + str2.Length)
    {
        return false;
    }
    str1.AsSpan().CopyTo(destination);
    str2.AsSpan().CopyTo(destination.Slice(str1.Length));
    return true;
}

Most methods in U8XmlParser provides the overload for RawString and ReadOnlySpan<byte>, so you can treat them in the same way.

from u8xmlparser.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.