Example #1
0
        public FormatString FormatInput(string input)
        {
            Regex checkCharge = new Regex("^([^\\+\\- ]*)(\\+\\d?|\\-\\d?)?");

            Match  m                = checkCharge.Match(input);
            string rawFormula       = m.Groups[1].Value;
            int    rawFormulaLength = m.Groups[1].Value.Length;
            int    chargeLength     = 0;

            if (m.Groups[2].Success)
            {
                chargeLength = m.Groups[2].Value.Length;
            }

            FormatString result = new FormatString(input);

            if (chargeLength > 0)
            {
                result.SetFlagAt(rawFormulaLength, chargeLength, FormatFlags.Superscript);
            }

            FormatCompounds(rawFormula, result);

            Regex  swapCharge = new Regex("^([^\\+\\- ]*)(([\\+\\-])(\\d))?");
            string newContent = swapCharge.Replace(result.Content, "$1$4$3");

            return(new FormatString(newContent, result.FormatMask));
        }
Example #2
0
        private static int FormatCompounds(string input, FormatString result, int textPointer = 0)
        {
            Regex findCompondsAndGroups = new Regex("([A-Z][a-z]?\\d*)|(\\([^()]*(?:\\(.*\\))?[^()]*\\)\\d+)");

            MatchCollection matches = findCompondsAndGroups.Matches(input);

            foreach (Match m2 in matches)
            {
                if (m2.Groups[1].Success) //Compound
                {
                    foreach (char c in m2.Groups[1].Value.ToCharArray())
                    {
                        if (Char.IsNumber(c))
                        {
                            result.SetFlagAt(textPointer, FormatFlags.Subscript);
                        }
                        textPointer++;
                    }
                }
                else if (m2.Groups[2].Success) //Group
                {
                    Regex  extractQtyFromInnerText = new Regex("\\((.*)\\)(\\d*)");
                    Match  m3        = extractQtyFromInnerText.Match(m2.Groups[2].Value);
                    string innerText = m3.Groups[1].Value;
                    string qty       = "";
                    if (m3.Groups[2].Success)
                    {
                        qty = m3.Groups[2].Value;
                    }

                    textPointer++; //to skip "("
                    textPointer = FormatCompounds(innerText, result, textPointer);
                    textPointer++; //to skip ")"

                    for (int i = 0; i < qty.Length; i++)
                    {
                        result.SetFlagAt(textPointer, FormatFlags.Subscript);
                        textPointer++;
                    }
                }
            }
            return(textPointer);
        }
Example #3
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (base.Equals(obj))
            {
                return(true);
            }

            FormatString a = obj as FormatString;

            if (a == null)
            {
                return(false);
            }

            if (a.Content != this.Content)
            {
                return(false);
            }

            if (a.FormatMask.Count != this.FormatMask.Count)
            {
                return(false);
            }

            for (int i = 0; i < a.FormatMask.Count; i++)
            {
                if (a.FormatMask[i] != this.FormatMask[i])
                {
                    return(false);
                }
            }

            return(true);
        }