當前位置: 首頁>>代碼示例>>C#>>正文


C# Buffer.Peek方法代碼示例

本文整理匯總了C#中System.Buffer.Peek方法的典型用法代碼示例。如果您正苦於以下問題:C# Buffer.Peek方法的具體用法?C# Buffer.Peek怎麽用?C# Buffer.Peek使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Buffer的用法示例。


在下文中一共展示了Buffer.Peek方法的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.Peek方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。