当前位置: 首页>>代码示例>>C#>>正文


C# Buffer.GetCharacter方法代码示例

本文整理汇总了C#中System.Buffer.GetCharacter方法的典型用法代码示例。如果您正苦于以下问题:C# Buffer.GetCharacter方法的具体用法?C# Buffer.GetCharacter怎么用?C# Buffer.GetCharacter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Buffer的用法示例。


在下文中一共展示了Buffer.GetCharacter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Scan

        static Token Scan(Buffer b, out bool error)
        {
            int startPosition = b.Position;
            char ch = b.GetCharacter();

            error = false;

            // CASE 1: Next token is a separator character
            if (ch == OpenParenthesis)
                return new Token(Token.Kind.Open, "(", startPosition);
            else if (ch == ClosedParenthesis)
                return new Token(Token.Kind.Close, ")", startPosition);
            if (ch == OpenBracket)
                return new Token(Token.Kind.LBracket, "(", startPosition);
            else if (ch == ClosedBracket)
                return new Token(Token.Kind.RBracket, ")", startPosition);
            else if (ch == Comma)
                return new Token(Token.Kind.Comma, ",", startPosition);
            // CASE 2: Next token is a quoted string
            else if (ch == QuoteChar)
            {
                StringBuilder stringToken = new StringBuilder();
                bool sawEscape = false;
                bool endOfString = false;
                while (!endOfString)
                {
                    ch = b.GetCharacter();
                    if (ch == EOF)
                    {
                        error = true;
                        return null;
                    }
                    else if (ch == Escape && !sawEscape)
                    {
                        sawEscape = true;
                    }
                    else if (sawEscape)  // to do: add && IsEscapedChar(ch)
                    {
                        sawEscape = false;
                        stringToken.Append(ch);
                    }
                    else if (ch == QuoteChar)
                    {
                        endOfString = true;
                    }
                    else
                    {
                        stringToken.Append(ch);
                    }
                }
                return new Token(Token.Kind.String, stringToken.ToString(), startPosition);
            }

            // Case 3: Integer
            else if (Char.IsDigit(ch) || (ch == Minus && !b.IsDelimited() && Char.IsDigit(b.Peek())))
            {
                StringBuilder intToken = new StringBuilder();
                intToken.Append(ch);

                while (!b.IsDelimited())
                {
                    ch = b.GetCharacter();
                    if (Char.IsDigit(ch))
                        intToken.Append(ch);
                    else
                    {
                        error = true;
                        return null;
                    }
                }
                return new Token(Token.Kind.Integer, intToken.ToString(), startPosition);
            }

            // Case 4: Function symbol
            else if (Char.IsLetterOrDigit(ch) || ch == AtSign)
            {
                StringBuilder symbolToken = new StringBuilder();
                symbolToken.Append(ch);

                while (!b.IsDelimited())
                {
                    ch = b.GetCharacter();
                    if (Char.IsLetterOrDigit(ch) || ch == Underscore)
                        symbolToken.Append(ch);
                    else
                    {
                        error = true;
                        return null;
                    }
                }

                string str = symbolToken.ToString();
                if (str == "true" || str == "false" || str == "True" || str == "False")
                    return new Token(Token.Kind.Boolean, str, startPosition);
                else if (str == "null")
                    return new Token(Token.Kind.Null, str, startPosition);
                else
                    return new Token(Token.Kind.Symbol, str, startPosition);
            }

//.........这里部分代码省略.........
开发者ID:juhan,项目名称:NModel,代码行数:101,代码来源:TermReader.cs


注:本文中的System.Buffer.GetCharacter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。