Skip to content

Instantly share code, notes, and snippets.

@yojimbo87
Created August 26, 2012 08:06
Show Gist options
  • Save yojimbo87/3476002 to your computer and use it in GitHub Desktop.
Save yojimbo87/3476002 to your computer and use it in GitHub Desktop.
Raw document string parsing test
internal static ODocument ToDocument(int version, string rawDocument)
{
ODocument document = new ODocument();
document.Version = version;
int atIndex = rawDocument.IndexOf('@');
int colonIndex = rawDocument.IndexOf(':');
int fieldNameStartIndex = 0;
int stringStartIndex = -1;
bool isNextFieldChild = false;
// parse class name
if ((atIndex != -1) && (atIndex < colonIndex))
{
document.Class = rawDocument.Substring(0, atIndex);
fieldNameStartIndex = atIndex + 1;
}
else
{
document.Class = "";
}
for (int i = 0; i < rawDocument.Length; i++)
{
if (fieldNameStartIndex != -1)
{
// parse field name
if (rawDocument[i] == ':')
{
document.Fields.Add(rawDocument.Substring(fieldNameStartIndex, i - fieldNameStartIndex), null);
fieldNameStartIndex = -1;
if ((rawDocument[i + 1] == '[') || (rawDocument[i + 1] == '('))
{
}
}
}
else
{
// check for start of a field name
if ((stringStartIndex == -1) && (rawDocument[i] == ','))
{
fieldNameStartIndex = i + 1;
}
// parse string
if (rawDocument[i].Equals('"'))
{
if (stringStartIndex == -1)
{
stringStartIndex = i;
}
else
{
stringStartIndex = -1;
}
}
}
}
return document;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment