Created
August 2, 2012 05:43
-
-
Save rpgmaker/3234105 to your computer and use it in GitHub Desktop.
XmlReaderEx (Improved Version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Runtime.InteropServices; | |
using System.Text.RegularExpressions; | |
namespace PServiceBus.Serializer.Xml { | |
public unsafe class XmlReaderEx { | |
private int _index; | |
private string _elementName, _attributeTypeValue, _xml; | |
private bool _isEndElement; | |
const string TypeStr = "Type"; | |
const char SpaceStr = ' ', GreaterStr = '>', LessStr = '<', BackSlash = '/', Null = '\0'; | |
private static Regex _typeRegex = new Regex(@"type=""(?<Type>[^""]+)""", RegexOptions.Compiled); | |
public XmlReaderEx(string xml) { | |
_xml = xml; | |
} | |
public string ElementName { get { return _elementName; } } | |
public string TypeAttribute { get { return _attributeTypeValue; } } | |
public bool IsEndElement { get { return _isEndElement; } } | |
public string GetValue() { | |
if (_isEndElement) return string.Empty; | |
var count = 1; | |
fixed (char* p = _xml) { | |
char* chrs = p + _index; | |
var current = *chrs; | |
if (current == Null) return string.Empty; | |
do { | |
current = *(++chrs); | |
_index++; | |
if (current == LessStr) break; | |
count++; | |
} while (true); | |
var chars = new char[count]; | |
fixed (char* cp = chars) | |
memcpy(cp, ((char*)(chrs - count)), count); | |
return new string(chars); | |
} | |
} | |
public bool Read(){ | |
char current = Null; | |
bool hasValue = false, hasTypeAttribute = false; | |
string elementName = null; | |
int count = 0; | |
fixed (char* p = _xml) { | |
char* chrs = p + _index; | |
if (*chrs == Null) return false; | |
_attributeTypeValue = null; | |
_isEndElement = false; | |
do { | |
current = *(++chrs); | |
count++; | |
_index++; | |
if (current == GreaterStr) { | |
_index++; | |
++chrs; | |
break; | |
} | |
if (current == SpaceStr && elementName == null) { | |
elementName = GetString(chrs, count); | |
count = 0; | |
hasTypeAttribute = true; | |
continue; | |
} | |
} while (true); | |
if (hasValue = (count > 0 && *(chrs - count) != Null)) { | |
var text = GetString(chrs, count); | |
if (hasTypeAttribute) | |
_attributeTypeValue = _typeRegex.Match(text).Groups[TypeStr].Value; | |
else if (elementName == null) | |
elementName = text; | |
if (elementName[0] == BackSlash) { | |
_isEndElement = true; | |
elementName = elementName.Substring(1); | |
} | |
} | |
} | |
_elementName = elementName; | |
return hasValue; | |
} | |
private unsafe static string GetString(char* chrs, int count) { | |
var size = count - 1; | |
var chars = new char[size]; | |
fixed (char* cp = chars) | |
memcpy(cp, ((char*)(chrs - count)), size); | |
return new string(chars); | |
} | |
private static unsafe void memcpy(char* dmem, char* smem, int charCount) { | |
if ((((int)dmem) & 2) != 0) { | |
dmem[0] = smem[0]; | |
dmem++; | |
smem++; | |
charCount--; | |
} | |
while (charCount >= 8) { | |
*((uint*)dmem) = *((uint*)smem); | |
*((uint*)(dmem + 2)) = *((uint*)(smem + 2)); | |
*((uint*)(dmem + 4)) = *((uint*)(smem + 4)); | |
*((uint*)(dmem + 6)) = *((uint*)(smem + 6)); | |
dmem += 8; | |
smem += 8; | |
charCount -= 8; | |
} | |
if ((charCount & 4) != 0) { | |
*((uint*)dmem) = *((uint*)smem); | |
*((uint*)(dmem + 2)) = *((uint*)(smem + 2)); | |
dmem += 4; | |
smem += 4; | |
} | |
if ((charCount & 2) != 0) { | |
*((uint*)dmem) = *((uint*)smem); | |
dmem += 2; | |
smem += 2; | |
} | |
if ((charCount & 1) != 0) { | |
dmem[0] = smem[0]; | |
} | |
} | |
public void MoveToEndElement(string name) { | |
do { | |
if (_elementName == name && _isEndElement) break; | |
} while (Read()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment