public static string RemoveStopwords(this string source, IEnumerable <string> stopwords)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(source);
            }
            if (stopwords == null)
            {
                return(source);
            }
            //Just to make sure we deal with fixed collection
            stopwords = stopwords as IList <string> ?? stopwords.ToArray();
            if (!stopwords.Any())
            {
                return(source);
            }
            //TODO: if the need arises add aditional processing to escape special regex-related symbols inside stopword
            var regex   = new Regex(string.Join("|", stopwords.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => $@"(^{x}\W{{1}})|(\W{{1}}{x}\W{{1}})|(\W{{1}}{x}$)")), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            var matches = regex.Matches(source);

            if (matches.Count == 0)
            {
                return(source);
            }
            var result = new StringBuilder(source);

            //Sort matches in reversed order to remove them without spanning multiple garabage string objects
            foreach (var match in matches.Cast <Match>().OrderByDescending(x => x.Index))
            {
                var removeRange = FindRemoveRange(match);
                result.Remove(removeRange.Key, removeRange.Value - removeRange.Key + 1);
            }
            result.Trim().Replace("  ", " ");
            return(result.ToString());
        }
Exemplo n.º 2
0
        public void StringBuilder_Trim_TrimsEmpty()
        {
            var sb = new StringBuilder();

            sb.Trim();
            Assert.AreEqual(String.Empty, sb.ToString());
        }
        public void TrimTest()
        {
            StringBuilder sb = null;

            sb = new StringBuilder("   asd sdf  ");
            Assert.Equal("asd sdf", sb.Trim().ToString());
        }
Exemplo n.º 4
0
        public void Trim(string str, char c, string expected)
        {
            var actual = new StringBuilder(str);

            actual.Trim(c);
            Assert.Equal(expected, actual.ToString());
        }
Exemplo n.º 5
0
    public void TrimTests(string input, string expected)
    {
        var sb     = new StringBuilder(input);
        var result = sb.Trim();

        Assert.AreEqual(expected, result);
    }
Exemplo n.º 6
0
        public virtual StringBuilder ReplacePrepositions(StringBuilder buf)
        {
            Debug.Assert(buf != null);

            buf.SetFormat(" {0} ", Regex.Replace(buf.ToString(), @"\s+", " ").ToLower().Trim());

            buf = buf.Replace(" in to ", " into ");

            buf = buf.Replace(" inside ", " in ");

            buf = buf.Replace(" from in ", " fromin ");

            buf = buf.Replace(" on to ", " onto ");

            buf = buf.Replace(" on top of ", " on ");

            buf = buf.Replace(" from on ", " fromon ");

            buf = buf.Replace(" below ", " under ").Replace(" beneath ", " under ").Replace(" underneath ", " under ");

            buf = buf.Replace(" from under ", " fromunder ");

            buf = buf.Replace(" in back of ", " behind ");

            buf = buf.Replace(" from behind ", " frombehind ");

            return(buf.Trim());
        }
Exemplo n.º 7
0
        public void Trim()
        {
            var sb = new StringBuilder("   This is a test.   ");

            sb.Trim();
            Assert.Equal("This is a test.", sb.ToString());
        }
Exemplo n.º 8
0
        public void CanTrimStringBuilder()
        {
            var sb = new StringBuilder();

            var self = sb.Trim();

            Assert.Same(sb, self);
            Assert.Equal(0, sb.Length);

            sb.Clear().Append(" \t foo \r\n bar \n\r\n");
            Assert.Equal("foo \r\n bar", sb.Trim().ToString());

            sb.Clear().Append(" \t \n \v \f \r ");
            Assert.Empty(sb.Trim().ToString());

            Assert.Null(((StringBuilder)null).Trim());
        }
Exemplo n.º 9
0
        public void StringBuilderTrim_InputWithWhiteSpace_ReturnsTrimedString(string input, string expectedOutput)
        {
            StringBuilder inputSB = new StringBuilder(input);

            inputSB.Trim();

            StringAssert.AreEqualIgnoringCase(inputSB.ToString(), expectedOutput);
        }
Exemplo n.º 10
0
        public void StringBuilder_Trim_ThrowsArgumentNullExceptionWhenSourceIsNull()
        {
            // Arrange
            StringBuilder source = null;

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => source.Trim());
        }
Exemplo n.º 11
0
        public void StringBuilder_Trim_ThrowsArgumentNullExceptionWhenTrimCharsIsNull()
        {
            // Arrange
            StringBuilder source = new StringBuilder();

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => source.Trim(null));
        }
Exemplo n.º 12
0
        public void StringBuilder_TrimChars_TrimsStartAndEnd()
        {
            var sb = new StringBuilder();

            sb.Append("ABTestAB");

            sb.Trim('A', 'B');
            Assert.AreEqual("Test", sb.ToString());
        }
Exemplo n.º 13
0
        public void StringBuilder_Trim_TrimsStartAndEnd()
        {
            var sb = new StringBuilder();

            sb.Append(" \r\n\tTest\t\r\n ");

            sb.Trim();
            Assert.AreEqual("Test", sb.ToString());
        }
Exemplo n.º 14
0
        public void StringBuilder_Trim_SucceedsWhenNoTrailingChars()
        {
            var sb = new StringBuilder();

            sb.Append("\t\r\n Test");

            sb.Trim();
            Assert.AreEqual("Test", sb.ToString());
        }
