コード例 #1
0
        /// <summary>
        /// Generates all subset <see cref="WordStructure"/> combinations of the <see cref="WordStructure"/> passed in.
        /// </summary>
        /// <param name="instr"></param>
        /// <param name="outstr"></param>
        private void GetCombinations(WordStructure instr, List <WordStructure> outstr)
        {
            var comps = instr.Components.ToList();

            for (int i = 0; i < comps.Count(); i++)
            {
                WordStructureComponent wsc = instr.Components[i];
                if (wsc.IsOptional)
                {
                    comps.RemoveAt(i);
                    var str = new WordStructure(comps.ToArray());
                    outstr.Add(str);
                    GetCombinations(str, outstr);
                    comps.Insert(i, wsc);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Parses a string into a <see cref="WordStructure"/> object.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static WordStructure Parse(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentNullException("str");
            }

            WordStructure structure = new WordStructure();

            bool isOptional = false;

            for (int i = 0; i < str.Length; i++)
            {
                var character = str.ElementAt(i);

                // check for special characters.
                switch (character)
                {
                case ParserSymbols.OPTIONAL_START:
                    isOptional = true;
                    continue;

                case ParserSymbols.OPTIONAL_END:
                    isOptional = false;
                    continue;
                }

                // build the component.
                WordStructureComponent component = new WordStructureComponent
                {
                    IsOptional = isOptional,
                    Symbol     = character
                };

                structure.Components.Add(component);
            }

            return(structure);
        }