Beispiel #1
0
        public TextPtr SkipWhitespace(bool skipNewline = false)
        {
            TextPtr ptr    = this;
            int     length = ptr.Content.Length;

            if (skipNewline)
            {
                while (true)
                {
                    char c = ptr.Char;
                    if ((ptr.Index >= length) || !char.IsWhiteSpace(c))
                    {
                        return(ptr);
                    }
                    ptr = op_Increment(ptr);
                }
            }
            while (true)
            {
                char ch2 = ptr.Char;
                if (((ptr.Index >= length) || ptr.IsNewLine()) || !char.IsWhiteSpace(ch2))
                {
                    return(ptr);
                }
                ptr = op_Increment(ptr);
            }
        }
Beispiel #2
0
        public TextPtr FindEndOfLine(bool skipNewline = false)
        {
            int length = this.Content.Length;

            if (this.Index >= length)
            {
                return(this);
            }
            TextPtr ptr = this;

            while (ptr.Index < length)
            {
                if (ptr.IsNewLine())
                {
                    if (!skipNewline)
                    {
                        return(ptr);
                    }
                    if (ptr.Char == '\r')
                    {
                        ptr = op_Increment(ptr);
                    }
                    return(op_Increment(ptr));
                }
                ptr = op_Increment(ptr);
            }
            return(ptr);
        }
Beispiel #3
0
 private void Realize(ref MyIniKey key, ref StringSegment value)
 {
     if (!value.IsCached)
     {
         string  text = value.Text;
         TextPtr ptr  = new TextPtr(text, value.Start);
         if ((value.Length > 0) && ptr.IsNewLine())
         {
             StringBuilder tmpValueBuilder = this.TmpValueBuilder;
             try
             {
                 ptr = TextPtr.op_Increment(ptr.FindEndOfLine(true));
                 int count = (value.Start + value.Length) - ptr.Index;
                 tmpValueBuilder.Append(text, ptr.Index, count);
                 tmpValueBuilder.Replace("\n|", "\n");
                 this.m_items[key] = value = new StringSegment(tmpValueBuilder.ToString());
             }
             finally
             {
                 tmpValueBuilder.Clear();
             }
         }
         else
         {
             this.m_items[key] = value = new StringSegment(value.ToString());
         }
     }
 }