Exemplo n.º 15
0
        public void StringBuilder_Trim_ReturnsTheSameInstance()
        {
            // Arrange
            StringBuilder source = new StringBuilder("\"foo\"");
            // Act
            var result = source.Trim('\"');

            // Assert
            Assert.AreSame(source, result);
        }
			/// <summary>
			/// Constructs a WHERE statements on this column for the given <paramref name="values"/>.
			/// </summary>
			/// <param name="context">The <see cref="IMansionContext"/>.</param>
			/// <param name="commandContext">The <see cref="QueryCommandContext"/>.</param>
			/// <param name="pair">The <see cref="TableColumnPair"/>.</param>
			/// <param name="values">The values on which to construct the where statement.</param>
			protected override void DoToWhereStatement(IMansionContext context, QueryCommandContext commandContext, TableColumnPair pair, IList<object> values)
			{
				// assemble the properties
				var buffer = new StringBuilder();
				foreach (var value in values)
					buffer.AppendFormat("@{0},", commandContext.Command.AddParameter(value));

				// append the query
				commandContext.QueryBuilder.AppendWhere(" [{0}].[id] IN ( SELECT [{1}].[id] FROM [{1}] WHERE [{1}].[name] = '{2}' AND [{1}].[value] IN ({3}) )", commandContext.QueryBuilder.RootTableName, pair.Table.Name, PropertyName, buffer.Trim());
			}
Exemplo n.º 17
0
        public static string ClearComments(this string html)
        {
            html = html.CleanHeader();

            var starts = new List <int>();

            for (var i = 0; i < html.Length; i++)
            {
                if (i >= html.Length - 4)
                {
                    break;
                }

                i = html.IndexOf(@"<!--", i, StringComparison.Ordinal);
                if (i == -1)
                {
                    break;
                }
                starts.Add(i);
            }

            var ends = starts.Select(start => html.IndexOf(@"-->", start, StringComparison.Ordinal) + 3).ToList();

            var content = new StringBuilder(html).ToString();

            //Enable cleaning mso styling
            content = starts.Select((t, i) => html.Substring(t, ends[i] - t)).Aggregate(content, (current, comment) => current.Replace(comment, ""));

            content = content.Replace(@"<![if !vml]>", "");
            content = content.Replace(@"<![endif]>", "");



            content = content.Substring(content.IndexOf("<body"));
            content = content.Substring(content.IndexOf(">") + 1);
            content = content.Remove(content.LastIndexOf("</body>"), content.Length - content.LastIndexOf("</body>"));


            //deleting index from description
            if (content.Contains("<div style='mso-element:comment-list'>"))
            {
                content = content.Remove(content.IndexOf("<div style='mso-element:comment-list'>"));
            }

            for (int i = 0; ; i++)
            {
                if (!content.Contains(">["))
                {
                    break;
                }
                //content = content.Remove(content.IndexOf(">[")+1, 5);
                content = content.Remove(content.IndexOf(">[") + 1, (content.IndexOf("]</a>") + 1) - (content.IndexOf(">[") + 1));
            }
            return(content.Trim());
        }
Exemplo n.º 18
0
        public void StringBuilder_Trim_RemovesSingleCharacterFromStartAndEnd()
        {
            // Arrange
            StringBuilder source = new StringBuilder("\"foo\"");

            // Act
            source.Trim('\"');
            // Assert
            Assert.AreEqual(3, source.Length);
            Assert.AreEqual("foo", source.ToString());
        }
Exemplo n.º 19
0
        public void StringBuilder_Trim_RemovesMultipleTrimCharactersFromStartAndEnd()
        {
            // Arrange
            StringBuilder source = new StringBuilder("\"*foo*\"");

            // Act
            source.Trim('\"', '*');
            // Assert
            Assert.AreEqual(3, source.Length);
            Assert.AreEqual("foo", source.ToString());
        }
Exemplo n.º 20
0
    // reasonable to assume you will use this everywhere, not just
    // Sql statements, but log statements, anywhere you need to
    // dump a list into a readable format!
    //
    // HINT: extra credit: you can generalize this, and provide
    // specialized short hands that invoke the general method
    public static string ToCommaSeparatedString <T>(this IEnumerable <T> values)
    {
        StringBuilder commaSeparated = new StringBuilder();

        foreach (T value in values)
        {
            // PERF: store format string as const
            commaSeparated.AppendFormat("{0}, ", value);
        }
        // PERF: store trim chars as static readonly array
        return(commaSeparated.Trim(", ".ToCharArray()));
    }
Exemplo n.º 21
0
        [Test] public void StringBuilderExtensions()
        {
            const string s = "  \t\nA string XXX\n  \t  ";

            {            // Trim start
                var sb = new StringBuilder(s);
                Assert.Equal(s.TrimStart(' ', '\t', '\n', 'X'), sb.TrimStart(' ', '\t', '\n', 'X').ToString());
            }
            {            // Trim end
                var sb = new StringBuilder(s);
                Assert.Equal(s.TrimEnd(' ', '\t', '\n', 'X'), sb.TrimEnd(' ', '\t', '\n', 'X').ToString());
            }
            {            // Trim
                var sb = new StringBuilder(s);
                Assert.Equal(s.Trim(' ', '\t', '\n', 'X'), sb.Trim(' ', '\t', '\n', 'X').ToString());
            }
            {            // Substring
                var sb = new StringBuilder(s);
                Assert.Equal(sb.Substring(6, 6), "string");
                Assert.Equal(sb.Substring(0, 3), "  \t");
                Assert.Equal(sb.Substring(13, sb.Length - 13), "XXX\n  \t  ");
            }
            {            // Remove
                var sb = new StringBuilder(s);
                int removed;
                sb.Remove(' ', out removed);
                Assert.Equal(sb.ToString(), "\t\nAstringXXX\n\t");
                Assert.Equal(removed, 8);
            }
            {            // StartsWith
                var sb = new StringBuilder(s);
                Assert.Equal(sb.StartsWith("  \t\n"), true);
                Assert.Equal(sb.StartsWith("A string", 4), true);
                Assert.Equal(sb.StartsWith("A string  ", 4), false);

                sb.Length = 0;
                Assert.Equal(sb.StartsWith("1"), false);
                Assert.Equal(sb.StartsWith(""), true);
            }
            {            // IndexOf
                var sb = new StringBuilder(s);
                Assert.Equal(sb.IndexOf('a', ignore_case: true), 4);
                Assert.Equal(sb.IndexOf('X', 1, 4), -1);
                Assert.Equal(sb.IndexOf("xx", ignore_case: true), 13);
                Assert.Equal(sb.IndexOf("XXXX"), -1);
                Assert.Equal(sb.IndexOf("XXX", 14, sb.Length - 14), -1);

                sb.Length = 0;
                Assert.Equal(sb.IndexOf("1"), -1);
                Assert.Equal(sb.IndexOf(""), 0);
            }
        }
        private string FormatMessage(Message message)
        {
            var buffer = new StringBuilder();

            buffer
            .AppendLine(FormatMessageHeader(message))
            .AppendLineIfNotNullOrWhiteSpace(FormatMarkdown(message.Content))
            .AppendLine()
            .AppendLineIfNotNullOrWhiteSpace(FormatAttachments(message.Attachments))
            .AppendLineIfNotNullOrWhiteSpace(FormatEmbeds(message.Embeds))
            .AppendLineIfNotNullOrWhiteSpace(FormatReactions(message.Reactions));

            return(buffer.Trim().ToString());
        }
