It is currently Sat Aug 19, 2017 4:23 am

All times are UTC - 5 hours




 Page 1 of 1 [ 11 posts ] 
Author Message
 Post subject: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 01, 2015 11:37 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
In this video I present Challenge #5. In this challenge you have to come up with a way to remove comments from gui text files. Your code must handle single line comments using two slash characters, as well as block comments using the slash-star, star-slash notation.

When you have a solution, post your code here and the first person to do this correctly will win 200 download credits.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 12:05 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
After going back to the drawing board here is what I have come up with. I have tested each case with both comments and I do not get any exceptions. Everything is rendering correctly too. I hope I have it right this time.

// ----------------------------------------------------------------------------
// removeComments()
void GuiLoader::removeComments() {
   
   const unsigned tokenLength = 1;   

   static std::string::size_type indexA;
   static std::string::size_type indexB;
   
   const std::string commentStartingToken( "/" );
   const std::string blockCommentStartingQualifier( "*" );
   const std::string blockCommentEnd( "*/" );   
      
   // Check To See If We Are Not In A Block Comment
   if ( !_inBlockComment ) {
      // If Store Index Value For Our First '/' Into indexA
      indexA = _strLine.find( commentStartingToken );
   }

   // If indexA Is Not On This Line - Reset To 0 And Return - No Comments
   if ( indexA == std::string::npos ) {
      indexA = 0;
      return;
   } else { // Otherwise There Is A Comment

      // First Check To See If We Are Not In A Block Comment
      if ( !_inBlockComment ) {
         
         // If True This Is A C++ Style Comment
         if ( _strLine.compare( indexA + 1, tokenLength, commentStartingToken ) == 0 ) {
            _strLine.erase( indexA );
            _strLine = Utility::trim( _strLine, std::string( " " ) );
            indexA = 0;
            return;
         }
         
         // If True This Is A C Style Comment
         if ( _strLine.compare( indexA + 1, tokenLength, blockCommentStartingQualifier ) == 0 ) {
            _inBlockComment = true;
            indexB = _strLine.find( blockCommentEnd );
         }      
      }   

      // Check To See If We Are In A Block Comment And To See If Our blockCommentEnd Is Not On This Line
      if ( _inBlockComment && indexB == std::string::npos ) {
         // Just To Be Safe Reset Our indexB Due To Recursive Calls.
         indexB = _strLine.find( blockCommentEnd );
      }

      // While In A Block Comment And indexB is Not On This Line.
      while ( _inBlockComment && indexB == std::string::npos ) {
         // Erase Line & Trim
         _strLine.erase( indexA );
         _strLine = Utility::trim( _strLine, std::string( " " ) );
         
         // Check To See If indexA Is == The Size Of Our String + 1
         // If So We Need To Reset indexA, Parse The Line And Get Next Line
         if ( indexA == _strLine.size() + 1 ) {
            indexA = 0;
            parseGui();
            getNextLine();

         } else { // If Not We Need Only To Reset indexA And Get Next Line (No Parsing)
            indexA = 0;
            getNextLine();
         }
      }

      // While In A Block Comment And indexB Is On This Line
      while ( _inBlockComment && indexB != std::string::npos ) {
         if ( indexA == 0 ) {
            // This Case Is For When A Block Comment Starts At The Beginning Of A Line
            _strLine.replace( indexA, indexB+2 - indexA, "" );
            break;
         } else {
            // This Case Is When A Block Comment Starts Past The Beginning Of A Line
            _strLine.replace( indexA, (indexB+2 - indexA) + 1, "" );
            break;
         }
      }
      
      // Reset All Indexes & Flags - Trim String To Be Safe.
      indexA = 0;
      indexB = 0;
      _strLine = Utility::trim( _strLine, std::string( " " ) );
      _inBlockComment = false;

      // Recursively Call Function For Block Comments
      removeComments();
   }

} // removeComments


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 6:08 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
This solution is not correct. This code gets stuck in an infinite loop and crashes the program. Test your code with the case were you have 2 block quotes back to back like this:

