FindEndOfString() public static method

public static FindEndOfString ( String text, int start ) : int
text String
start int
return int
示例#1
0
        public bool parseNext(ref string buffer, out string cmd)
        {
            for (int i = 0; i < buffer.Length; i++)
            {
                string c = buffer.Substring(i, 1);

                if (c == "\"")
                {
                    i = Utils.FindEndOfString(buffer, i + 1);
                }
                else if (c == ".")
                {
                    int pTest;
                    if (i == buffer.Length - 1 || int.TryParse(buffer.Substring(i + 1, 1), out pTest) == false)
                    {
                        cmd    = buffer.Substring(0, i);
                        buffer = buffer.Substring(i + 1).Trim();
                        return(true);
                    }
                }
                else if (c == "{")
                {
                    i = Utils.BraceMatch(buffer, i);

                    if (i == -1)
                    {
                        cmd = "";
                        return(false);
                    }
                }
            }

            cmd = "";
            return(false);
        }
示例#2
0
        private List <String> splitByListIgnoreBracket(String input, ref List <String> operators, bool returnIfOneElement)
        {
            String        buffer = "";
            String        s;
            List <String> retList = new List <string>();

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '(')
                {
                    int startI = i;
                    Utils.Balance(ref Text, ref i, ')');
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (input[i] == '"')
                {
                    int startI = i;
                    i       = Utils.FindEndOfString(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else
                {
                    s = MatchAt(ref input, i, operators);

                    if (s != null)
                    {
                        // TODO: If buffer empty, syntax error

                        retList.Add(buffer);
                        retList.Add(s);
                        buffer = "";

                        i += s.Length - 1;
                    }
                    else
                    {
                        buffer += input[i];
                    }
                }
            }

            if (buffer.Trim() != "")
            {
                retList.Add(buffer);
            }

            if (returnIfOneElement)
            {
                return(retList);
            }
            else
            {
                return(retList.Count > 1 ? retList : null);
            }
        }
示例#3
0
        public string stripComment(string line)
        {
            for (var i = 0; i < line.Length; i++)
            {
                if (line[i] == '\"')
                {
                    i = Utils.FindEndOfString(line, i + 1);
                    if (i == -1)
                    {
                        break;
                    }
                }
                else if (i < line.Length - 1 && line.Substring(i, 2) == "//")
                {
                    return(line.Substring(0, i));
                }
            }

            return(line);
        }
示例#4
0
文件: Utils.cs 项目: yadenisyur/KOS
        public static string[] ProcessParams(string input)
        {
            String        buffer = "";
            List <String> output = new List <string>();

            for (var i = 0; i < input.Length; i++)
            {
                char c = input[i];

                if (c == '\"')
                {
                    var prevI = i;
                    i       = Utils.FindEndOfString(input, i + 1);
                    buffer += input.Substring(prevI, i - prevI + 1);
                }
                else
                {
                    if (c == ',')
                    {
                        output.Add(buffer.Trim());
                        buffer = "";
                    }
                    else
                    {
                        buffer += c;
                    }
                }
            }

            if (buffer.Trim().Length > 0)
            {
                output.Add(buffer.Trim());
            }

            return(output.ToArray());
        }
示例#5
0
        private void processSymbols()
        {
            // Is the input empty?
            if (String.IsNullOrEmpty(Text))
            {
                return;
            }

            // HEADING.. BY is now deprecated in favor of HEADING(x,y), but here it is if you're using it still
            Text = Regex.Replace(Text, "HEADING ([ :@A-Za-z0-9\\.\\-\\+\\*/]+) BY ([ :@A-Za-z0-9\\.\\-\\+\\*/]+)", "HEADING($2,$1)", RegexOptions.IgnoreCase);

            //enables scientific notation eg 6.6E-11 -> 6.6*10^-11
            Text = Regex.Replace(Text, "(\\d)E([-+]{1}[0-9]+)", "$1*10^$2");

            // Resource tags are now deprecated in favor of SHIP:ResourceName
            Text = Regex.Replace(Text, "(\\s|^)<([a-zA-Z]+)>(\\s|$)", " SHIP:$2 ", RegexOptions.IgnoreCase);

            // Is this JUST a matched symbol?
            String s = MatchAt(ref Text, 0, allSymbols);

            if (s != null && Text.Length == s.Length)
            {
                if (mathSymbols.Contains(s))
                {
                    Type = TermTypes.MATH_OPERATOR;
                }
                else if (comparisonSymbols.Contains(s))
                {
                    Type = TermTypes.COMPARISON_OPERATOR;
                }
                else if (booleanSymbols.Contains(s))
                {
                    Type = TermTypes.BOOLEAN_OPERATOR;
                }

                return;
            }

            SubTerms = new List <Term>();

            // If this is a parameter list, grab the parameters
            if (Type == TermTypes.PARAMETER_LIST)
            {
                var parameterList = parseParameters(Text);
                if (parameterList != null)
                {
                    foreach (String param in parameterList)
                    {
                        SubTerms.Add(new Term(param));
                    }
                }

                return;
            }

            // Does this thing contain a boolean operation?
            var booleanElements = splitByListIgnoreBracket(Text, ref booleanSymbols);

            if (booleanElements != null)
            {
                Type = TermTypes.BOOLEAN;

                foreach (String element in booleanElements)
                {
                    if (booleanSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.BOOLEAN_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }

            // Does this thing contain a comparison?
            var comparisonElements = splitByListIgnoreBracket(Text, ref comparisonSymbols);

            if (comparisonElements != null)
            {
                Type = TermTypes.COMPARISON;

                foreach (String element in comparisonElements)
                {
                    SubTerms.Add(new Term(element));
                }

                return;
            }

            // Does this thing contain an Index?
            var listElements = splitByListIgnoreBracket(Text, ref listaccessSymbols);

            if (listElements != null)
            {
                Type = TermTypes.INDEX;

                foreach (var element in listElements)
                {
                    if (listaccessSymbols.Contains(element))
                    {
                        SubTerms.Add(new Term(element, TermTypes.INDEX_OPERATOR));
                    }
                    else
                    {
                        SubTerms.Add(new Term(element));
                    }
                }

                return;
            }


            // Parse this as a normal term
            String buffer = "";

            for (int i = 0; i < Text.Length; i++)
            {
                s = MatchAt(ref Text, i, allSymbols);

                if (s == null)
                {
                    buffer += Text[i];
                }
                else if (s == "(")
                {
                    int startI = i;
                    Utils.Balance(ref Text, ref i, ')');

                    if (buffer.Trim() != "")
                    {
                        string functionName = buffer.Trim();
                        buffer = "";

                        Term bracketTerm  = new Term(Text.Substring(startI + 1, i - startI - 1), TermTypes.PARAMETER_LIST);
                        Term functionTerm = Merge(new Term(functionName), bracketTerm);
                        functionTerm.Type = TermTypes.FUNCTION;

                        SubTerms.Add(functionTerm);
                    }
                    else
                    {
                        SubTerms.Add(new Term(Text.Substring(startI + 1, i - startI - 1)));
                    }
                }
                else if (s == "\"")
                {
                    int startI = i;
                    i       = Utils.FindEndOfString(Text, i + 1);
                    buffer += Text.Substring(startI, i - startI + 1);
                }
                else if (s == ":")
                {
                    int    end        = findEndOfSuffix(Text, i + 1);
                    String suffixName = Text.Substring(i + 1, end - i);
                    i += end - i;

                    if (buffer.Trim() != "")
                    {
                        SubTerms.Add(new Term(buffer.Trim()));
                        buffer = "";
                    }

                    if (SubTerms.Count > 0)
                    {
                        Term last = SubTerms.Last();
                        SubTerms.Remove(last);

                        Term structureTerm = Merge(last, new Term(suffixName, TermTypes.SUFFIX));
                        structureTerm.Type = TermTypes.STRUCTURE;
                        SubTerms.Add(structureTerm);
                    }
                }
                else if (s == "-")
                {
                    if (buffer.Trim() != "" ||
                        (SubTerms.Count > 0 && SubTerms.Last().Type != TermTypes.MATH_OPERATOR &&
                         SubTerms.Last().Type != TermTypes.COMPARISON_OPERATOR))
                    {
                        // Not a sign, treat as operator
                        if (buffer.Trim() != "")
                        {
                            SubTerms.Add(new Term(buffer.Trim()));
                        }
                        SubTerms.Add(new Term(s));

                        buffer = "";
                        i     += s.Length - 1;
                    }
                    else
                    {
                        buffer += Text[i];
                    }
                }
                else
                {
                    if (buffer.Trim() != "")
                    {
                        SubTerms.Add(new Term(buffer.Trim()));
                    }
                    SubTerms.Add(new Term(s));

                    buffer = "";
                    i     += s.Length - 1;
                }
            }

            // If there's only one term, we're done!
            if (SubTerms.Count == 0)
            {
                Type = TermTypes.FINAL;
                return;
            }

            if (buffer.Trim() != "")
            {
                SubTerms.Add(new Term(buffer));
            }

            // If I end up with exactly one subTerm, then I AM that subterm. Exception: If I already have a special type
            if (SubTerms.Count == 1 && this.Type == TermTypes.REGULAR)
            {
                Term child = SubTerms[0];
                SubTerms.Clear();

                CopyFrom(ref child);
            }
        }
示例#6
0
        public bool parseNext(ref string buffer, out string cmd, ref int lineCount, out int lineStart)
        {
            lineStart = -1;

            for (int i = 0; i < buffer.Length; i++)
            {
                string c = buffer.Substring(i, 1);

                if (lineStart < 0 && Regex.Match(c, "\\S").Success)
                {
                    lineStart = lineCount;
                }
                else if (c == "\n")
                {
                    lineCount++;
                }

                if (c == "\"")
                {
                    i = Utils.FindEndOfString(buffer, i + 1);

                    if (i == -1)
                    {
                        cmd = "";
                        return(false);
                    }
                }
                else if (c == ".")
                {
                    int pTest;
                    if (i == buffer.Length - 1 || int.TryParse(buffer.Substring(i + 1, 1), out pTest) == false)
                    {
                        cmd    = buffer.Substring(0, i);
                        buffer = buffer.Substring(i + 1).Trim();

                        return(true);
                    }
                }
                else if (c == "{")
                {
                    i = Utils.BraceMatch(buffer, i);

                    if (i == -1)
                    {
                        cmd = "";
                        return(false);
                    }
                    else
                    {
                        // Do you see a period after this right brace? If not, let's just pretend there is one ok?
                        if (!buffer.Substring(i + 1).StartsWith("."))
                        {
                            cmd    = buffer.Substring(0, i + 1);
                            buffer = buffer.Substring(i + 1).Trim();

                            return(true);
                        }
                    }
                }
            }

            cmd = "";
            return(false);
        }