Beispiel #4
0
        private static int FindSection(string config, string section)
        {
            TextPtr ptr = new TextPtr(config);

            if (!MatchesSection(ref ptr, section))
            {
                while (!ptr.IsOutOfBounds())
                {
                    ptr = TextPtr.op_Increment(ptr.Find("\n"));
                    if (ptr.Char == '[')
                    {
                        if (MatchesSection(ref ptr, section))
                        {
                            return(ptr.Index);
                        }
                    }
                    else if (ptr.StartsWith("---"))
                    {
                        ptr = (ptr + 3).SkipWhitespace(false);
                        if (ptr.IsEndOfLine())
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                return(ptr.Index);
            }
            return(-1);
        }
Beispiel #5
0
        private void ParseParameter(ref TextPtr ptr)
        {
            StringSegment item  = this.ParseQuoted(ref ptr);
            int           count = this.Items.Count;

            this.m_items.Add(item);
            this.m_argumentIndexes.Add(count);
        }
Beispiel #6
0
 public bool IsStartOfLine()
 {
     if (this.Index > 0)
     {
         TextPtr ptr = this - 1;
         return(ptr.IsNewLine());
     }
     return(true);
 }
Beispiel #7
0
        private bool TryParseSwitch(ref TextPtr ptr)
        {
            if (ptr.Char != '-')
            {
                return(false);
            }
            StringSegment item  = this.ParseQuoted(ref ptr);
            int           count = this.Items.Count;

            this.m_items.Add(item);
            this.m_switchIndexes.Add(new StringSegment(item.Text, item.Start + 1, item.Length - 1), count);
            return(true);
        }
Beispiel #8
0
        public bool StartsWith(string what)
        {
            TextPtr ptr = this;

            foreach (char ch in what)
            {
                if (ptr.Char != ch)
                {
                    return(false);
                }
                ptr = op_Increment(ptr);
            }
            return(true);
        }
Beispiel #9
0
        public bool StartsWithCaseInsensitive(string what)
        {
            TextPtr ptr = this;

            foreach (char ch in what)
            {
                if (char.ToUpper(ptr.Char) != char.ToUpper(ch))
                {
                    return(false);
                }
                ptr = op_Increment(ptr);
            }
            return(true);
        }
Beispiel #10
0
        private void ReadPrefix(ref TextPtr ptr, out StringSegment prefix)
        {
            bool    flag = false;
            TextPtr ptr2 = ptr;

            while (!ptr.IsOutOfBounds())
            {
                if (ptr.IsStartOfLine() && (ptr.Char == ';'))
                {
                    if (!flag)
                    {
                        flag = true;
                        ptr2 = ptr;
                    }
                    ptr = ptr.FindEndOfLine(false);
                }
                TextPtr ptr3 = ptr.SkipWhitespace(false);
                if (!ptr3.IsNewLine())
                {
                    break;
                }
                if (ptr3.Char == '\r')
                {
                    ptr = ptr3 + 2;
                }
                else
                {
                    ptr = ptr3 + 1;
                }
            }
            if (flag)
            {
                TextPtr ptr4 = ptr;
                while (char.IsWhiteSpace(ptr4.Char) && (ptr4 > ptr2))
                {
                    ptr4 = TextPtr.op_Decrement(ptr4);
                }
                int length = ptr4.Index - ptr2.Index;
                if (length > 0)
                {
                    prefix = new StringSegment(ptr.Content, ptr2.Index, length);
                    return;
                }
            }
            prefix = new StringSegment();
        }
Beispiel #11
0
        private static bool MatchesSection(ref TextPtr ptr, string section)
        {
            if (!ptr.StartsWith("["))
            {
                return(false);
            }
            TextPtr ptr2 = ptr + 1;

            if (!ptr2.StartsWithCaseInsensitive(section))
            {
                return(false);
            }
            ptr2 += section.Length;
            if (!ptr2.StartsWith("]"))
            {
                return(false);
            }
            return(true);
        }
Beispiel #12
0
        public bool TryParse(string argument)
        {
            this.Clear();
            if (string.IsNullOrEmpty(argument))
            {
                return(false);
            }
            TextPtr ptr = new TextPtr(argument);

Label_001D:
            ptr = ptr.SkipWhitespace(false);
            if (!ptr.IsOutOfBounds())
            {
                if (!this.TryParseSwitch(ref ptr))
                {
                    this.ParseParameter(ref ptr);
                }
                goto Label_001D;
            }
            return(this.Items.Count > 0);
        }
Beispiel #13
0
 private bool TryParseCore(string content, string section, ref MyIniParseResult result)
 {
     content = content ?? "";
     if (!string.Equals(this.m_content, content, StringComparison.Ordinal))
     {
         this.Clear();
         TextPtr ptr = new TextPtr(content);
         if (section != null)
         {
             int num = FindSection(content, section);
             if (num == -1)
             {
                 if (result.IsDefined)
                 {
                     result = new MyIniParseResult(new TextPtr(content), $"Cannot find section " { section } "");
                 }
                 return(false);
             }
             ptr += num;
         }
         while (!ptr.IsOutOfBounds())
         {
             if (!this.TryParseSection(ref ptr, ref result, section == null))
             {
                 if (result.IsDefined && !result.Success)
                 {
                     return(false);
                 }
                 break;
             }
             if (section != null)
             {
                 this.m_content = null;
                 return(true);
             }
         }
         this.m_content = content;
     }
     return(true);
 }
Beispiel #14
0
        private StringSegment ParseQuoted(ref TextPtr ptr)
        {
            TextPtr ptr3;
            TextPtr ptr4;
            TextPtr ptr2 = ptr;
            bool    flag = ptr2.Char == '"';

            if (flag)
            {
                ptr2 = TextPtr.op_Increment(ptr2);
            }
            for (ptr3 = ptr2; !ptr3.IsOutOfBounds(); ptr3 = TextPtr.op_Increment(ptr3))
            {
                if (ptr3.Char == '"')
                {
                    flag = !flag;
                }
                if (!flag && char.IsWhiteSpace(ptr3.Char))
                {
                    ptr  = ptr3;
                    ptr4 = ptr3 - 1;
                    if (ptr4.Char == '"')
                    {
                        ptr3 = ptr4;
                    }
                    return(new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index));
                }
            }
            ptr3 = new TextPtr(ptr.Content, ptr.Content.Length);
            ptr  = ptr3;
            ptr4 = ptr3 - 1;
            if (ptr4.Char == '"')
            {
                ptr3 = ptr4;
            }
            return(new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index));
        }
 internal MyIniParseResult(TextPtr ptr, string error)
 {
     this.m_lineNo = 0;
     this.m_ptr    = ptr;
     this.Error    = error;
 }