LayoutAbsolute offset_300,0 /*align_top*//* size_250,150*/ color_1,0.75,1,1 padding_10 {


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 4:03 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I just tested the code. When I applied the back 2 back block comments on the first line of the test.gui file /*Testing*//*Testing Back 2 Back*/ The code runs correctly.
When I tried your case LayoutAbsolute offset_300,0 /*align_top*//*size_250,150*/ color_1,0.75,1,1 padding_10 { The program does go into an infinite loop, but when I add a space in between the two slashed like this: LayoutAbsolute offset_300,0 /*align_top*/ /*size_250,150*/ color_1,0.75,1,1 padding_10 { It does render correctly. I'll have to go through the debugger and find out why it is crashing without the space.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 4:23 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
It didn't take me long to fix this problem, in the while loop where there were two cases I had removed both if statements replaced it with doing just the string.replace() Now It appears to work either with a or without a space in between the first ending / and the second beginning /.

Here is the revised code:
// ----------------------------------------------------------------------------
// removeComments()
void GuiLoader::removeComments() {
   
   const unsigned tokenLength = 1;   

   static std::string::size_type indexA;
   static std::string::size_type indexB;
   
   const std::string commentStartingToken( "/" );
   const std::string blockCommentStartingQualifier( "*" );
   const std::string blockCommentEnd( "*/" );   
      
   // Check To See If We Are Not In A Block Comment
   if ( !_inBlockComment ) {
      // If Store Index Value For Our First '/' Into indexA
      indexA = _strLine.find( commentStartingToken );
   }

   // If indexA Is Not On This Line - Reset To 0 And Return - No Comments
   if ( indexA == std::string::npos ) {
      indexA = 0;
      return;
   } else { // Otherwise There Is A Comment

      // First Check To See If We Are Not In A Block Comment
      if ( !_inBlockComment ) {
         
         // If True This Is A C++ Style Comment
         if ( _strLine.compare( indexA + 1, tokenLength, commentStartingToken ) == 0 ) {
            _strLine.erase( indexA );
            _strLine = Utility::trim( _strLine, std::string( " " ) );
            indexA = 0;
            return;
         }
         
         // If True This Is A C Style Comment
         if ( _strLine.compare( indexA + 1, tokenLength, blockCommentStartingQualifier ) == 0 ) {
            _inBlockComment = true;
            indexB = _strLine.find( blockCommentEnd );
         }      
      }   

      // Check To See If We Are In A Block Comment And To See If Our blockCommentEnd Is Not On This Line
      if ( _inBlockComment && indexB == std::string::npos ) {
         // Just To Be Safe Reset Our indexB Due To Recursive Calls.
         indexB = _strLine.find( blockCommentEnd );
      }

      // While In A Block Comment And indexB is Not On This Line.
      while ( _inBlockComment && indexB == std::string::npos ) {
         // Erase Line & Trim
         _strLine.erase( indexA );
         _strLine = Utility::trim( _strLine, std::string( " " ) );
         
         // Check To See If indexA Is == The Size Of Our String + 1
         // If So We Need To Reset indexA, Parse The Line And Get Next Line
         if ( indexA == _strLine.size() + 1 ) {
            indexA = 0;
            parseGui();
            getNextLine();

         } else { // If Not We Need Only To Reset indexA And Get Next Line (No Parsing)
            indexA = 0;
            getNextLine();
         }
      }

      // While In A Block Comment And indexB Is On This Line
      while ( _inBlockComment && indexB != std::string::npos ) {
         _strLine.replace( indexA, ((indexB+2) - indexA), "" );
         break;
      }
      
      // Reset All Indexes & Flags - Trim String To Be Safe.
      indexA = 0;
      indexB = 0;
      _strLine = Utility::trim( _strLine, std::string( " " ) );
      _inBlockComment = false;

      // Recursively Call Function For Block Comments
      removeComments();
   }

} // removeComments


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 6:22 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
This solution is not correct. I can still make the code get stuck in an infinite loop and crash the program using my test cases. I will give you a hint to why this is happening on Tuesday if you still haven't figured it out by then, that way we can give the other members some more time to come up with a solution.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Sun Mar 08, 2015 6:41 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Okay, no problem. I know I am close to getting it, but working with strings is not my strength, unless you would be willing to allow me to use your test case so I can do some more debugging, I'll continue to work at it.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Wed Mar 11, 2015 8:23 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
ok here is another hint. Consider what happens when you have something like this:

  LayoutAbsolute offset_300,0 id_my/Name 


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Fri Mar 13, 2015 3:28 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I've been working on it and so far this version seems to render everything and I do not get into an infinite loop with a single '/' in a line of text.

// ----------------------------------------------------------------------------
// removeComments()
void GuiLoader::removeComments() {
   
   const unsigned tokenLength = 2;   

   // Index For C++ Single Line Comments   
   const std::string lineComment( "//" );
   const std::string::size_type indexA = _strLine.find( lineComment );

   // Is There A C++ Comment On This Line?
   if ( indexA == std::string::npos ) {
      // No Comment - Do Nothing;
   } else if ( _strLine.compare( indexA, tokenLength, lineComment )  == 0 ) {
      // Yes - Erase Everything From Index A
      _strLine.erase( indexA );
      _strLine = Utility::trim( _strLine, std::string( " " ) );
   }

   // Indexes For C Style Block Comments
   const std::string blockCommentStart( "/*" );
   const std::string blockCommentEnd( "*/" );   

   static std::string::size_type indexB;
   static std::string::size_type indexC;

   bool blockStartFound = false;
   bool blockEndFound   = false;
   
   indexB = _strLine.find( blockCommentStart );
   indexC = _strLine.find( blockCommentEnd );

   // Is There A C Style Comment On This Line?
   if ( indexB != std::string::npos && _strLine.compare( indexB, tokenLength, blockCommentStart ) == 0 ) {
      _inBlockComment = true;
      blockStartFound = true;
   }

   // Is The C Style End Found On This Line?
   if ( indexC != std::string::npos && _strLine.compare( indexC, tokenLength, blockCommentEnd ) == 0 ) {
      blockEndFound = true;
   }

   if ( _inBlockComment && blockStartFound && blockEndFound ) {
      // Block Comment Starts And Stops On This Line
      _strLine.replace( indexB, (indexC + tokenLength) - indexB, "" );
      _inBlockComment = false;
      blockStartFound = false;
      blockEndFound   = false;
      
      // Check To See If Another Block Comment Starts On This Same Line
      indexB = _strLine.find( blockCommentStart );
      if ( indexB != std::string::npos && _strLine.compare( indexB, tokenLength, blockCommentStart ) == 0 ) {
         _inBlockComment = true;
         removeComments();
      }
   }

   if ( _inBlockComment && blockStartFound && !blockEndFound ) {
      // Block Comment Starts On This Line But Ends On Another
      _strLine.erase( indexB );
      _strLine = Utility::trim( _strLine, std::string( " " ) );
      parseGui();
      getNextLine();
   }

   if ( _inBlockComment && !blockStartFound ) {
      // Block Comment Started On Previous Line Check To See If It Ends On This Line
      if ( indexC != std::string::npos && _strLine.compare( indexC, tokenLength, blockCommentEnd ) == 0 ) {
         // Erase From 0 To indexC + tokenLength
         _strLine.replace( 0, indexC + tokenLength, "" );
         _inBlockComment = false;
         blockEndFound   = false;
      } else {
         _strLine.erase();
         _strLine = Utility::trim( _strLine, std::string( " " ) );
         parseGui();
         getNextLine();
      }

      // Check To See If Another Block Comment Starts On This Same Line
      indexB = _strLine.find( blockCommentStart );
      if ( indexB != std::string::npos && _strLine.compare( indexB, tokenLength, blockCommentStart ) == 0 ) {
         _inBlockComment = true;
         removeComments();
      }
   }

   _strLine = Utility::trim( _strLine, std::string( " " ) );   
   
} // removeComments


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Fri Mar 13, 2015 7:22 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
This solution is not correct. When running this code against one of my test files, it throws an exception even though it should not. You can see this for yourself using the following test file:

Screen id_ScreenRoot {
   LayoutGrid numColumns_3 offset_0,0 align_topLeft size_250,150 color_1,1,0.75,1 padding_10 {
      Image id_keeper1 size_100,50 /*ignore*/ color_0.2,0.2,0.2,1 /*
      Image id_keeper2 size_100,50 color_0.2,0.2,0.2,1
      //Image id_keeper3 size_100,50 color_0.2,0.2,0.2,1*/
      Image id_keeper4 size_100,50 color_0.2,0.2,0.2,1
      Image id_keeper5 size_100,50 color_0.2,0.2,0.2,1
      Image id_keeper6 size_100,50 color_0.2,0.2,0.2,1
   }
}


I have posted VMK75 to the website so everyone can now see my solution.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 74 - Remove Comments Challenge
PostPosted: Fri Mar 13, 2015 11:49 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Okay I'll test it out before I watch the next video.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 11 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 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

Jump to:  

cron