Comments (3)
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.
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.
@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)
- `RawString.StartsWith` and `RawString.EndsWith` treats any unpaired surrogate in argument string as "�" HOT 1
- XmlNode.OuterXml HOT 3
- SystemFormatException HOT 5
- Position for each element HOT 2
- Feature Request: Support for mixed content HOT 7
- FormatException should contain detailed error message. HOT 2
- XML parser accepts invalid entities HOT 1
- Ampersand is not allowed in Xml attributes HOT 2
- FormatException thrown when parsing DOCTYPE HOT 7
- The parser should be able to read the xml with comments at the end.
- Generic serialization using source generators HOT 1
- Feature Request: Xml Validation HOT 1
- AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from u8xmlparser.