Exemplo n.º 23
0
        public static string FormatMessage(RenderContext context, Message message)
        {
            var buffer = new StringBuilder();

            buffer
            .AppendLine(FormatMessageHeader(context, message))
            .AppendLineIfNotEmpty(FormatMessageContent(context, message))
            .AppendLine()
            .AppendLineIfNotEmpty(FormatAttachments(context, message.Attachments))
            .AppendLineIfNotEmpty(FormatEmbeds(context, message.Embeds))
            .AppendLineIfNotEmpty(FormatReactions(context, message.Reactions));

            return(buffer.Trim().ToString());
        }
Exemplo n.º 24
0
 public static void SerializeHDMap(HDMap map, out StringBuilder sb)
 {
     sb = new StringBuilder();
     Ros.Bridge.SerializeInternal(1, sb, map.GetType(), map, sType: Ros.SerialType.HDMap);
     sb.Trim();
     if (sb[0] == '{')
     {
         sb.Remove(0, 1);
     }
     if (sb[sb.Length - 1] == '}')
     {
         sb.Remove(sb.Length - 1, 1);
     }
 }
Exemplo n.º 25
0
    // reasonable to assume you will use this everywhere, not just
    // Sql statements, but log statements, anywhere you need to
    // dump a list into a readable format!
    //
    // HINT: extra credit: you can generalize this, and provide
    // specialized short hands that invoke the general method
    public static string ToCommaSeparatedString <T>(this IEnumerable <T> values)
    {
        // SIGH: so apparently this does not generate minimal
        // assembler on every machine, please note the following
        // is written for clarity, please feel free to substitute
        // your own favourite ultra-performance high-octance
        // string appender algorithm
        StringBuilder commaSeparated = new StringBuilder();

        foreach (T value in values)
        {
            // PERF: store format string as const
            commaSeparated.AppendFormat("{0}, ", value);
        }
        // PERF: store trim chars as static readonly array
        return(commaSeparated.Trim(", ".ToCharArray()));
    }
Exemplo n.º 26
0
        public static string CleanIdentifier(string input)
        {
            var sb = new StringBuilder(Regex.Replace(input, CLEAN_PATTERN, "_"));

            sb.Push(c => c.Equals('_') || Char.IsNumber(c));
            sb.Trim(" ");
            var result = sb.ToString();

            if (result.Equals(String.Empty) || result.All(c => c.Equals('_') || Char.IsNumber(c)))
            {
                result = "I" + input.GetHashCode().ToString(CultureInfo.InvariantCulture).Replace("-", "0");
            }
            if (!input.Equals(result))
            {
                //logger.Debug("Using '{0}' to identify field '{1}'.", result, input);
            }
            return(sb.ToString());
        }
Exemplo n.º 27
0
        private static string CreateValidSlug(string value)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
            {
                return(Guid.NewGuid().ToString());
            }

            var builder = new StringBuilder(value);

            foreach (string invalidChar in InvalidChars)
            {
                builder.Replace(invalidChar, string.Empty);
            }

            builder.Trim().Replace("  ", " ").Replace(" ", "-");

            return(builder.ToString().ToLowerInvariant());
        }
