コード例 #1
0
        ///// <summary>
        ///// Replaces all occurrences of a specified string, with another specified string.
        ///// Replacing is produced with provision for tokens.
        ///// </summary>
        ///// <param name="textValue">Text for replace.</param>
        ///// <param name="oldValue">A String to be replaced.</param>
        ///// <param name="newValue">A String to replace all occurrences of oldValue.</param>
        ///// <returns>Replaced string.</returns>
        //public static string Replace(string textValue, string oldValue, string newValue)
        //{
        //    StringBuilder sb = new StringBuilder(textValue);
        //    StiLexer lexer = new StiLexer(textValue);

        //    StiToken token = null;
        //    do
        //    {
        //        token = lexer.GetToken();
        //        if (token.Type == StiTokenType.Ident && ((string)token.Data) == oldValue)
        //        {
        //            sb = sb.Replace(oldValue, newValue, token.Index, token.Length);
        //            lexer.positionInText += newValue.Length;
        //        }

        //    }
        //    while (token.Type != StiTokenType.EOF);

        //    return sb.ToString();
        //}


        /// <summary>
        /// Replaces all occurrences of a specified String, with another specified String.
        /// Before oldValue must follow specified prefix - token.
        /// Replacing is produced with provision for tokens.
        /// </summary>
        /// <param name="textValue">Text for replace.</param>
        /// <param name="prefix">Prefix - token.</param>
        /// <param name="oldValue">A String to be replaced.</param>
        /// <param name="newValue">A String to replace all occurrences of oldValue.</param>
        /// <returns>Replaced string.</returns>
        public static string ReplaceWithPrefix(string textValue, string prefix, string oldValue, string newValue)
        {
            StringBuilder sb    = new StringBuilder(textValue);
            StiLexer      lexer = new StiLexer(textValue);

            StiToken prefixToken = lexer.GetToken();

            if (prefixToken.Type == StiTokenType.EOF)
            {
                return(textValue);
            }

            StiToken token = null;

            do
            {
                token = lexer.GetToken();
                if (token.Type == StiTokenType.Ident &&
                    prefixToken.Type == StiTokenType.Ident &&
                    ((string)prefixToken.Data) == prefix &&
                    ((string)token.Data) == oldValue)
                {
                    sb = sb.Replace(oldValue, newValue, token.Index, token.Length);
                    lexer.positionInText += newValue.Length;
                }
                prefixToken = token;
            }while (token.Type != StiTokenType.EOF);

            return(sb.ToString());
        }
コード例 #2
0
        public static List <StiToken> GetAllTokens(string str)
        {
            var tokens = new List <StiToken>();
            var lexer  = new StiLexer(str);

            while (true)
            {
                var token = lexer.GetToken();
                if (token == null || token.Type == StiTokenType.EOF)
                {
                    return(tokens);
                }
                tokens.Add(token);
            }
        }
コード例 #3
0
        public static bool IdentExists(string str, string name, bool caseSensitive)
        {
            var lexer = new StiLexer(str);

            while (true)
            {
                var token = lexer.GetToken();
                if (token == null || token.Type == StiTokenType.EOF)
                {
                    return(false);
                }
                if (token.Type == StiTokenType.Ident && token.Data != null)
                {
                    if (caseSensitive && token.Data.ToString() == name)
                    {
                        return(true);
                    }
                    if (!caseSensitive && token.Data.ToString().ToLowerInvariant() == name.ToLowerInvariant())
                    {
                        return(true);
                    }
                }
            }
        }