Example #1
0
        /// <summary>
        /// Splits the string into an array of individual words
        /// </summary>
        /// <param name="value">The value to split into words</param>
        /// <returns>An array of extracted words</returns>
        public static string[] SplitIntoWords(this string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(new string[] { });
            }

            // Trim and remove double spaces to avoid empty words
            value = value.Trim().Replace("  ", " ");

            var matchingWords = new List <string>();
            var wordBuilder   = new StringBuilder();
            var currentIndex  = 0;
            var previousChar  = '\0';

            foreach (char currentChar in value)
            {
                if (Char.IsLetterOrDigit(currentChar) || Char.IsSymbol(currentChar))
                {
                    wordBuilder.Append(currentChar);
                }
                else if (Char.IsWhiteSpace(currentChar) || Char.IsPunctuation(currentChar))
                {
                    if (wordBuilder.Length > 0)
                    {
                        var flushWord = true;

                        // Check ahead of the current position to see if the next character is a digit
                        // If the current digit is a number, the current punctuation is a full stop
                        // and the next digit is also a number, then treat it as part of the word
                        // (e.g. "1.50" would be treated as a whole word instead of "1" and "50")
                        if (Char.IsNumber(previousChar) && currentChar == '.' && (currentIndex + 1) < value.Length)
                        {
                            var nextChar = value[currentIndex + 1];

                            if (Char.IsNumber(nextChar))
                            {
                                wordBuilder.Append(currentChar);
                                flushWord = false;
                            }
                        }

                        // Flush the word into the matching words collection
                        if (flushWord)
                        {
                            matchingWords.Add(wordBuilder.ToString());
                            wordBuilder.Clear();
                        }
                    }
                }

                currentIndex++;
                previousChar = currentChar;
            }

            // Add anything not yet flushed to the words list
            if (wordBuilder.Length > 0)
            {
                matchingWords.Add(wordBuilder.ToString());
            }

            return(matchingWords.ToArray());
        }
Example #2
0
//        internal const int NoSpace=0x2000;

        public static int StringToInt(string value, int fromBase, int flags)
        {
            if ((flags & IsTight) == 0)
            {
                throw new NotImplementedException();
            }

            if (value == null)
            {
                return(0);
            }

            int  chars  = 0;
            uint result = 0;
            int  digitValue;

            int  i        = 0;
            int  len      = value.Length;
            bool negative = false;

            if (len == 0)
            {
                // Mimic broken .net behaviour
                throw new ArgumentOutOfRangeException("Empty string");
            }

            //Check for a sign
            if (value [i] == '-')
            {
                if (fromBase != 10)
                {
                    throw new ArgumentException("String cannot contain a minus sign if the base is not 10.");
                }

                if ((flags & TreatAsUnsigned) != 0)
                {
                    throw new OverflowException("Negative number");
                }

                negative = true;
                i++;
            }
            else if (value [i] == '+')
            {
                i++;
            }

            if (fromBase == 16 && i + 1 < len && value [i] == '0' && (value [i + 1] == 'x' || value [i + 1] == 'X'))
            {
                i += 2;
            }

            uint max_value;

            if ((flags & TreatAsI1) != 0)
            {
                max_value = Byte.MaxValue;
            }
            else if ((flags & TreatAsI2) != 0)
            {
                max_value = UInt16.MaxValue;
            }
            else
            {
                max_value = UInt32.MaxValue;
            }

            while (i < len)
            {
                char c = value [i++];
                if (Char.IsNumber(c))
                {
                    digitValue = c - '0';
                }
                else if (Char.IsLetter(c))
                {
                    digitValue = Char.ToLowerInvariant(c) - 'a' + 10;
                }
                else
                {
                    if (chars > 0)
                    {
                        throw new FormatException("Additional unparsable characters are at the end of the string.");
                    }

                    throw new FormatException("Could not find any parsable digits.");
                }

                if (digitValue >= fromBase)
                {
                    if (chars > 0)
                    {
                        throw new FormatException("Additional unparsable characters are at the end of the string.");
                    }

                    throw new FormatException("Could not find any parsable digits.");
                }

                var res = (uint)fromBase * result + (uint)digitValue;
                if (res < result || res > max_value)
                {
                    throw new OverflowException();
                }

                result = res;
                chars++;
            }

            if (chars == 0)
            {
                throw new FormatException("Could not find any parsable digits.");
            }

            return(negative ? -(int)result : (int)result);
        }