So im really bored and decided I wanted to code something. Sense im still learning( I know c/c++/C# syntax pretty well but I still lack the knowlege you gain in univ etc about performance and whatnot )
I wanted to see if I was going in a decent direction with it.
Anywho, here it is. Be gentle( lol ).
Code:
/*
* Basically just a helper class for string parsing, eventually it
* may actually be more usful than what it is right now.
*
* - Sithid( DemonicUrges05 (AT) gmail (DOT) com )
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace StringParser
{
public class StringParser
{
protected string[] m_Strings;
private string m_FullText;
private char[] m_Delimiters;
public string FullText { get { return m_FullText; } set { m_FullText = value; } }
public char[] Delimiters { get { return m_Delimiters; } set { m_Delimiters = value; } }
public int Length { get { return m_Strings.Length; } }
// Useful for letting me access the string arrays indexes without ever exposing it.
// Could probly work ok with a normal getter and no setter. I guess I just like this
// way.
public string this[int key]
{
get
{
if ( key < m_Strings.Length )
return m_Strings[key];
else // Return the first index of the array if the key is out of range.
return m_Strings.Length > 0 ? m_Strings[0] : String.Empty; // Return String.Empty if theres zero indexs.
}
protected set
{
if ( key < m_Strings.Length )
m_Strings[key] = value;
}
}
public StringParser()
: this( string.Empty, false )
{
}
public StringParser( string toParse, bool willParse )
: this( toParse, new char[] { ',' }, willParse )
{
}
public StringParser( string toParse, char[] delimiter, bool willParse )
{
m_FullText = toParse;
m_Delimiters = delimiter;
if ( willParse )
Parse( m_FullText, m_Delimiters, false );
}
public void Parse( bool removeEmpties )
{
Parse( m_FullText, m_Delimiters, removeEmpties );
}
public void Parse( string toParse, bool removeEmpties )
{
Parse( toParse, m_Delimiters, removeEmpties );
}
public void Parse( string toParse, char[] delimiter, bool removeEmpties )
{
m_Strings = toParse.Split( delimiter );
// This slows it down the most( oviously ).
// I would not trim it and use the RemoveEmptyIndexs method after parse but not all strings would use
// an empty space as a delimiter so for now trim will have to stay to weed off the white spaces before
// and after chars are removed.
// Afterthought, add a bool to check if we want all whitespace weeded out regardless, if so, skip the trim.
if ( !removeEmpties )
{
for ( int i = 0; i < m_Strings.Length; ++i )
{
m_Strings[i].Trim();
}
}
}
public void RemoveEmptyIndexs()
{
// Something tells me this is loooow.
// Will look into finding a faster way
// to weed off empty indexes.
List<string> list = new List<string>();
for( int i = 0; i < m_Strings.Length; ++i )
{
if( m_Strings[i] != "" && m_Strings[i] != string.Empty )
{
list.Add( m_Strings[i] );
}
}
string[] temp = new string[list.Count];
for ( int i = 0; i < temp.Length; ++i )
{
temp[i] = list[i];
}
m_Strings = temp;
}
}
}
My test console app gave me this:
Quote:
Press enter to start.
Parsing a string with ',' as the delimiter.
Original text:
One day, I would like to have a Ph. D, I wonder If I ever will, hrm.
Parsed strings:
One
day
I
would
like
to
have
a
Ph.
D
I
wonder
If
I
ever
will
hrm.
Finished.[7669838]
Starting bulk parsing test.
Bulk parse testing took 1.640625 total seconds to do 1000000 loops.
Seems skipping the trim when I plan to use RemoveEmptyIndexes after the parse saved me alot of time( used to be 5.5s per 1000000 loops ).