BG Development


: (41) «  ... 39 40 [41]   ( ) Reply to this topicStart new topicStart Poll

> Battle of the slowest - markdown parser 2.0
Stilgar
: 23-01-2018, 23:19
Quote Post



:
:
:

: 11826
: 13.05.08



QUOTE (Delegate @ 23-01-2018, 20:31)
, ! ..
C# , , . icon_biggrin.gif

.NET Native .


--------------------
, .
PMEmail PosterUsers Website
Top
Delegate
: 24-01-2018, 11:03
Quote Post



:
:
:

: 1497
: 30.05.09



, .

CODE
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace markdown3
{

class Program
{

static string[] urlArray = new string[40000];
const int intMaxHalf = (Int32.MaxValue / 2);
static void Main(string[] args)
{


StringBuilder line;
string inFileName = args[0];
string outFileName = args[1];

var infile = File.ReadAllLines(inFileName);
List<string> lineList = new List<string>(infile);
bool bParagraphOpened = false;
lineList.Insert(0, "<html><head><meta http-equiv=\"content - type\" content=\"text / html; charset = utf - 8\" /></head><body>");

var watch = System.Diagnostics.Stopwatch.StartNew();

for (int i = lineList.Count - 1; i >= 0; i--)
{


line = new StringBuilder(lineList[i]);

int iHead = line.ToString().IndexOf("#");
if (iHead >= 0)
{

int c = iHead;
while (line[c] == '#') { c++; }
line = line.Remove(0, c);
char headCh = (char)c;
line = line.Insert(0, "<h" + c + ">");
line.Append("</h" + c + ">");
lineList[i] = line.ToString();
continue;

}

int iBold = line.ToString().IndexOf("*");
bool bTagOpen = true;
while (iBold >= 0)
{

line = line.Remove(iBold, 1);

if (bTagOpen)
line = line.Insert(iBold, "<b>");

else
line = line.Insert(iBold, "</b>");


iBold = line.ToString().IndexOf("*");
bTagOpen = !bTagOpen;

}


if (line.Length < 1 && bParagraphOpened == false)
{
line.Append("<p>");
bParagraphOpened = true;
lineList[i] = line.ToString();
continue;
}
if (line.Length < 1 && bParagraphOpened == true)
{
line.Append("</p>");
bParagraphOpened = false;
lineList[i] = line.ToString();
continue;
}

int start = line.ToString().IndexOf("[");

if (start == 0)
{
line = line.Remove(start, 1);
start = line.ToString().IndexOf("]");
int hashCode = 0;
if (start > 0)
{

hashCode = line.ToString().Substring(0, start).GetHashCode();
line = line.Remove(0, 1);
string url = line.ToString().Substring(start);
if (hashCode < 0)
{
urlArray[hashCode / intMaxHalf * -1] = url;
}
else
{
urlArray[hashCode / intMaxHalf] = url;

}


lineList[i] = null;
lineList.RemoveAt(i);
continue;
}
}
else
{

int end = line.ToString().IndexOf("]");
if (start > 0 && end > 0)
{
string dispText = line.ToString().Substring(start + 1, end - start - 1);

int currentHash = dispText.GetHashCode();

string urlText = (currentHash < 0) ? urlArray[(currentHash / intMaxHalf) * -1] : urlArray[(currentHash / intMaxHalf)];


line = line.Remove(start + 1, end - start - 1);
line = line.Insert(start, "<a href=" + "\"" + urlText + "\"" + ">" + dispText);

start = line.ToString().IndexOf("[");
line = line.Remove(start, 1);
end = line.ToString().IndexOf("]");
line = line.Remove(end, 1);
line = line.Insert(end, "</a>");

}
}

lineList[i] = line.ToString();
}




lineList.Add("</body></html>");
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time in milliseconds without IO operations : " + elapsedMs);

File.WriteAllLines(outFileName, lineList.ToArray());



}




}
}




Delegate 24-01-2018, 11:05
PMEmail Poster
Top
relax4o
: 24-01-2018, 16:53
Quote Post



:
:
:

: 1786
: 04.04.07



Visual Studio. . IDE . .


--------------------
:D

QUOTE (oveRLuckEd)
PHP, .


QUOTE (nbacool2)
input , SQL Very Happy
PM
Top
hristonev
: 24-01-2018, 17:00
Quote Post



:
:
:

: 380
: 19.09.17



icon_razz.gif .
PMEmail Poster
Top
relax4o
: 24-01-2018, 17:06
Quote Post



:
:
:

: 1786
: 04.04.07



QUOTE (hristonev @ 24-01-2018, 17:00)
icon_razz.gif .

, .NET Framework Redistributable .


--------------------
:D

QUOTE (oveRLuckEd)
PHP, .


QUOTE (nbacool2)
input , SQL Very Happy
PM
Top
1 (1 , 0 )
, :
« | | »

Topic Options : (41) «  ... 39 40 [41]  Reply to this topicStart new topicStart Poll

 


Copyright © 2003-2018 | BG Development | All Rights Reserved
RSS 2.0