Exemplo n.º 28
0
        public override IEnumerable <Row> Execute(IEnumerable <Row> rows)
        {
            var line     = 1;
            var isBinary = _fileInfo.Extension == ".xls";

            using (var fileStream = File.Open(_fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                using (var reader = isBinary ? ExcelReaderFactory.CreateBinaryReader(fileStream) : ExcelReaderFactory.CreateOpenXmlReader(fileStream)) {
                    if (reader == null)
                    {
                        yield break;
                    }

                    var emptyBuilder = new StringBuilder();

                    while (reader.Read())
                    {
                        line++;

                        if (line > _start)
                        {
                            if (_end == 0 || line <= _end)
                            {
                                var row = new Row();
                                row["TflFileName"] = _fileInfo.FullName;
                                emptyBuilder.Clear();
                                foreach (var field in _fields)
                                {
                                    var value = reader.GetValue(field.Index);
                                    row[field.Alias] = value;
                                    emptyBuilder.Append(value);
                                }
                                emptyBuilder.Trim(" ");
                                if (!emptyBuilder.ToString().Equals(string.Empty))
                                {
                                    yield return(row);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 29
0
        public override IRow Transform(IRow row)
        {
            var context = new VelocityContext();

            foreach (var field in _input)
            {
                context.Put(field.Alias, row[field]);
            }

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb)) {
                NVelocity.App.Velocity.Evaluate(context, sw, _templateName, Context.Transform.Template);
                sw.Flush();
            }

            sb.Trim(' ', '\n', '\r');
            row[Context.Field] = Context.Field.Convert(sb.ToString());

            Increment();
            return(row);
        }
        public IEnumerable <object[]> Read()
        {
            // todo: fix duplication here

            using (var fileStream = File.Open(_fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                var isBinary = _fileInfo.Extension.ToLower() == ".xls";
                using (var reader = isBinary ? ExcelReaderFactory.CreateBinaryReader(fileStream) : ExcelReaderFactory.CreateOpenXmlReader(fileStream)) {
                    if (reader == null)
                    {
                        yield break;
                    }

                    var emptyDetector = new StringBuilder();

                    while (reader.Read())
                    {
                        emptyDetector.Clear();
                        var row = new List <object>();
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            if (i == _lines)
                            {
                                break;
                            }
                            var value = reader.GetValue(i);
                            row.Add(value);
                            emptyDetector.Append(value);
                        }
                        emptyDetector.Trim(" ");
                        if (!emptyDetector.ToString().Equals(string.Empty))
                        {
                            yield return(row.ToArray());
                        }
                    }
                }
            }
        }
Exemplo n.º 31
0
		string GetComments()
		{
			var sb = new StringBuilder ();

			foreach (var c in Lexer.Comments)
			{
				if (c.CommentType.HasFlag(Comment.Type.Documentation))
					sb.AppendLine(c.CommentText);
			}

			TrackerVariables.Comments.AddRange(Lexer.Comments);
			Lexer.Comments.Clear();

			sb.Trim ();

			if (sb.Length == 0)
				return string.Empty;

			// Overwrite only if comment is not 'ditto'
			if (sb.Length != 5 || sb.ToString().ToLowerInvariant() != "ditto")
				PreviousComment = sb;

			return PreviousComment.ToString();
		}
Exemplo n.º 32
0
        /// <summary>
        /// Prepares simulated input data as string
        /// </summary>
        protected virtual string PrepareSimulatedData()
        {
            StringBuilder inputData = new StringBuilder();
            StringBuilder tempLine  = new StringBuilder();

            Receivers.ToList().ForEach(r => inputData.Append(r.Location).Append(PublicFields.PositionSeparator));
            inputData = inputData.Trim(PublicFields.PositionSeparator);
            inputData.Append('\n');
            var times = ConvertingHelper.ConvertPointsToPropagationTimes(Receivers, Points);

            foreach (var timeLine in times)
            {
                tempLine.Clear();
                foreach (var time in timeLine)
                {
                    tempLine.Append(time.ToString("0.00000000", CultureInfo.InvariantCulture)).Append(PublicFields.PositionSeparator);
                }
                tempLine = tempLine.Trim(PublicFields.PositionSeparator);
                tempLine.Append('\n');
                inputData.Append(tempLine);
            }

            return(inputData.ToString());
        }
Exemplo n.º 33
0
        /// <summary>
        /// Returns the pre- and post-declaration comment
        /// </summary>
        /// <returns></returns>
        string CheckForPostSemicolonComment()
        {
            if (t == null)
                return string.Empty;

            int ExpectedLine = t.Line;

            var ret = new StringBuilder ();

            int i=0;
            foreach (var c in Lexer.Comments)
            {
                if (c.CommentType.HasFlag(Comment.Type.Documentation))
                {
                    // Ignore ddoc comments made e.g. in int a /** ignored comment */, b,c;
                    // , whereas this method is called as t is the final semicolon
                    if (c.EndPosition <= t.Location)
                    {
                        i++;
                        Comments.Add(c);
                        continue;
                    }
                    else if (c.StartPosition.Line > ExpectedLine)
                        break;

                    ret.AppendLine(c.CommentText);
                }

                i++;
                Comments.Add(c);
            }
            Lexer.Comments.RemoveRange(0, i);

            if (ret.Length == 0)
                return string.Empty;

            ret.Trim();

            // Add post-declaration string if comment text is 'ditto'
            if (ret.Length == 5 && ret.ToString().ToLowerInvariant() == "ditto")
                return PreviousComment.ToString();

            // Append post-semicolon comment string to previously read comments
            if (PreviousComment.Length != 0) // If no previous comment given, do not insert a new-line
                return (PreviousComment = ret).ToString();

            ret.Insert (0, Environment.NewLine);

            PreviousComment.Append(ret.ToString());
            return ret.ToString();
        }
Exemplo n.º 34
0
		/// <summary>
		/// Constructs a WHERE statements on this column for the given <paramref name="values"/>.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="commandContext">The <see cref="QueryCommandContext"/>.</param>
		/// <param name="pair">The <see cref="TableColumnPair"/>.</param>
		/// <param name="values">The values on which to construct the where statement.</param>
		protected override void DoToWhereStatement(IMansionContext context, QueryCommandContext commandContext, TableColumnPair pair, IList<object> values)
		{
			// add the table to the query
			commandContext.QueryBuilder.AddTable(context, pair.Table, commandContext.Command);

			// check for single or multiple values
			if (values.Count == 1)
				commandContext.QueryBuilder.AppendWhere(" [{0}].[{1}] = @{2}", pair.Table.Name, pair.Column.ColumnName, commandContext.Command.AddParameter(GetValue(context, values[0])));
			else
			{
				// start the clause
				var buffer = new StringBuilder();
				buffer.AppendFormat("[{0}].[{1}] IN (", pair.Table.Name, pair.Column.ColumnName);

				// loop through all the values
				foreach (var value in values.Select(val => GetValue(context, val)))
					buffer.AppendFormat("@{0},", commandContext.Command.AddParameter(value));

				// finish the clause
				commandContext.QueryBuilder.AppendWhere("{0})", buffer.Trim());
			}
		}
 public void TrimTest()
 {
     StringBuilder sb = null;
     sb = new StringBuilder("   asd sdf  ");
     Assert.Equal(sb.Trim().ToString(), "asd sdf");
 }
Exemplo n.º 36
0
        /// <summary>
        /// Пытается очистить указанную строку, представляющую название файла (без пути), от недопустимых символов, если таковые присутствуют. 
        /// Если очистка успешна, возвращает 'true' и очищенное коректное имя файла через выводной параметр. 
        /// Если же указанную строку невозможно очистить, возвращает 'false' и NULL через выводной параметр.
        /// </summary>
        /// <param name="Input">Строка, представляющая имя файла без пути</param>
        /// <param name="FixedFilename">Выводной параметр, содержащий очищенное имя файла, если его удалось очистить</param>
        /// <returns></returns>
        public static Boolean TryCleanFilename(String Input, out String FixedFilename)
        {
            FixedFilename = null;
            if (Input.HasVisibleChars() == false) { return false; }
            StringBuilder sb = new StringBuilder(Input.Length);
            Char[] invalid_chars = Path.GetInvalidFileNameChars();
            foreach (Char current in Input)
            {
                if (current.IsIn(invalid_chars) == false)
                {
                    sb.Append(current);
                }
            }
            String output = sb.Trim().ToString();
            if (output.HasVisibleChars() == false) { return false; }

            String[] all_segments = output.Split(new char[1] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            String first_segment = all_segments.First();
            if (first_segment.IsIn(StringComparison.OrdinalIgnoreCase, FilePathTools.IllegalFilenames) == true)
            {
                if (all_segments.Length < 3) { return false; }
                else
                {
                    output = output.TrimStart(first_segment + ".", StringComparison.OrdinalIgnoreCase, false);
                }
            }
            FixedFilename = output;
            return true;
        }
Exemplo n.º 37
0
		/// <summary>
		/// Форматирование текста.
		/// <b>НЕПОТОКОБЕЗОПАСНЫЙ!</b>
		/// </summary>
		/// <param name="txt">Исходный текст.</param>
		/// <param name="smile">Признак обработки смайликов.</param>
		/// <param name="doNotReplaceTags">Не заменять служебные символы HTML.</param>
		/// <param name="doNotFormatImplicitLinks">Не форматировать явно не указанные ссылки.</param>
		/// <returns>Сформатированный текст.</returns>
		public virtual string Format(
			string txt,
			bool smile,
			bool doNotReplaceTags,
			bool doNotFormatImplicitLinks)
		{
			var sb = new StringBuilder(txt);

			sb.Trim(TrimArray);

			if (sb.IsEmpty())
				return "";

			// Внимание! Порядок преобразования ВАЖЕН.
			//

			// Замена  небезопасных символов
			if (!doNotReplaceTags)
				sb = sb.ReplaceTagsWQ();

			// Приведение всех типов концов строк к \n
			//
			sb = _rxNewLineUnifier.Replace(sb, "\n");

			// Обработка исходных кодов и тегов, 
			// которые не могут быть внутри исходников.
			//

			// temporary remove [code...] tags
			const string codeExpression = "$$code{0}$$";
			var codeMatcher = new Matcher(codeExpression);
			sb = _rxCodeFormatting.Replace(sb, codeMatcher.Match);

			// temporary remove [img] tags
			const string imgExpression = "$$img{0}$$";
			var imgMatcher = new Matcher(imgExpression);
			sb = _imgTagRegex.Replace(sb, imgMatcher.Match);

			// temporary remove [url] & [purl] tags
			const string urlExpression = "$$url{0}$$";
			var urlMatcher = new Matcher(urlExpression);
			sb = _urlTagRegex.Replace(sb, urlMatcher.Match);

			// temporary remove implicit links
			const string implicitUrlExpression = "$$iurl{0}$$";
			var implicitUrlMatcher = new Matcher(implicitUrlExpression);
			if (!doNotFormatImplicitLinks)
				sb = _urlRegex.Replace(sb, implicitUrlMatcher.Match);

			// temporary remove [q] tags
			const string quoteExpression = "$$quote{0}$$";
			var quoteMatcher = new Matcher(quoteExpression);
			sb = _rxPrep12.Replace(sb, quoteMatcher.Match);

			// temporary remove [cut] tags
			const string cutExpression = "$$cut{0}$$";
			Matcher cutMatcher;
			do
			{
				cutMatcher = new Matcher(cutExpression);
				sb = _rxPrep13.Replace(sb, cutMatcher.Match);

				// Цитирование.
				sb = _rxTextUrl09.Replace(sb,
					m => $"<span class='lineQuote level{WebUtility.HtmlDecode(m.Groups["lev"].Value).Length}'>{m.Groups[0].Value}</span>");

				// restore & transform [cut] tags
				for (var i = 0; i < cutMatcher.Count; i++)
				{
					var m = cutMatcher[i];
					var capt = String.IsNullOrEmpty(m.Groups[3].Value) ? "Скрытый текст" : m.Groups[3].Value;
					sb = sb.Replace(String.Format(cutExpression, i),
						_hiddenTextSnippet.Replace("%CAPT%", capt).Replace("%TEXT%", m.Groups[4].Value).
						Replace("%URL%", GetImagePrefix()));
				}
			} while (cutMatcher.Count > 0);

			// restore & transform [q] tags
			// Цитирование [q].
			// http://www.rsdn.ru/forum/?mid=111506
			for (var i = 0; i < quoteMatcher.Count; i++)
				sb =
					sb.Replace(
						string.Format(quoteExpression, i),
						$"<blockquote class='q'><p>{quoteMatcher[i].Groups[1]}</p></blockquote>");

			// Обработка смайликов с учетом отмены и http://www.rsdn.ru/forum/?mid=184751
			if (smile)
			{
				var prefix = GetImagePrefix();
				sb = _smileReplacers.Aggregate(
					sb,
					(current, replacer) => replacer.Replace(current, prefix));
			}

			// ISBN
			sb = 
				_isbnDetector.Replace(
					sb,
					match =>
					{
						var isbn = new StringBuilder(match.Length);
						foreach (Capture capture in match.Groups["isbn"].Captures)
						{
							isbn.Append(capture.Value).Append('-');
						}
						if (isbn.Length > 0)
							isbn.Length--;
						return ProcessISBN(match, isbn.ToString());
					});

			// restore & transform [url] and [purl] tags
			for (var i = 0; i < urlMatcher.Count; i++)
			{
				var url =
					urlMatcher[i]
						.Groups["url"]
						.Value;
				var tag = urlMatcher[i].Groups["tag"].Value;

				// если url и tag перепутаны:
				//
				if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
					// если tag не пустой
					//
					if (!String.IsNullOrEmpty(tag))
						// если tag правильный Uri
						//
						if (Uri.IsWellFormedUriString(tag, UriKind.RelativeOrAbsolute))
						{
							// 
							//
							var temp = tag;
							tag = url;
							url = temp;
						}

				url = url.Replace("&amp;", "&"); // Returns escaped ampersands

				sb = sb.Replace(
					string.Format(urlExpression, i),
					ProcessURLs(url, tag));
			}

			// restore & transform implicit links
			for (var i = 0; i < implicitUrlMatcher.Count; i++)
				sb = sb.Replace(
					string.Format(implicitUrlExpression, i),
					ProcessImplicitURLs(implicitUrlMatcher[i]));

			// restore & transform [img] tags
			for (var i = 0; i < imgMatcher.Count; i++)
				sb = sb.Replace(
					string.Format(imgExpression, i),
					ImagesDelegate(this, imgMatcher[i]));

			// RSDN links
			sb = _rsdnLinkDetector.Replace(sb, ProcessRsdnLink);

			// [email]
			sb = _emailTagRegex.Replace(sb, ProcessEmailLink);

			// Replace hyphen to dash
			sb = _dashDetector.Replace(sb, "&mdash;");

			// [tagline]
			sb = _taglineDetector.Replace(sb, "<div class='tagline'>$1</div>");

			// [list]
			sb = _rxPrep06.Replace(sb, @"<ul style='margin-top:0; margin-bottom:0;'>$1</ul>");

			// [list=..]
			sb = _rxPrep07.Replace(sb, ListEvaluator);

			// [*]
			sb = _rxPrep08.Replace(sb, "<li />");

			// [hr]
			sb = _rxPrep09.Replace(sb, "<hr />");

			// Q12345(6)
			sb = _rxPrep10.Replace(
				sb,
				@"<a target='_blank' class='m' href='http://support.microsoft.com/default.aspx?scid=kb;EN-US;$1'>$1</a>");

			// Сообщение модератора.
			sb = _moderatorDetector.Replace(sb, "<div class='mod'>$1</div>");

			// Table
			sb = _rxTable.Replace(
				sb,
				"<table class='formatter' border='0' cellspacing='2' cellpadding='5'>$1</table>");
			sb = _rxTableRow.Replace(sb, "<tr class='formatter'>$1</tr>");
			sb = _rxTableHeader.Replace(sb, "<th class='formatter'>$1</th>");
			sb = _rxTableColumn.Replace(sb, "<td class='formatter'>$1</td>");

			// Headers
			sb = HeadersRegex.Replace(sb, "<h$2 class='formatter'>$1</h$2>");

			// Добавляем в конец каждой строки <br />,
			// но не после </table>, </div>, </ol>, </ul>, <blockquote> (возможно внутри <span>)
			// и не в самом конце текста
			sb = _rxNewLines.Replace(sb, "<br />$0");

			sb = _inlineTagReplacers.Aggregate(sb, (cur, replacer) => replacer(cur));

			// Ссылки на MSDN.
			sb = _rxMsdn.Replace(sb, DoMSDNRef);

			// Нужно для отмены тэгов и отмены смайликов.
			sb = _rxPrep01.Replace(sb, "");
			sb = _rxPrep02.Replace(sb, "");
			sb = _rxPrep03.Replace(sb, "");

			// restore & transform [code] tags
			for (var i = 0; i < codeMatcher.Count; i++)
			{
				// code coloring
				var code = PaintCode(codeMatcher[i]);

				// bold & italic formatting inside code
				// without checking canceling tag syntax
				code = _inlineTagReplacersNoChecks.Aggregate(code, (cur, replacer) => replacer(cur));

				sb = sb.Replace(string.Format(codeExpression, i), code.ToString());
			}

			return sb.ToString();
		}
Exemplo n.º 38
0
        public static string ClearComments(this string html)
        {
            html = html.CleanHeader();

            var starts = new List<int>();
            for (var i = 0; i < html.Length; i++)
            {
                if (i >= html.Length - 4)
                {
                    break;
                }

                i = html.IndexOf(@"<!--", i, StringComparison.Ordinal);
                if (i == -1)
                {
                    break;
                }
                starts.Add(i);
            }

            var ends = starts.Select(start => html.IndexOf(@"-->", start, StringComparison.Ordinal) + 3).ToList();

            var content = new StringBuilder(html).ToString(); 
            //Enable cleaning mso styling
            content = starts.Select((t, i) => html.Substring(t, ends[i] - t)).Aggregate(content, (current, comment) => current.Replace(comment, ""));

            content = content.Replace(@"<![if !vml]>", "");
            content = content.Replace(@"<![endif]>", "");




            content = content.Substring(content.IndexOf("<body"));
            content = content.Substring(content.IndexOf(">") + 1);
            content = content.Remove(content.LastIndexOf("</body>"), content.Length - content.LastIndexOf("</body>"));


            //deleting index from description
            if (content.Contains("<div style='mso-element:comment-list'>"))
            {
                content = content.Remove(content.IndexOf("<div style='mso-element:comment-list'>"));
            }

            for (int i = 0; ; i++)
            {
                if (!content.Contains(">["))
                {
                    break;
                }
                //content = content.Remove(content.IndexOf(">[")+1, 5);
                content = content.Remove(content.IndexOf(">[") + 1, (content.IndexOf("]</a>")+1) - (content.IndexOf(">[") + 1));
            }
            return content.Trim();

        }
Exemplo n.º 39
0
        // supports_rule
        //   : "@supports" supports_condition group_rule_body
        //   ;
        internal bool ParseSupportsRule(RuleAppendFunc aAppendFunc, object aProcessData)
        {
            bool conditionMet = false;
              var condition = new StringBuilder();

              mScanner.StartRecording();
              bool parsed = ParseSupportsCondition(ref conditionMet);

              if (!parsed) {
            mScanner.StopRecording();
            return false;
              }

              if (!ExpectSymbol('{', true)) {
            { if (!mSuppressErrors) mReporter.ReportUnexpected("PESupportsGroupRuleStart", mToken); };
            mScanner.StopRecording();
            return false;
              }

              UngetToken();
              mScanner.StopRecording(condition);

              // Remove the "{" that would follow the condition.
              if (condition.Length() != 0) {
            condition.Truncate(condition.Length() - 1);
              }

              // Remove spaces from the start and end of the recorded supports condition.
              condition.Trim(" ", true, true, false);

              // Record whether we are in a failing @supports, so that property parse
              // errors don't get reported.
              using (/*var failing = */new nsAutoFailingSupportsRule(this, conditionMet)) {

            GroupRule rule = new CSSSupportsRule(ref conditionMet, condition);
            return ParseGroupRule(rule, aAppendFunc, aProcessData);
              }
        }
Exemplo n.º 40
0
        /// <summary>
        /// Splits a command line by spaces, keeping quoted text together
        /// </summary>
        public static IEnumerable<string> SplitCommandLine(string commandLine)
        {
            if (string.IsNullOrWhiteSpace(commandLine)) return Enumerable.Empty<string>();
            var args = new List<string>();
            StringBuilder sb = new StringBuilder(commandLine);
            sb.Trim();

            int argStart = 0;
            int lastQuote = -1;
            for (int i = 0; i < sb.Length; i++)
            {
                char current = sb[i];
                if (char.IsWhiteSpace(current))
                {
                    if (argStart == i)
                    {
                        // Leading argument whitespace
                        argStart++;
                        continue;
                    }
                    else if (argStart != lastQuote)
                    {
                        // Not in a quote, end of arg
                        args.Add(sb.ToString(argStart, i - argStart));
                        argStart = i + 1;
                        continue;
                    }
                }

                if (current == '"')
                {
                    if (lastQuote == -1)
                    {
                        if (argStart == i)
                        {
                            // Start of quote
                            lastQuote = i;
                            argStart = i;
                            continue;
                        }
                    }
                    else
                    {
                        // End of quote, trim out quotes
                        argStart++;
                        args.Add(sb.ToString(argStart, i - argStart));
                        lastQuote = -1;
                        argStart = i + 1;
                        continue;
                    }
                }
            }

            if (lastQuote != -1)
            {
                // Orphaned quote, move the argument start forward
                argStart++;
            }

            if (argStart < sb.Length)
            {
                args.Add(sb.ToString(argStart, sb.Length - argStart));
            }

            return args;
        }
Exemplo n.º 41
0
        /// <summary>
        /// �������������� ������.
        /// <b>������������������!</b>
        /// </summary>
        /// <param name="txt">�������� �����.</param>
        /// <param name="smile">������� ��������� ���������.</param>
        /// <param name="doNotReplaceTags">�� �������� ��������� ������� HTML.</param>
        /// <param name="doNotFormatImplicitLinks">�� ������������� ���� �� ��������� ������.</param>
        /// <returns>���������������� �����.</returns>
        public virtual string Format(
            string txt,
            bool smile,
            bool doNotReplaceTags,
            bool doNotFormatImplicitLinks)
        {
            var sb = new StringBuilder(txt);

            sb.Trim(TrimArray);

            if (sb.IsEmpty())
                return "";

            // ��������! ������� �������������� �����.
            //

            // ������  ������������ ��������
            if (!doNotReplaceTags)
                sb = sb.ReplaceTagsWQ();

            // ���������� ���� ����� ������ ����� � \n
            //
            sb = _rxNewLineUnifier.Replace(sb, "\n");

            // ��������� �������� ����� � �����,
            // ������� �� ����� ���� ������ ����������.
            //

            // temporary remove [code...] tags
            const string codeExpression = "$$code{0}$$";
            var codeMatcher = new Matcher(codeExpression);
            sb = _rxCodeFormatting.Replace(sb, codeMatcher.Match);

            // temporary remove [img] tags
            const string imgExpression = "$$img{0}$$";
            var imgMatcher = new Matcher(imgExpression);
            sb = _imgTagRegex.Replace(sb, imgMatcher.Match);

            // temporary remove [url] & [purl] tags
            const string urlExpression = "$$url{0}$$";
            var urlMatcher = new Matcher(urlExpression);
            sb = _urlTagRegex.Replace(sb, urlMatcher.Match);

            // temporary remove implicit links
            const string implicitUrlExpression = "$$iurl{0}$$";
            var implicitUrlMatcher = new Matcher(implicitUrlExpression);
            if (!doNotFormatImplicitLinks)
                sb = _urlRegex.Replace(sb, implicitUrlMatcher.Match);

            // temporary remove [q] tags
            const string quoteExpression = "$$quote{0}$$";
            var quoteMatcher = new Matcher(quoteExpression);
            sb = _rxPrep12.Replace(sb, quoteMatcher.Match);

            // temporary remove [cut] tags
            const string cutExpression = "$$cut{0}$$";
            Matcher cutMatcher;
            do
            {
                cutMatcher = new Matcher(cutExpression);
                sb = _rxPrep13.Replace(sb, cutMatcher.Match);

                // �����������.
                sb = _rxTextUrl09.Replace(sb, "<span class='lineQuote'>$&</span>");

                // restore & transform [cut] tags
                for (var i = 0; i < cutMatcher.Count; i++)
                {
                    var m = cutMatcher[i];
                    var capt = String.IsNullOrEmpty(m.Groups[3].Value) ? "������� �����" : m.Groups[3].Value;
                    sb = sb.Replace(String.Format(cutExpression, i),
                        _hiddenTextSnippet.Replace("%CAPT%", capt).Replace("%TEXT%", m.Groups[4].Value).
                        Replace("%URL%", GetImagePrefix()));
                }
            } while (cutMatcher.Count > 0);

            // restore & transform [q] tags
            // ����������� [q].
            // http://www.rsdn.ru/forum/?mid=111506
            for (var i = 0; i < quoteMatcher.Count; i++)
                sb =
                    sb.Replace(
                        string.Format(quoteExpression, i),
                        string.Format(
                            "<blockquote class='q'><p>{0}</p></blockquote>",
                            quoteMatcher[i].Groups[1]));

            // ��������� ��������� � ������ ������ � http://www.rsdn.ru/forum/?mid=184751
            if (smile)
            {
                var prefix = GetImagePrefix();
                sb = _smileReplacers.Aggregate(
                    sb,
                    (current, replacer) => replacer.Replace(current, prefix));
            }

            // ISBN
            sb =
                _isbnDetector.Replace(
                    sb,
                    match =>
                    {
                        var isbn = new StringBuilder(match.Length);
                        foreach (Capture capture in match.Groups["isbn"].Captures)
                        {
                            isbn.Append(capture.Value).Append('-');
                        }
                        if (isbn.Length > 0)
                            isbn.Length--;
                        return ProcessISBN(match, isbn.ToString());
                    });

            // restore & transform [url] and [purl] tags
            for (var i = 0; i < urlMatcher.Count; i++)
            {
                var url = urlMatcher[i].Groups["url"].Value;
                var tag = urlMatcher[i].Groups["tag"].Value;

                // ���� url � tag ����������:
                //
                if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
                    // ���� tag �� ������
                    //
                    if (!String.IsNullOrEmpty(tag))
                        // ���� tag ���������� Uri
                        //
                        if (Uri.IsWellFormedUriString(tag, UriKind.RelativeOrAbsolute))
                        {
                            //
                            //
                            var temp = tag;
                            tag = url;
                            url = temp;
                        }

                sb = sb.Replace(
                    string.Format(urlExpression, i),
                    ProcessURLs(url, tag));
            }

            // restore & transform implicit links
            for (var i = 0; i < implicitUrlMatcher.Count; i++)
                sb = sb.Replace(
                    string.Format(implicitUrlExpression, i),
                    ProcessImplicitURLs(implicitUrlMatcher[i]));

            // restore & transform [img] tags
            for (var i = 0; i < imgMatcher.Count; i++)
                sb = sb.Replace(
                    string.Format(imgExpression, i),
                    ImagesDelegate(this, imgMatcher[i]));

            // RSDN links
            sb = _rsdnLinkDetector.Replace(sb, ProcessRsdnLink);

            // [email]
            sb = _emailTagRegex.Replace(sb, ProcessEmailLink);

            // Replace hyphen to dash
            sb = _dashDetector.Replace(sb, "&mdash;");

            // [tagline]
            sb = _taglineDetector.Replace(sb, "<div class='tagline'>$1</div>");

            // [list]
            sb = _rxPrep06.Replace(sb, @"<ul style='margin-top:0; margin-bottom:0;'>$1</ul>");

            // [list=..]
            sb = _rxPrep07.Replace(sb, ListEvaluator);

            // [*]
            sb = _rxPrep08.Replace(sb, "<li />");

            // [hr]
            sb = _rxPrep09.Replace(sb, "<hr />");

            // Q12345(6)
            sb = _rxPrep10.Replace(
                sb,
                @"<a target='_blank' class='m' href='http://support.microsoft.com/default.aspx?scid=kb;EN-US;$1'>$1</a>");

            // ��������� ����������.
            sb = _moderatorDetector.Replace(sb, "<div class='mod'>$1</div>");

            // Table
            sb = _rxTable.Replace(
                sb,
                "<table class='formatter' border='0' cellspacing='2' cellpadding='5'>$1</table>");
            sb = _rxTableRow.Replace(sb, "<tr class='formatter'>$1</tr>");
            sb = _rxTableHeader.Replace(sb, "<th class='formatter'>$1</th>");
            sb = _rxTableColumn.Replace(sb, "<td class='formatter'>$1</td>");

            // Headers
            sb = _rxHeaders.Replace(sb, "<h$2 class='formatter'>$1</h$2>");

            // ��������� � ����� ������ ������ <br />,
            // �� �� ����� </table>, </div>, </ol>, </ul>, <blockquote> (�������� ������ <span>)
            // � �� � ����� ����� ������
            sb = _rxNewLines.Replace(sb, "<br />$0");

            sb = _inlineTagReplacers.Aggregate(sb, (cur, replacer) => replacer(cur));

            // ������ �� MSDN.
            sb = _rxMsdn.Replace(sb, DoMSDNRef);

            // ����� ��� ������ ����� � ������ ���������.
            sb = _rxPrep01.Replace(sb, "");
            sb = _rxPrep02.Replace(sb, "");
            sb = _rxPrep03.Replace(sb, "");

            // restore & transform [code] tags
            for (var i = 0; i < codeMatcher.Count; i++)
            {
                // code coloring
                var code = PaintCode(codeMatcher[i]);

                // bold & italic formatting inside code
                // without checking canceling tag syntax
                code = _inlineTagReplacersNoChecks.Aggregate(code, (cur, replacer) => replacer(cur));

                sb = sb.Replace(string.Format(codeExpression, i), code.ToString());
            }

            return sb.ToString();
        }
Exemplo n.º 42
0
 public static string CleanIdentifier(string input) {
     var sb = new StringBuilder(Regex.Replace(input, CLEAN_PATTERN, "_"));
     sb.Push(c => c.Equals('_') || Char.IsNumber(c));
     sb.Trim(" ");
     var result = sb.ToString();
     if (result.Equals(String.Empty) || result.All(c => c.Equals('_') || Char.IsNumber(c))) {
         result = "I" + input.GetHashCode().ToString(CultureInfo.InvariantCulture).Replace("-", "0");
     }
     if (!input.Equals(result)) {
         //logger.Debug("Using '{0}' to identify field '{1}'.", result, input);
     }
     return sb.ToString();
 }