⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asbeautifier.cpp

📁 Artistic Style is a reindenter and reformatter of C, C++, C# and Java source code. When indentin
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                            // recreate the header list in headerStack up to the previous 'if'                            // from the temporary snapshot stored in lastTempStack.                            int restackSize = lastTempStack->size() - indexOfIf - 1;                            for (int r=0; r<restackSize; r++)                            {                                headerStack->push_back(lastTempStack->back());                                lastTempStack->pop_back();                            }                            if (!closingBracketReached)                                tabCount += restackSize;                        }                        /*                         * If the above if is not true, i.e. no 'if' before the 'else',                         * then nothing beautiful will come out of this...                         * I should think about inserting an Exception here to notify the caller of this...                         */                    }                }                // check if 'while' closes a previous 'do'                else if (newHeader == &AS_WHILE)                {                    if (lastTempStack != NULL)                    {                        int indexOfDo = indexOf(*lastTempStack, &AS_DO); // <---                        if (indexOfDo != -1)                        {                            // recreate the header list in headerStack up to the previous 'do'                            // from the temporary snapshot stored in lastTempStack.                            int restackSize = lastTempStack->size() - indexOfDo - 1;                            for (int r=0; r<restackSize; r++)                            {                                headerStack->push_back(lastTempStack->back());                                lastTempStack->pop_back();                            }                            if (!closingBracketReached)                                tabCount += restackSize;                        }                    }                }                // check if 'catch' closes a previous 'try' or 'catch'                else if (newHeader == &AS_CATCH || newHeader == &AS_FINALLY)                {                    if (lastTempStack != NULL)                    {                        int indexOfTry = indexOf(*lastTempStack, &AS_TRY);                        if (indexOfTry == -1)                            indexOfTry = indexOf(*lastTempStack, &AS_CATCH);                        if (indexOfTry != -1)                        {                            // recreate the header list in headerStack up to the previous 'try'                            // from the temporary snapshot stored in lastTempStack.                            int restackSize = lastTempStack->size() - indexOfTry - 1;                            for (int r=0; r<restackSize; r++)                            {                                headerStack->push_back(lastTempStack->back());                                lastTempStack->pop_back();                            }                            if (!closingBracketReached)                                tabCount += restackSize;                        }                    }                }                else if (newHeader == &AS_CASE)                {                    isInCase = true;                    if (!caseIndent)                        --tabCount;                }                else if(newHeader == &AS_DEFAULT)                {                    isInCase = true;                    if (!caseIndent)                        --tabCount;                }                else if (newHeader == &AS_PUBLIC || newHeader == &AS_PROTECTED || newHeader == &AS_PRIVATE)                {                    if (isCStyle && !isInClassHeader)                        --tabCount;                    isIndentableHeader = false;                }                //else if ((newHeader == &STATIC || newHeader == &SYNCHRONIZED) &&                //         !headerStack->empty() &&                //         (headerStack->back() == &STATIC || headerStack->back() == &SYNCHRONIZED))                //{                //    isIndentableHeader = false;                //}                else if (newHeader == &AS_STATIC                         || newHeader == &AS_SYNCHRONIZED                         || (newHeader == &AS_CONST && isCStyle))                {                    if (!headerStack->empty() &&                            (headerStack->back() == &AS_STATIC                             || headerStack->back() == &AS_SYNCHRONIZED                             || headerStack->back() == &AS_CONST))                    {                        isIndentableHeader = false;                    }                    else                    {                        isIndentableHeader = false;                        probationHeader = newHeader;                    }                }                else if (newHeader == &AS_CONST)                {                    // this will be entered only if NOT in C style                    // since otherwise the CONST would be found to be a probstion header...                    //if (isCStyle)                    //  isInConst = true;                    isIndentableHeader = false;                }                /*                              else if (newHeader == &OPERATOR)                              {                                  if (isCStyle)                                      isInOperator = true;                                  isIndentableHeader = false;                              }                */                else if (newHeader == &AS_TEMPLATE)                {                    if (isCStyle)                        isInTemplate = true;                    isIndentableHeader = false;                }                if (isIndentableHeader)                {                    // 3.2.99                    //spaceTabCount-=indentLength;                    headerStack->push_back(newHeader);                    isInStatement = false;                    if (indexOf(nonParenHeaders, newHeader) == -1)                    {                        isInConditional = true;                    }                    lastLineHeader = newHeader;                }                else                    isInHeader = false;                //lastLineHeader = newHeader;                outBuffer.append(newHeader->substr(1));                i += newHeader->length() - 1;                continue;            }        }        if (isCStyle && !isalpha(prevCh)                && line.COMPARE(i, 8, AS_OPERATOR) == 0 && !isalnum(line[i+8]))        {            isInOperator = true;            outBuffer.append(AS_OPERATOR.substr(1));            i += 7;            continue;        }        if (ch == '?')            isInQuestion = true;        // special handling of 'case' statements        if (ch == ':')        {            if (line.length() > i+1 && line[i+1] == ':') // look for ::            {                ++i;                outBuffer.append(1, ':');                ch = ' ';                continue;            }
            else if (isCStyle && isInClass && prevNonSpaceCh != ')')            {                --tabCount;                // found a 'private:' or 'public:' inside a class definition                // so do nothing special            }            else if (isCStyle && isInClassHeader)            {                // found a 'class A : public B' definition                // so do nothing special            }            else if (isInQuestion)            {                isInQuestion = false;            }            else if (isCStyle && prevNonSpaceCh == ')')            {                isInClassHeader = true;                if (i==0)                    tabCount += 2;            }            else            {                currentNonSpaceCh = ';'; // so that brackets after the ':' will appear as block-openers                if (isInCase)                {                    isInCase = false;                    ch = ';'; // from here on, treat char as ';'                }                else // is in a label (e.g. 'label1:')                {                    if (labelIndent)                        --tabCount; // unindent label by one indent                    else                        tabCount = 0; // completely flush indent to left                }            }        }        if ((ch == ';'  || (parenDepth>0 && ch == ','))  && !inStatementIndentStackSizeStack->empty())            while (inStatementIndentStackSizeStack->back() + (parenDepth>0 ? 1 : 0)  < inStatementIndentStack->size())                inStatementIndentStack->pop_back();        // handle ends of statements        if ( (ch == ';' && parenDepth == 0) || ch == '}'/* || (ch == ',' && parenDepth == 0)*/)        {            if (ch == '}')            {                // first check if this '}' closes a previous block, or a static array...                if (!bracketBlockStateStack->empty())                {                    bool bracketBlockState = bracketBlockStateStack->back();                    bracketBlockStateStack->pop_back();                    if (!bracketBlockState)                    {                        if (!inStatementIndentStackSizeStack->empty())                        {                            // this bracket is a static array                            int previousIndentStackSize = inStatementIndentStackSizeStack->back();                            inStatementIndentStackSizeStack->pop_back();                            while (previousIndentStackSize < inStatementIndentStack->size())                                inStatementIndentStack->pop_back();                            parenDepth--;                            if (i == 0)                                shouldIndentBrackettedLine = false;                            if (!parenIndentStack->empty())                            {                                int poppedIndent = parenIndentStack->back();                                parenIndentStack->pop_back();                                if (i == 0)                                    spaceTabCount = poppedIndent;                            }                        }                        continue;                    }                }                // this bracket is block closer...                ++lineClosingBlocksNum;                if(!inStatementIndentStackSizeStack->empty())                    inStatementIndentStackSizeStack->pop_back();                if (!blockParenDepthStack->empty())                {                    parenDepth = blockParenDepthStack->back();                    blockParenDepthStack->pop_back();                    isInStatement = blockStatementStack->back();                    blockStatementStack->pop_back();                    if (isInStatement)                        blockTabCount--;                }                closingBracketReached = true;                int headerPlace = indexOf(*headerStack, &AS_OPEN_BRACKET); // <---                if (headerPlace != -1)                {                    const string *popped = headerStack->back();                    while (popped != &AS_OPEN_BRACKET)                    {                        headerStack->pop_back();                        popped = headerStack->back();                    }                    headerStack->pop_back();                    if (!tempStacks->empty())                    {                        vector<const string*> *temp =  tempStacks->back();                        tempStacks->pop_back();                        delete temp;                    }                }                ch = ' '; // needed due to cases such as '}else{', so that headers ('else' tn tih case) will be identified...            }            /*             * Create a temporary snapshot of the current block's header-list in the             * uppermost inner stack in tempStacks, and clear the headerStack up to             * the begining of the block.             * Thus, the next future statement will think it comes one indent past             * the block's '{' unless it specifically checks for a companion-header             * (such as a previous 'if' for an 'else' header) within the tempStacks,             * and recreates the temporary snapshot by manipulating the tempStacks.             */            if (!tempStacks->back()->empty())                while (!tempStacks->back()->empty())                    tempStacks->back()->pop_back();            while (!headerStack->empty() && headerStack->back() != &AS_OPEN_BRACKET)            {                tempStacks->back()->push_back(headerStack->back());                headerStack->pop_back();            }            if (parenDepth == 0 && ch == ';')                isInStatement=false;            isInClassHeader = false;            continue;        }        // check for preBlockStatements ONLY if not within parenthesies        // (otherwise 'struct XXX' statements would be wrongly interpreted...)        if (prevCh == ' ' && !isInTemplate && parenDepth == 0)        {            const string *newHeader = findHeader(line, i, preBlockStatements);            if (newHeader != NULL)            {                isInClassHeader = true;                outBuffer.append(newHeader->substr(1));                i += newHeader->length() - 1;                //if (isCStyle)                headerStack->push_back(newHeader);            }        }
		// Handle operators
		//
////        // PRECHECK if a '==' or '--' or '++' operator was reached.////        // If not, then register an indent IF an assignment operator was reached.////        // The precheck is important, so that statements such as 'i--==2' are not recognized////        // to have assignment operators (here, '-=') in them . . .

		const string *foundAssignmentOp = NULL;
		const string *foundNonAssignmentOp = NULL;

        immediatelyPreviousAssignmentOp = NULL;

		// Check if an operator has been reached.
		foundAssignmentOp = findHeader(line, i, assignmentOperators, false); 
		foundNonAssignmentOp = findHeader(line, i, nonAssignmentOperators, false); 

		// Since findHeader's boundry checking was not used above, it is possible
		// that both an assignment op and a non-assignment op where found,
		// e.g. '>>' and '>>='. If this is the case, treat the LONGER one as the 
		// found operator.
		if (foundAssignmentOp != NULL && foundNonAssignmentOp != NULL)
			if (foundAssignmentOp->length() < foundNonAssignmentOp->length())
				foundAssignmentOp = NULL;
			else
				foundNonAssignmentOp = NULL;

		if (foundNonAssignmentOp != NULL)
		{
			if (foundNonAssignmentOp->length() > 1)
            {
                outBuffer.append(foundNonAssignmentOp->substr(1));
                i += foundNonAssignmentOp->length() - 1;
            }
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -