ikorin24 / U8XmlParser

Extremely fast UTF-8 xml parser library
MIT License
95 stars 13 forks source link

Provides line and position information. #28

Closed ikorin24 closed 2 years ago

ikorin24 commented 2 years ago

Features

close #18, close #22

How to Use

const string XmlString =
@"<root>
  <foo>忍殺</foo>
</root>";

using XmlObject xml = XmlParser.Parse(XmlString);
XmlNode foo = xml.Root.FindChild("foo");

// Get location of <foo>...</foo>
DataLocation location = xml.GetLocation(foo);

// Position is char position, NOT byte offset
Console.WriteLine($"Start: Line {location.Start.Line}, Position {location.Start.Position}");
Console.WriteLine($"End: Line {location.End.Line}, Position {location.End.Position}");

// Length is byte length, NOT string length
Console.WriteLine($"Range: Start {location.Range.Start}, Length {location.Range.Length}");

/*
 * (Numbers are zero-based)
 * Start: Line 1, Position 2
 * End: Line 1, Position 15
 * Range: Start 10, Length 17
*/

Public API Diff

 namespace U8Xml
 {
+    public readonly struct DataLocation : IEquatable<DataLocation>
+    {
+        public readonly DataLinePosition Start;
+        public readonly DataLinePosition End;
+        public readonly DataRange Range;
+        public DataLocation(DataLinePosition start, DataLinePosition end, DataRange range);
+        public void Deconstruct(out DataLinePosition start, out DataLinePosition end, out DataRange range);
+        public override bool Equals(object? obj);
+        public bool Equals(DataLocation other);
+        public override int GetHashCode();
+        public static bool operator ==(in DataLocation left, in DataLocation right)
+        public static bool operator !=(in DataLocation left, in DataLocation right);
+        public override string ToString();
+    }

+    public readonly struct DataLinePosition : IEquatable<DataLinePosition>
+    {
+        public readonly int Line;
+        public readonly int Position;
+        public DataLinePosition(int line, int position);
+        public void Deconstruct(out int line, out int position);
+        public override bool Equals(object? obj);
+        public bool Equals(DataLinePosition other);
+        public override int GetHashCode();
+        public static bool operator ==(DataLinePosition left, DataLinePosition right);
+        public static bool operator !=(DataLinePosition left, DataLinePosition right);
+        public override string ToString();
+    }

+    public readonly struct DataRange : IEquatable<DataRange>
+    {
+        public readonly int Start;
+        public readonly int Length;
+        public DataRange(int start, int length);
+        public void Deconstruct(out int start, out int length);
+        public override bool Equals(object? obj);
+        public bool Equals(DataRange other);
+        public override int GetHashCode();
+        public static bool operator ==(DataRange left, DataRange right);
+        public static bool operator !=(DataRange left, DataRange right);
+        public override string ToString();
+    }

+    public static class SpanByteExtensions
+    {
+        public static Span<byte> Slice(this Span<byte> span, DataRange range);
+        public static ReadOnlySpan<byte> Slice(this ReadOnlySpan<byte> span, DataRange range);
+    }

     public readonly struct RawString : IEquatable<RawString>
     {
+        public RawString Slice(DataRange range);
+        public bool ReferenceEquals(RawString other);
     }

     public sealed class XmlObject : IDisposable
     {
+        public RawString AsRawString(int start);
+        public RawString AsRawString(int start, int length);
+        public RawString AsRawString(DataRange range);
+        public DataLocation GetLocation(XmlNode node);
+        public DataLocation GetLocation(XmlAttribute attr);
+        public DataLocation GetLocation(RawString str);
+        public DataLocation GetLocation(DataRange range);
+        public DataRange GetRange(XmlNode node);
+        public DataRange GetRange(XmlAttribute attr);
+        public DataRange GetRange(RawString str);
     }
 }

 namespace U8Xml.Unsafes
 {
     public readonly struct XmlObjectUnsafe : IDisposable
     {
+        public RawString AsRawString(int start);
+        public RawString AsRawString(int start, int length);
+        public RawString AsRawString(DataRange range);
+        public DataLocation GetLocation(XmlNode node);
+        public DataLocation GetLocation(XmlAttribute attr);
+        public DataLocation GetLocation(RawString str);
+        public DataLocation GetLocation(DataRange range);
+        public DataRange GetRange(XmlNode node);
+        public DataRange GetRange(XmlAttribute attr);
+        public DataRange GetRange(RawString str);
     }
 }