Last visit was: It is currently Thu Mar 28, 2024 7:25 am


All times are UTC-05:00




Post new topic Reply to topic  [7 posts ] 
    Author Message
     Post subject:C# String Parsing Helper ... I guess
    PostPosted:Sat May 17, 2008 6:32 am 
     

    Joined:Wed May 14, 2008 3:31 am
    Posts:14
    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 ).


    Last edited by sithid on Sat May 17, 2008 6:41 am, edited 1 time in total.

    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Sat May 17, 2008 6:39 am 
    Literally Nine
    User avatar
     

    Joined:Sat Apr 02, 2005 3:31 pm
    Posts:1171
    Location:The vicinity of an area adjacent to a location.
    Wow, you just happened to post when I was perusing. Nifty.

    Also, I think perhaps you're going too far to the "dark side" of programming in that you're over-complexifying things. Creating a string parser is useful, certainly, but I don't think that creating a class for it is necessary if it has no specific purpose. Let's say for instance you wanted to be able to parse XML. For instance:
    Code:
    <section> <tag attribute="attribute-value">data</tag> </section>
    That would require a pretty specialized class to parse it (I would know. I tried writing an XML parser and ended up giving up because I figured the libxml2 guys really do know what they're doing).

    Or even if you're parsing something simple. A .ini file for instance. You'd need to handle syntax something like this:
    Code:
    [section] name=value name2=value2
    and so forth. Parsing it isn't entirely complex, but you do need a specialized class for it.

    The big problem with a generalized string parser is performance. If you are designing a string parser, you should keep speed in mind, because generally the painful points of a program are string-related operations. A generalized class can be made to be high-performance, but it may be sub-optimal for (or even incapable of) specialized tasks.

    But perhaps I'm looking at this a bit too seriously. If you're just coding for the sake of practice, go for it. But if you're trying to write something you'd actually use, you need to be careful and plan ahead a bit.

    Hopefully you can get something out of this. I feel like I'm rambling. :)

    _________________
    - Tycho

    Image


    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Sat May 17, 2008 6:59 am 
     

    Joined:Wed May 14, 2008 3:31 am
    Posts:14
    Trying to pass some time, really bored :(. It does this loop:
    Code:
    for( int i = 0; i < 1000000; ++i ) { parser = new StringParser( "One, day, I, would, like, to, have, a, Ph. D, I, wonder, If, I, ever, will, hrm.", false ); parser.Parse( true ); parser.RemoveEmptyIndexs(); }
    In 2.2 seconds now( better than before ). :).


    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Sat May 17, 2008 6:00 pm 
    Literally Nine
    User avatar
     

    Joined:Sat Apr 02, 2005 3:31 pm
    Posts:1171
    Location:The vicinity of an area adjacent to a location.
    RemoveEmptyIndexs[sic] is kind of a strange function. Shouldn't need to do that. Your original algorithm should eliminate such empty indices.

    _________________
    - Tycho

    Image


    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Sun May 18, 2008 2:57 am 
     

    Joined:Wed May 14, 2008 3:31 am
    Posts:14
    It didnt though, not if I used ' ' and ',' as delimiters, the use of Trim would only trim the text of empty space, but did nothing for an index with empty text.


    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Mon May 19, 2008 3:56 am 
    Literally Nine
    User avatar
     

    Joined:Sat Apr 02, 2005 3:31 pm
    Posts:1171
    Location:The vicinity of an area adjacent to a location.
    I meant to say that it was a design flaw, not a bug.

    _________________
    - Tycho

    Image


    Top
    Offline  
     Post subject:Re: C# String Parsing Helper ... I guess
    PostPosted:Mon May 19, 2008 5:41 am 
     

    Joined:Wed May 14, 2008 3:31 am
    Posts:14
    Quote:
    I meant to say that it was a design flaw, not a bug.
    I see.

    Removed the use of that method and rewrote the parse one.
    Code:
    public void Parse( string toParse, char[] delimiter ) { string[] m_Temp = toParse.Split( delimiter ); List<string> m_Valid = new List<string>(); for ( int i = 0; i < m_Temp.Length; ++i ) { if ( m_Temp[i] != string.Empty ) { m_Valid.Add( m_Temp[i] ); } } m_Strings = m_Valid.ToArray(); }


    Top
    Offline  
    Display posts from previous: Sort by 
    Post new topic Reply to topic

      All times are UTC-05:00


      Who is online

      Users browsing this forum: No registered users and 14 guests


      You cannot post new topics in this forum
      You cannot reply to topics in this forum
      You cannot edit your posts in this forum
      You cannot delete your posts in this forum
      You cannot post attachments in this forum

      Search for:
      Jump to:  
      Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
      Theme created by Miah with assistance from hyprnova