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


C# TextReader.TryRead方法代码示例

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


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

示例1: ReadPropertyValue

        /// <summary>
        /// Reads a property value that supports quotes and escaping.
        /// </summary>
        /// <param name="reader">The text reader.</param>
        /// <returns>The property value as a string or null if there is no value.</returns>
        private string ReadPropertyValue(TextReader reader)
        {
            reader.SkipWhitespace();

            var result = new StringBuilder();
            bool singleQuotes = false;
            bool doubleQuotes = false;
            bool quoted = false;
            char c;

            // First character
            if (!reader.TryRead(out c))
                return null;
            ;

            if (c == '\'')
                singleQuotes = quoted = true;
            else if (c == '"')
                doubleQuotes = quoted = true;
            else
                result.Append(c);

            // Next characters
            do {
                if (!reader.TryRead(out c))
                    break;

                if (c == '\\') {
                    // Not yet implemented in OpenRCT2
                    // result.Append(ReadEscapedCharacter(reader));
                    result.Append(c);
                } else if ((c == ';' || c == '#') && !quoted) {
                    break;
                } else if (c == '\'' && singleQuotes) {
                    singleQuotes = false;
                    break;
                } else if (c == '"' && doubleQuotes) {
                    doubleQuotes = false;
                    break;
                } else {
                    result.Append(c);
                }
            } while (true);

            if (singleQuotes || doubleQuotes)
                throw new Exception("No end quote(s)");

            if (!quoted)
                return result.ToString().TrimEnd();

            return result.ToString();
        }
开发者ID:danidoedel,项目名称:OpenRCT2Launcher,代码行数:57,代码来源:IniConfiguration.cs

示例2: ParseGroup

        private string ParseGroup(TextReader reader, int lineNumber, char openCharacter, char closeCharacter)
        {
            if (!reader.TryReadExpected(openCharacter.ToString()))
            {
                _logger.LogError(lineNumber, $"Expected '{openCharacter}'");
                return null;
            }

            var sb = new StringBuilder();
            char c;
            bool missingEndBracket = true;
            while (reader.TryRead(out c))
            {
                if (c == closeCharacter)
                {
                    missingEndBracket = false;
                    break;
                }
                else
                {
                    sb.Append(c);
                }
            }

            if (missingEndBracket)
            {
                _logger.LogError(lineNumber, $"Group name did not end with '{closeCharacter}'");
                return null;
            }

            ParseWhitespace(reader, lineNumber, 0);

            return sb.ToString();
        }
开发者ID:trebla64,项目名称:Localisation,代码行数:34,代码来源:LanguageParser.cs

示例3: ReadEscapedCharacter

        /// <summary>
        /// Read an escaped character e.g. "\n". Must be performed after the backslash has been read.
        /// </summary>
        /// <param name="reader">The text reader.</param>
        /// <returns>The escaped character.</returns>
        private char ReadEscapedCharacter(TextReader reader)
        {
            char c;
            if (!reader.TryRead(out c))
                throw new Exception("Expected escape character");

            switch (c) {
            case '0':
                return '\0';
            case 't':
                return '\t';
            case 'r':
                return '\r';
            case 'n':
                return '\n';
            case 'x':
                char[] hexCode = new char[4];
                if (reader.ReadBlock(hexCode, 0, 4) != 4)
                    throw new Exception("Wrong format for escaped unicode character.");
                return (char)Int32.Parse(new string(hexCode), NumberStyles.AllowHexSpecifier);
            default:
                return c;
            }
        }
开发者ID:danidoedel,项目名称:OpenRCT2Launcher,代码行数:29,代码来源:IniConfiguration.cs


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