Skip to main content
Added refactored version.
Source Link
Petrroll
  • 288
  • 2
  • 7

EDIT: Added refactored version 2.0 to the end!

Refactored version:

NOTES:

  • I've kept camelCase naming for private methods names. The official naming convention talks only about public methods and camelCase really helps me with readability.

  • I've separated parseNewExpression to it's own method as it seemed to me that there was too much code repetition.

  • ParseExpression() is still without parameters because one should be able to Reparse previously added expression without the need to set it again (e.g. after SkipInvalidTokens settings change).

  • Significantly reduced ParseExpression methods (there was quite a lot of overlapping cuntionality). Now it's slower but waaay nicer (and who cares about speed anyway...)

     public void ParseExpression()
     {
         ParsedSubstringType lastType = ParsedSubstringType.NotSet;
    
         parsedExpression.Clear();
         parsedTypes.Clear();
    
         StringBuilder charBuffer = new StringBuilder(avgTokenLength);
         foreach (char token in rawExpression)
         {
             ParsedSubstringType currentType = getTokenType(token);
             if(IsCoumpnoundable(lastType) && currentType != lastType)
             {
                 parseNewExpression(charBuffer, lastType);
             }
    
             lastType = currentType;
             if (IsCoumpnoundable(currentType))
             {
                 charBuffer.Append(token);
                 continue;
             }
    
             if (currentType == ParsedSubstringType.Invalid && SkipInvalidChars) { continue; }
             charBuffer.Append(token);
    
             parseNewExpression(charBuffer, currentType);
         }
    
         if (charBuffer.Length > 0)
         {
             parseNewExpression(charBuffer, lastType);
         }
     }
     private void parseNewExpression(StringBuilder charBuffer, ParsedSubstringType currentType)
     {
         string expression = (isTrashable(currentType)) ? string.Empty : charBuffer.ToString();
    
         parsedTypes.Add(currentType);
         parsedExpression.Add(expression);
         charBuffer.Clear();
     }
    
     private bool IsCoumpnoundable(ParsedSubstringType type)
     {
         return (
                 type == ParsedSubstringType.Num ||
                 type == ParsedSubstringType.Name ||
                 type == ParsedSubstringType.WhiteSpace
              );
     }
    
     private bool isTrashable(ParsedSubstringType type)
     {
         return (type == ParsedSubstringType.WhiteSpace);
     }
    

EDIT: Added refactored version 2.0 to the end!

Refactored version:

NOTES:

  • I've kept camelCase naming for private methods names. The official naming convention talks only about public methods and camelCase really helps me with readability.

  • I've separated parseNewExpression to it's own method as it seemed to me that there was too much code repetition.

  • ParseExpression() is still without parameters because one should be able to Reparse previously added expression without the need to set it again (e.g. after SkipInvalidTokens settings change).

  • Significantly reduced ParseExpression methods (there was quite a lot of overlapping cuntionality). Now it's slower but waaay nicer (and who cares about speed anyway...)

     public void ParseExpression()
     {
         ParsedSubstringType lastType = ParsedSubstringType.NotSet;
    
         parsedExpression.Clear();
         parsedTypes.Clear();
    
         StringBuilder charBuffer = new StringBuilder(avgTokenLength);
         foreach (char token in rawExpression)
         {
             ParsedSubstringType currentType = getTokenType(token);
             if(IsCoumpnoundable(lastType) && currentType != lastType)
             {
                 parseNewExpression(charBuffer, lastType);
             }
    
             lastType = currentType;
             if (IsCoumpnoundable(currentType))
             {
                 charBuffer.Append(token);
                 continue;
             }
    
             if (currentType == ParsedSubstringType.Invalid && SkipInvalidChars) { continue; }
             charBuffer.Append(token);
    
             parseNewExpression(charBuffer, currentType);
         }
    
         if (charBuffer.Length > 0)
         {
             parseNewExpression(charBuffer, lastType);
         }
     }
     private void parseNewExpression(StringBuilder charBuffer, ParsedSubstringType currentType)
     {
         string expression = (isTrashable(currentType)) ? string.Empty : charBuffer.ToString();
    
         parsedTypes.Add(currentType);
         parsedExpression.Add(expression);
         charBuffer.Clear();
     }
    
     private bool IsCoumpnoundable(ParsedSubstringType type)
     {
         return (
                 type == ParsedSubstringType.Num ||
                 type == ParsedSubstringType.Name ||
                 type == ParsedSubstringType.WhiteSpace
              );
     }
    
     private bool isTrashable(ParsedSubstringType type)
     {
         return (type == ParsedSubstringType.WhiteSpace);
     }
    
Added refactored code.
Source Link
Petrroll
  • 288
  • 2
  • 7
Tweeted twitter.com/#!/StackCodeReview/status/543021938881622016
added 235 characters in body
Source Link
Petrroll
  • 288
  • 2
  • 7

INPUT/OUTPUT testing:

INPUT: 2 & 3*(7- 3+2+-2 / 2(()*(2)
OUTPUT string[]: {2,,,3,*,(,7,-,,3,+,3,+,-,2,,/,,2,(,(,),*,(,2,)}
OUTPUT types[]: {Num, WhiteSpace, WhiteSpace, Num, Operator, Bracket, Num, Operator, ... }

INPUT/OUTPUT testing:

INPUT: 2 & 3*(7- 3+2+-2 / 2(()*(2)
OUTPUT string[]: {2,,,3,*,(,7,-,,3,+,3,+,-,2,,/,,2,(,(,),*,(,2,)}
OUTPUT types[]: {Num, WhiteSpace, WhiteSpace, Num, Operator, Bracket, Num, Operator, ... }
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
deleted 1 character in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Petrroll
  • 288
  • 2
  • 7
Loading