Exemplo n.º 1
0
        private static string ExtractExecutableName(string cmdLine, out int argsPosition)
        {
            bool inQuotes = false;
            int  startIdx = 0;
            int  i;

            for (i = 0; i < cmdLine.Length; i++)
            {
                var symb = cmdLine[i];

                if (symb == '\"')
                {
                    if (inQuotes)
                    {
                        argsPosition = i + 1;
                        return(cmdLine.Substring(startIdx, i - startIdx));
                    }

                    inQuotes = true;
                    startIdx = i + 1;
                }
                else if (symb == ' ' && !inQuotes)
                {
                    argsPosition = i + 1;
                    return(cmdLine.Substring(startIdx, i - startIdx));
                }
            }

            if (inQuotes)
            {
                throw RuntimeException.InvalidArgumentValue();
            }

            argsPosition = i + 1;
            return(cmdLine.Substring(startIdx, i - startIdx));
        }
        /// <summary>
        /// Заменяет содержимое строки по номеру
        /// </summary>
        /// <param name="number">Номер заменяемой строки</param>
        /// <param name="newLine">Новое значение строки</param>
        public void ЗаменитьСтроку(int number, string newLine)
        {
            if (number > _lines.Count)
            {
                return;
            }

            if (number < 1)
            {
                throw RuntimeException.InvalidArgumentValue();
            }

            var newLines = ParseInputString(newLine);

            if (newLines.Count == 1)
            {
                _lines[number - 1] = newLines[0];
            }
            else
            {
                _lines[number - 1] = newLines[0];
                _lines.InsertRange(number, newLines.Skip(1));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Находит вхождение искомой строки как подстроки в исходной строке
        /// </summary>
        /// <param name="haystack">Строка, в которой ищем</param>
        /// <param name="needle">Строка, которую надо найти</param>
        /// <param name="direction">значение перечисления НаправлениеПоиска (с конца/с начала)</param>
        /// <param name="startPos">Начальная позиция, с которой начинать поиск</param>
        /// <param name="occurance">Указывает номер вхождения искомой подстроки в исходной строке</param>
        /// <returns>Позицию искомой строки в исходной строке. Возвращает 0, если подстрока не найдена.</returns>
        public static int Найти(string haystack, string needle, НаправлениеПоиска direction = НаправлениеПоиска.СНачала, int startPos = 0, int occurance = 0)
        {
            int len = haystack.Length;

            if (len == 0 || needle.Length == 0)
            {
                return(0);
            }

            bool fromBegin = direction == НаправлениеПоиска.СНачала;

            if (startPos == 0)
            {
                startPos = fromBegin ? 1 : len;
            }

            if (startPos < 1 || startPos > len)
            {
                throw RuntimeException.InvalidArgumentValue();
            }

            if (occurance == 0)
            {
                occurance = 1;
            }

            int startIndex = startPos - 1;
            int foundTimes = 0;
            int index      = len + 1;

            if (fromBegin)
            {
                while (foundTimes < occurance && index >= 0)
                {
                    index = haystack.IndexOf(needle, startIndex, StringComparison.Ordinal);
                    if (index >= 0)
                    {
                        startIndex = index + 1;
                        foundTimes++;
                    }
                    if (startIndex >= len)
                    {
                        break;
                    }
                }
            }
            else
            {
                while (foundTimes < occurance && index >= 0)
                {
                    index = haystack.LastIndexOf(needle, startIndex, StringComparison.Ordinal);
                    if (index >= 0)
                    {
                        startIndex = index - 1;
                        foundTimes++;
                    }
                    if (startIndex < 0)
                    {
                        break;
                    }
                }
            }

            if (foundTimes == occurance)
            {
                return(index + 1);
            }
            else
            {
                return(0);
            }
        }
Exemplo n.º 4
0
        public static object Parse(object obj, Type type)
        {
            string presentation = obj.ToString();
            object result;

            if (type == typeof(bool))
            {
                if (String.Compare(presentation, "истина", StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(presentation, "true", StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(presentation, "да", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    result = true;
                }
                else if (String.Compare(presentation, "ложь", StringComparison.OrdinalIgnoreCase) == 0 ||
                         String.Compare(presentation, "false", StringComparison.OrdinalIgnoreCase) == 0 ||
                         String.Compare(presentation, "нет", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    result = false;
                }
                else
                {
                    throw RuntimeException.ConvertToBooleanException();
                }
            }
            else if (type == typeof(DateTime))
            {
                string format;
                if (presentation.Length == 14)
                {
                    format = "yyyyMMddHHmmss";
                }
                else if (presentation.Length == 8)
                {
                    format = "yyyyMMdd";
                }
                else if (presentation.Length == 12)
                {
                    format = "yyyyMMddHHmm";
                }
                else
                {
                    throw RuntimeException.ConvertToDateException();
                }

                if (presentation == "00000000" ||
                    presentation == "000000000000" ||
                    presentation == "00000000000000")
                {
                    result = null;
                }
                else
                {
                    try
                    {
                        result = DateTime.ParseExact(presentation, format, System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                        throw RuntimeException.ConvertToDateException();
                    }
                }
            }
            else if (type == typeof(decimal))
            {
                var numInfo  = NumberFormatInfo.InvariantInfo;
                var numStyle = NumberStyles.AllowDecimalPoint
                               | NumberStyles.AllowLeadingSign
                               | NumberStyles.AllowLeadingWhite
                               | NumberStyles.AllowTrailingWhite;

                try
                {
                    result = Decimal.Parse(presentation, numStyle, CultureInfo.CurrentCulture);
                }
                catch (FormatException)
                {
                    throw RuntimeException.ConvertToNumberException();
                }
            }
            else if (type == typeof(string))
            {
                result = presentation;
            }
            else
            {
                throw new NotSupportedException("constant type is not supported");
            }

            return(result);
        }