Thread: Adding tabulation why omitting specific keyword

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    12

    Adding tabulation why omitting specific keyword

    Hey everyone
    I'm making a program that formats code,but I can't seem to solve certain problems.

    Code:
    while ((finder = str.find(';', finder)) != std::string::npos)
        {
            if (str[finder + 1] != '\n')
            {
                str.insert(str.begin() + finder + 1, '\n');
            }
            else ++finder;
        }
    This code is supposed to add a new line after finding a";" character.
    The problem is,whenever it finds a for loop which contains two such characters it ruins the indention.How can I form an if statement to avoid this?

    I also have problem with this:
    Code:
    while ((finder = str.find('\n', finder)) != std::string::npos)
        {
            if ((str[finder + 1] != '\t') && (str[finder + 1] != '{') && (str[finder + 1] != '}')&&(str[finder+1]!=' '))
            {
                str.insert(str.begin() + finder + 1, '\t');
            }
            else ++finder;
        }
    I want to add a tabulation if there doesn't exist one already,but it ends up adding one anyway.
    Aside of that,is this an acceptable way of making a code beautifier?
    Thanks in advance

    Edit:Oops,there's supposed to be "while" in the topic
    Last edited by Flowz; 05-24-2016 at 12:29 PM.

  2. #2
    Guest
    Guest
    The problem is,whenever it finds a for loop which contains two such characters it ruins the indention.How can I form an if statement to avoid this?
    To avoid future headaches of this kind, it would probably be best if your program was already aware that it's inside a for-loop at that point. Just working with ; could end up adding complexity in another direction. Also consider that for(;;) is legal (if rare).

    Aside of that,is this an acceptable way of making a code beautifier?
    If you do this as an exercise that's fine, but if your intention is to create something usable, then you need to start with a more complex plan from the get go. Many serious auto-indentation tools still struggle with fairly generic syntax. Looking for semi-colons isn't gonna cut it ;)

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Hey this is what I came up with:
    Code:
    while (((finder = str.find(';', finder)) && (finder2 = str.find("for(", finder2))) != std::string::npos)
    	{
    		if (str.find("for(", finder2) == 1)
    		{
    			x = 2;
    			
    		}
    
    
    		if (str[finder + 1] != '\n'&&x == 0)
    		{
    			str.insert(str.begin() + finder + 1, '\n');
    		}
    
    
    		else {
    			++finder;
    			++finder2;
    			--x;
    		}
    	}
    I've made a variable x,that equals 2 when for is encountered and in the next statement it's supposed to add a new line only when x=0,but it doesn't seem to work.Any idea on what may be the problem?I've run it through a debugger and finder2 gets a weird random number.

  4. #4
    Guest
    Guest
    I don't know man, this is a bit much to follow without spending a lot of time on it. Inside your while loop you'll need to compare each return value from find against std::string::npos separately. Right now it looks like you're ANDing the two terms which means your left terms ends up being a bool, not a std::string::size_type.
    Code:
    while (((finder = str.find(';', finder)) != str.npos && (finder2 = str.find("for(", finder2)) != str.npos)
    And how do you presume that the find on line 3 returns 1? Inside the string you're processing, it could be a much higher value, no?

    But again, without spending a lot of time on this, I probably won't be of much help, sorry.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    It's ok,I understand what you mean.
    What I tried to do with that if statement was if the statement is true (if it does find "for") then execute line 5.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    To solve this problem properly you'd have to write a language parser.

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Heck I know nothing about that whatsoever I'm just starting with programming

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You don't really need a full fledged parser..

    Maintain separate states for the following (might have missed some cases!)
    1. Inside a string literal.
    2. Inside a char.
    3. Inside a single line comment
    4. Inside a multi line comment.
    These are (somewhat!) easy.
    For detecting whether you are inside the for(...), you need a stack.
    If you see the string for and are not on any of the above 4 states, if you see a ( push it on the stack, and when you see ) pop a ( off the stack.

    Now before inserting newlines when you see a semicolon, check that all the states are not set and that stack is empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uint8_t omitting from 0x00 value
    By mahece28 in forum C Programming
    Replies: 4
    Last Post: 06-23-2015, 01:02 PM
  2. adding specific chars from file to an array
    By CsdJohn in forum C Programming
    Replies: 1
    Last Post: 01-29-2014, 07:50 AM
  3. Replies: 13
    Last Post: 11-27-2011, 04:45 PM
  4. omitting an empty line
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 04-19-2008, 12:33 PM
  5. Tabulation, need help!
    By henz321 in forum C++ Programming
    Replies: 0
    Last Post: 08-30-2005, 10:12 AM

Tags for this Thread