示例#1
0
        /// <summary>
        /// Creates a DynamicString instance from the given expression.
        /// </summary>
        /// <param name="expression">
        /// A string that have runtime resolved sections marked with the syntax "$(resolver-name)".
        /// For example: "The current time is $(get-current-time)".
        /// To specify a single literal '$' character in the expression, specify two '$' characters; that is,
        /// "$$".
        /// </param>
        /// <returns>The created DynamicString instance.</returns>
        public static DynamicString FromString(string expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            DynamicString newCfgString = new DynamicString();
            int           searchIdx    = 0;
            int           searchOffset = 0;

            while (searchIdx + searchOffset < expression.Length)
            {
                int dollarIdx = expression.IndexOf('$', searchIdx + searchOffset);
                if (dollarIdx == -1)
                {
                    /// No more runtime sections, add the remaining part of the expression to the
                    /// section list.
                    newCfgString.sections.Add(expression.Substring(searchIdx).Replace("$$", "$"));
                    newCfgString.runtimeFlags.Add(false);
                    break;
                }
                else
                {
                    /// Potentially runtime section found, we have to check it.
                    if (expression.Length > dollarIdx + 1 && expression[dollarIdx + 1] == '$')
                    {
                        /// No, it's not a runtime section, just one simple '$' character escaped.
                        searchOffset = dollarIdx + 2 - searchIdx;
                        continue;
                    }
                    else if (expression.Length > dollarIdx + 1 && expression[dollarIdx + 1] == '(')
                    {
                        /// We have found the beginning of a runtime section, so we have to store the
                        /// previous non-runtime section.
                        if (dollarIdx - searchIdx > 0)
                        {
                            newCfgString.sections.Add(
                                expression.Substring(searchIdx, dollarIdx - searchIdx).Replace("$$", "$"));
                            newCfgString.runtimeFlags.Add(false);
                        }

                        /// Now parse the runtime section.
                        int sectionEndIdx = expression.IndexOf(')', dollarIdx);
                        if (sectionEndIdx != -1 && sectionEndIdx - dollarIdx > 2)
                        {
                            /// End of the runtime section has been found
                            string runtimeSection = expression.Substring(dollarIdx + 2, sectionEndIdx - (dollarIdx + 2));
                            newCfgString.sections.Add(runtimeSection);
                            newCfgString.runtimeFlags.Add(true);
                            searchIdx    = sectionEndIdx + 1;
                            searchOffset = 0;
                        }
                        else
                        {
                            /// Syntax error
                            throw new ConfigurationException(string.Format("Syntax error in DynamicString expression: {0}", expression));
                        }
                    }
                    else
                    {
                        /// Syntax error
                        throw new ConfigurationException(string.Format("Syntax error in DynamicString expression: {0}", expression));
                    }
                }
            }

            return(newCfgString);
        }
示例#2
0
 /// <summary>
 /// Checks whether this DynamicString has the same value as the specified DynamicString.
 /// </summary>
 /// <param name="other">The DynamicString to test.</param>
 /// <returns>True if other DynamicString has the same value as this DynamicString.</returns>
 public bool Equals(DynamicString other)
 {
     return(other.Value == this.Value);
 }