Beispiel #16
0
 private void RealizeComment(ref StringSegment comment)
 {
     if (!comment.IsCached)
     {
         string  text = comment.Text;
         TextPtr ptr  = new TextPtr(text, comment.Start);
         if (comment.Length > 0)
         {
             StringBuilder tmpValueBuilder = this.TmpValueBuilder;
             try
             {
                 TextPtr ptr2 = ptr + comment.Length;
                 for (bool flag = false; ptr < ptr2; flag = true)
                 {
                     if (flag)
                     {
                         tmpValueBuilder.Append('\n');
                     }
                     if (ptr.Char == ';')
                     {
                         ptr = TextPtr.op_Increment(ptr);
                         TextPtr ptr3  = ptr.FindEndOfLine(false);
                         int     count = ptr3.Index - ptr.Index;
                         tmpValueBuilder.Append(ptr.Content, ptr.Index, count);
                         ptr = ptr3.SkipWhitespace(false);
                         if (ptr.IsEndOfLine())
                         {
                             if (ptr.Char == '\r')
                             {
                                 ptr += 2;
                             }
                             else
                             {
                                 ptr = TextPtr.op_Increment(ptr);
                             }
                         }
                     }
                     else
                     {
                         ptr = ptr.SkipWhitespace(false);
                         if (!ptr.IsEndOfLine())
                         {
                             break;
                         }
                         if (ptr.Char == '\r')
                         {
                             ptr += 2;
                         }
                         else
                         {
                             ptr = TextPtr.op_Increment(ptr);
                         }
                     }
                 }
                 comment = new StringSegment(tmpValueBuilder.ToString());
             }
             finally
             {
                 tmpValueBuilder.Clear();
             }
         }
     }
 }
