Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StringParser" /> class.
 /// </summary>
 /// <param name="naturalComparer">The natural comparer.</param>
 public StringParser(NaturalComparer naturalComparer)
 {
     m_NaturalComparer = naturalComparer;
 }
Ejemplo n.º 2
0
            private void ParseString()
            {
                int  start        = m_Idx;
                bool roman        = (m_NaturalComparer.m_NaturalComparerOptions & NaturalComparerOption.RomanNumber) != 0;
                int  romanValue   = 0;
                int  lastRoman    = int.MaxValue;
                int  cptLastRoman = 0;

                do
                {
                    if (roman)
                    {
                        int thisRomanValue = NaturalComparer.RomanLetterValue(m_CurChar);
                        if (thisRomanValue > 0)
                        {
                            bool handled = false;

                            if ((thisRomanValue == 1 || thisRomanValue == 10 || thisRomanValue == 100))
                            {
                                NextChar();
                                int nextRomanValue = NaturalComparer.RomanLetterValue(m_CurChar);
                                if (nextRomanValue == thisRomanValue * 10 | nextRomanValue == thisRomanValue * 5)
                                {
                                    handled = true;
                                    if (nextRomanValue <= lastRoman)
                                    {
                                        romanValue += nextRomanValue - thisRomanValue;
                                        NextChar();
                                        lastRoman    = thisRomanValue / 10;
                                        cptLastRoman = 0;
                                    }
                                    else
                                    {
                                        roman = false;
                                    }
                                }
                            }
                            else
                            {
                                NextChar();
                            }
                            if (!handled)
                            {
                                if (thisRomanValue <= lastRoman)
                                {
                                    romanValue += thisRomanValue;
                                    if (lastRoman == thisRomanValue)
                                    {
                                        cptLastRoman += 1;
                                        switch (thisRomanValue)
                                        {
                                        case 1:
                                        case 10:
                                        case 100:
                                            if (cptLastRoman > 4)
                                            {
                                                roman = false;
                                            }
                                            break;

                                        case 5:
                                        case 50:
                                        case 500:
                                            if (cptLastRoman > 1)
                                            {
                                                roman = false;
                                            }
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        lastRoman    = thisRomanValue;
                                        cptLastRoman = 1;
                                    }
                                }
                                else
                                {
                                    roman = false;
                                }
                            }
                        }
                        else
                        {
                            roman = false;
                        }
                    }
                    else
                    {
                        NextChar();
                    }
                    if (!char.IsLetter(m_CurChar))
                    {
                        break;                            // TODO: might not be correct. Was : Exit Do
                    }
                }while (true);
                m_StringValue = m_Source.Substring(start, m_Idx - start);
                if (roman)
                {
                    m_NumericalValue = romanValue;
                    m_TokenType      = NaturalComparer.TokenType.Numerical;
                }
                else
                {
                    m_TokenType = NaturalComparer.TokenType.String;
                }
            }