Beispiel #17
0
        private bool TryParseItem(ref StringSegment section, ref TextPtr ptr, ref MyIniParseResult result, bool parseEndContent)
        {
            StringSegment segment;
            TextPtr       ptr3;
            TextPtr       ptr2 = ptr;

            this.ReadPrefix(ref ptr2, out segment);
            this.m_endComment = segment;
            if (ptr2.StartsWith("---"))
            {
                ptr3 = (ptr2 + 3).SkipWhitespace(false);
                if (ptr3.IsEndOfLine())
                {
                    this.m_endComment = segment;
                    ptr2 = ptr3;
                    ptr3 = new TextPtr(ptr2.Content, ptr2.Content.Length);
                    ptr  = ptr3 = ptr3;
                    if (parseEndContent)
                    {
                        ptr2 = ptr2.FindEndOfLine(true);
                        this.m_endContent = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
                    }
                    return(false);
                }
            }
            ptr2 = ptr2.TrimStart();
            if (!ptr2.IsOutOfBounds() && (ptr2.Char != '['))
            {
                ptr3 = ptr2.FindInLine('=');
                if (ptr3.IsOutOfBounds())
                {
                    if (result.IsDefined)
                    {
                        result = new MyIniParseResult(ptr2, "Expected key=value definition");
                    }
                    return(false);
                }
                StringSegment segment2 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.TrimEnd().Index - ptr2.Index);
                string        str      = MyIniKey.ValidateKey(ref segment2);
                if (str == null)
                {
                    ptr2 = ptr3 + 1;
                    ptr2 = ptr2.TrimStart();
                    ptr3 = ptr2.FindEndOfLine(false);
                    StringSegment segment3 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.TrimEnd().Index - ptr2.Index);
                    if (segment3.Length == 0)
                    {
                        TextPtr ptr4 = ptr3.FindEndOfLine(true);
                        if (ptr4.Char == '|')
                        {
                            TextPtr ptr5 = ptr4;
                            do
                            {
                                ptr4 = ptr5.FindEndOfLine(false);
                                ptr5 = ptr4.FindEndOfLine(true);
                            }while (ptr5.Char == '|');
                            ptr3 = ptr4;
                        }
                        segment3 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
                    }
                    MyIniKey key = new MyIniKey(section, segment2);
                    if (this.m_items.ContainsKey(key))
                    {
                        if (result.IsDefined)
                        {
                            result = new MyIniParseResult(new TextPtr(segment2.Text, segment2.Start), $"Duplicate key {key}");
                        }
                        return(false);
                    }
                    this.m_items[key] = segment3;
                    if (!segment.IsEmpty)
                    {
                        this.m_itemComments[key] = segment;
                        this.m_endComment        = new StringSegment();
                    }
                    ptr = ptr3.FindEndOfLine(true);
                    return(true);
                }
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr2, $"Key {str}");
                }
            }
            return(false);
        }
Beispiel #18
0
        private bool TryParseSection(ref TextPtr ptr, ref MyIniParseResult result, bool parseEndContent)
        {
            StringSegment segment;
            TextPtr       ptr2 = ptr;

            this.ReadPrefix(ref ptr2, out segment);
            this.m_endComment = segment;
            if (ptr2.Char != '[')
            {
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr, "Expected [section] definition");
                }
                return(false);
            }
            TextPtr ptr3 = ptr2.FindEndOfLine(false);

            while ((ptr3.Index > ptr2.Index) && (ptr3.Char != ']'))
            {
                ptr3 = TextPtr.op_Decrement(ptr3);
            }
            if (ptr3.Char != ']')
            {
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr, "Expected [section] definition");
                }
                return(false);
            }
            ptr2 = TextPtr.op_Increment(ptr2);
            StringSegment segment2 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
            string        str      = MyIniKey.ValidateSection(ref segment2);

            if (str == null)
            {
                ptr2 = (ptr3 + 1).SkipWhitespace(false);
                if (!ptr2.IsEndOfLine())
                {
                    if (result.IsDefined)
                    {
                        result = new MyIniParseResult(ptr2, "Expected newline");
                    }
                    return(false);
                }
                ptr2 = ptr2.FindEndOfLine(true);
                this.AddSection(ref segment2);
                if (!segment.IsEmpty)
                {
                    this.m_sectionComments[segment2] = segment;
                    this.m_endComment = new StringSegment();
                }
                while (this.TryParseItem(ref segment2, ref ptr2, ref result, parseEndContent))
                {
                }
                if (result.IsDefined && !result.Success)
                {
                    return(false);
                }
                ptr = ptr2;
                return(true);
            }
            if (result.IsDefined)
            {
                result = new MyIniParseResult(ptr2, $"Section {str}");
            }
            return(false);
        }