ASEnhancer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. // ASEnhancer.cpp
  2. // Copyright (c) 2017 by Jim Pattee <jimp03@email.com>.
  3. // This code is licensed under the MIT License.
  4. // License.md describes the conditions under which this software may be distributed.
  5. //-----------------------------------------------------------------------------
  6. // headers
  7. //-----------------------------------------------------------------------------
  8. #include "astyle.h"
  9. //-----------------------------------------------------------------------------
  10. // astyle namespace
  11. //-----------------------------------------------------------------------------
  12. namespace astyle {
  13. //
  14. //-----------------------------------------------------------------------------
  15. // ASEnhancer class
  16. //-----------------------------------------------------------------------------
  17. /**
  18. * ASEnhancer constructor
  19. */
  20. ASEnhancer::ASEnhancer()
  21. {
  22. }
  23. /**
  24. * Destructor of ASEnhancer
  25. */
  26. ASEnhancer::~ASEnhancer()
  27. {
  28. }
  29. /**
  30. * initialize the ASEnhancer.
  31. *
  32. * init() is called each time an ASFormatter object is initialized.
  33. */
  34. void ASEnhancer::init(int _fileType,
  35. int _indentLength,
  36. int _tabLength,
  37. bool _useTabs,
  38. bool _forceTab,
  39. bool _namespaceIndent,
  40. bool _caseIndent,
  41. bool _preprocBlockIndent,
  42. bool _preprocDefineIndent,
  43. bool _emptyLineFill,
  44. vector<const pair<const string, const string>* >* _indentableMacros)
  45. {
  46. // formatting variables from ASFormatter and ASBeautifier
  47. ASBase::init(_fileType);
  48. indentLength = _indentLength;
  49. tabLength = _tabLength;
  50. useTabs = _useTabs;
  51. forceTab = _forceTab;
  52. namespaceIndent = _namespaceIndent;
  53. caseIndent = _caseIndent;
  54. preprocBlockIndent = _preprocBlockIndent;
  55. preprocDefineIndent = _preprocDefineIndent;
  56. emptyLineFill = _emptyLineFill;
  57. indentableMacros = _indentableMacros;
  58. quoteChar = '\'';
  59. // unindent variables
  60. lineNumber = 0;
  61. braceCount = 0;
  62. isInComment = false;
  63. isInQuote = false;
  64. switchDepth = 0;
  65. eventPreprocDepth = 0;
  66. lookingForCaseBrace = false;
  67. unindentNextLine = false;
  68. shouldUnindentLine = false;
  69. shouldUnindentComment = false;
  70. // switch struct and vector
  71. sw.switchBraceCount = 0;
  72. sw.unindentDepth = 0;
  73. sw.unindentCase = false;
  74. switchStack.clear();
  75. // other variables
  76. nextLineIsEventIndent = false;
  77. isInEventTable = false;
  78. nextLineIsDeclareIndent = false;
  79. isInDeclareSection = false;
  80. }
  81. /**
  82. * additional formatting for line of source code.
  83. * every line of source code in a source code file should be sent
  84. * one after the other to this function.
  85. * indents event tables
  86. * unindents the case blocks
  87. *
  88. * @param line the original formatted line will be updated if necessary.
  89. */
  90. void ASEnhancer::enhance(string& line, bool isInNamespace, bool isInPreprocessor, bool isInSQL)
  91. {
  92. shouldUnindentLine = true;
  93. shouldUnindentComment = false;
  94. lineNumber++;
  95. // check for beginning of event table
  96. if (nextLineIsEventIndent)
  97. {
  98. isInEventTable = true;
  99. nextLineIsEventIndent = false;
  100. }
  101. // check for beginning of SQL declare section
  102. if (nextLineIsDeclareIndent)
  103. {
  104. isInDeclareSection = true;
  105. nextLineIsDeclareIndent = false;
  106. }
  107. if (line.length() == 0
  108. && !isInEventTable
  109. && !isInDeclareSection
  110. && !emptyLineFill)
  111. return;
  112. // test for unindent on attached braces
  113. if (unindentNextLine)
  114. {
  115. sw.unindentDepth++;
  116. sw.unindentCase = true;
  117. unindentNextLine = false;
  118. }
  119. // parse characters in the current line
  120. parseCurrentLine(line, isInPreprocessor, isInSQL);
  121. // check for SQL indentable lines
  122. if (isInDeclareSection)
  123. {
  124. size_t firstText = line.find_first_not_of(" \t");
  125. if (firstText == string::npos || line[firstText] != '#')
  126. indentLine(line, 1);
  127. }
  128. // check for event table indentable lines
  129. if (isInEventTable
  130. && (eventPreprocDepth == 0
  131. || (namespaceIndent && isInNamespace)))
  132. {
  133. size_t firstText = line.find_first_not_of(" \t");
  134. if (firstText == string::npos || line[firstText] != '#')
  135. indentLine(line, 1);
  136. }
  137. if (shouldUnindentComment && sw.unindentDepth > 0)
  138. unindentLine(line, sw.unindentDepth - 1);
  139. else if (shouldUnindentLine && sw.unindentDepth > 0)
  140. unindentLine(line, sw.unindentDepth);
  141. }
  142. /**
  143. * convert a force-tab indent to spaces
  144. *
  145. * @param line a reference to the line that will be converted.
  146. */
  147. void ASEnhancer::convertForceTabIndentToSpaces(string& line) const
  148. {
  149. // replace tab indents with spaces
  150. for (size_t i = 0; i < line.length(); i++)
  151. {
  152. if (!isWhiteSpace(line[i]))
  153. break;
  154. if (line[i] == '\t')
  155. {
  156. line.erase(i, 1);
  157. line.insert(i, tabLength, ' ');
  158. i += tabLength - 1;
  159. }
  160. }
  161. }
  162. /**
  163. * convert a space indent to force-tab
  164. *
  165. * @param line a reference to the line that will be converted.
  166. */
  167. void ASEnhancer::convertSpaceIndentToForceTab(string& line) const
  168. {
  169. assert(tabLength > 0);
  170. // replace leading spaces with tab indents
  171. size_t newSpaceIndentLength = line.find_first_not_of(" \t");
  172. size_t tabCount = newSpaceIndentLength / tabLength; // truncate extra spaces
  173. line.replace(0U, tabCount * tabLength, tabCount, '\t');
  174. }
  175. /**
  176. * find the colon following a 'case' statement
  177. *
  178. * @param line a reference to the line.
  179. * @param caseIndex the line index of the case statement.
  180. * @return the line index of the colon.
  181. */
  182. size_t ASEnhancer::findCaseColon(const string& line, size_t caseIndex) const
  183. {
  184. size_t i = caseIndex;
  185. bool isInQuote_ = false;
  186. char quoteChar_ = ' ';
  187. for (; i < line.length(); i++)
  188. {
  189. if (isInQuote_)
  190. {
  191. if (line[i] == '\\')
  192. {
  193. i++;
  194. continue;
  195. }
  196. else if (line[i] == quoteChar_) // check ending quote
  197. {
  198. isInQuote_ = false;
  199. quoteChar_ = ' ';
  200. continue;
  201. }
  202. else
  203. {
  204. continue; // must close quote before continuing
  205. }
  206. }
  207. if (line[i] == '"' // check opening quote
  208. || (line[i] == '\'' && !isDigitSeparator(line, i)))
  209. {
  210. isInQuote_ = true;
  211. quoteChar_ = line[i];
  212. continue;
  213. }
  214. if (line[i] == ':')
  215. {
  216. if ((i + 1 < line.length()) && (line[i + 1] == ':'))
  217. i++; // bypass scope resolution operator
  218. else
  219. break; // found it
  220. }
  221. }
  222. return i;
  223. }
  224. /**
  225. * indent a line by a given number of tabsets
  226. * by inserting leading whitespace to the line argument.
  227. *
  228. * @param line a reference to the line to indent.
  229. * @param indent the number of tabsets to insert.
  230. * @return the number of characters inserted.
  231. */
  232. int ASEnhancer::indentLine(string& line, int indent) const
  233. {
  234. if (line.length() == 0
  235. && !emptyLineFill)
  236. return 0;
  237. size_t charsToInsert = 0;
  238. if (forceTab && indentLength != tabLength)
  239. {
  240. // replace tab indents with spaces
  241. convertForceTabIndentToSpaces(line);
  242. // insert the space indents
  243. charsToInsert = indent * indentLength;
  244. line.insert(line.begin(), charsToInsert, ' ');
  245. // replace leading spaces with tab indents
  246. convertSpaceIndentToForceTab(line);
  247. }
  248. else if (useTabs)
  249. {
  250. charsToInsert = indent;
  251. line.insert(line.begin(), charsToInsert, '\t');
  252. }
  253. else // spaces
  254. {
  255. charsToInsert = indent * indentLength;
  256. line.insert(line.begin(), charsToInsert, ' ');
  257. }
  258. return charsToInsert;
  259. }
  260. /**
  261. * check for SQL "BEGIN DECLARE SECTION".
  262. * must compare case insensitive and allow any spacing between words.
  263. *
  264. * @param line a reference to the line to indent.
  265. * @param index the current line index.
  266. * @return true if a hit.
  267. */
  268. bool ASEnhancer::isBeginDeclareSectionSQL(const string& line, size_t index) const
  269. {
  270. string word;
  271. size_t hits = 0;
  272. size_t i;
  273. for (i = index; i < line.length(); i++)
  274. {
  275. i = line.find_first_not_of(" \t", i);
  276. if (i == string::npos)
  277. return false;
  278. if (line[i] == ';')
  279. break;
  280. if (!isCharPotentialHeader(line, i))
  281. continue;
  282. word = getCurrentWord(line, i);
  283. for (size_t j = 0; j < word.length(); j++)
  284. word[j] = (char) toupper(word[j]);
  285. if (word == "EXEC" || word == "SQL")
  286. {
  287. i += word.length() - 1;
  288. continue;
  289. }
  290. if (word == "DECLARE" || word == "SECTION")
  291. {
  292. hits++;
  293. i += word.length() - 1;
  294. continue;
  295. }
  296. if (word == "BEGIN")
  297. {
  298. hits++;
  299. i += word.length() - 1;
  300. continue;
  301. }
  302. return false;
  303. }
  304. if (hits == 3)
  305. return true;
  306. return false;
  307. }
  308. /**
  309. * check for SQL "END DECLARE SECTION".
  310. * must compare case insensitive and allow any spacing between words.
  311. *
  312. * @param line a reference to the line to indent.
  313. * @param index the current line index.
  314. * @return true if a hit.
  315. */
  316. bool ASEnhancer::isEndDeclareSectionSQL(const string& line, size_t index) const
  317. {
  318. string word;
  319. size_t hits = 0;
  320. size_t i;
  321. for (i = index; i < line.length(); i++)
  322. {
  323. i = line.find_first_not_of(" \t", i);
  324. if (i == string::npos)
  325. return false;
  326. if (line[i] == ';')
  327. break;
  328. if (!isCharPotentialHeader(line, i))
  329. continue;
  330. word = getCurrentWord(line, i);
  331. for (size_t j = 0; j < word.length(); j++)
  332. word[j] = (char) toupper(word[j]);
  333. if (word == "EXEC" || word == "SQL")
  334. {
  335. i += word.length() - 1;
  336. continue;
  337. }
  338. if (word == "DECLARE" || word == "SECTION")
  339. {
  340. hits++;
  341. i += word.length() - 1;
  342. continue;
  343. }
  344. if (word == "END")
  345. {
  346. hits++;
  347. i += word.length() - 1;
  348. continue;
  349. }
  350. return false;
  351. }
  352. if (hits == 3)
  353. return true;
  354. return false;
  355. }
  356. /**
  357. * check if a one-line brace has been reached,
  358. * i.e. if the currently reached '{' character is closed
  359. * with a complimentary '}' elsewhere on the current line,
  360. *.
  361. * @return false = one-line brace has not been reached.
  362. * true = one-line brace has been reached.
  363. */
  364. bool ASEnhancer::isOneLineBlockReached(const string& line, int startChar) const
  365. {
  366. assert(line[startChar] == '{');
  367. bool isInComment_ = false;
  368. bool isInQuote_ = false;
  369. int _braceCount = 1;
  370. int lineLength = line.length();
  371. char quoteChar_ = ' ';
  372. char ch = ' ';
  373. for (int i = startChar + 1; i < lineLength; ++i)
  374. {
  375. ch = line[i];
  376. if (isInComment_)
  377. {
  378. if (line.compare(i, 2, "*/") == 0)
  379. {
  380. isInComment_ = false;
  381. ++i;
  382. }
  383. continue;
  384. }
  385. if (ch == '\\')
  386. {
  387. ++i;
  388. continue;
  389. }
  390. if (isInQuote_)
  391. {
  392. if (ch == quoteChar_)
  393. isInQuote_ = false;
  394. continue;
  395. }
  396. if (ch == '"'
  397. || (ch == '\'' && !isDigitSeparator(line, i)))
  398. {
  399. isInQuote_ = true;
  400. quoteChar_ = ch;
  401. continue;
  402. }
  403. if (line.compare(i, 2, "//") == 0)
  404. break;
  405. if (line.compare(i, 2, "/*") == 0)
  406. {
  407. isInComment_ = true;
  408. ++i;
  409. continue;
  410. }
  411. if (ch == '{')
  412. ++_braceCount;
  413. else if (ch == '}')
  414. --_braceCount;
  415. if (_braceCount == 0)
  416. return true;
  417. }
  418. return false;
  419. }
  420. /**
  421. * parse characters in the current line to determine if an indent
  422. * or unindent is needed.
  423. */
  424. void ASEnhancer::parseCurrentLine(string& line, bool isInPreprocessor, bool isInSQL)
  425. {
  426. bool isSpecialChar = false; // is a backslash escape character
  427. for (size_t i = 0; i < line.length(); i++)
  428. {
  429. char ch = line[i];
  430. // bypass whitespace
  431. if (isWhiteSpace(ch))
  432. continue;
  433. // handle special characters (i.e. backslash+character such as \n, \t, ...)
  434. if (isSpecialChar)
  435. {
  436. isSpecialChar = false;
  437. continue;
  438. }
  439. if (!(isInComment) && line.compare(i, 2, "\\\\") == 0)
  440. {
  441. i++;
  442. continue;
  443. }
  444. if (!(isInComment) && ch == '\\')
  445. {
  446. isSpecialChar = true;
  447. continue;
  448. }
  449. // handle quotes (such as 'x' and "Hello Dolly")
  450. if (!isInComment
  451. && (ch == '"'
  452. || (ch == '\'' && !isDigitSeparator(line, i))))
  453. {
  454. if (!isInQuote)
  455. {
  456. quoteChar = ch;
  457. isInQuote = true;
  458. }
  459. else if (quoteChar == ch)
  460. {
  461. isInQuote = false;
  462. continue;
  463. }
  464. }
  465. if (isInQuote)
  466. continue;
  467. // handle comments
  468. if (!(isInComment) && line.compare(i, 2, "//") == 0)
  469. {
  470. // check for windows line markers
  471. if (line.compare(i + 2, 1, "\xf0") > 0)
  472. lineNumber--;
  473. // unindent if not in case braces
  474. if (line.find_first_not_of(" \t") == i
  475. && sw.switchBraceCount == 1
  476. && sw.unindentCase)
  477. shouldUnindentComment = true;
  478. break; // finished with the line
  479. }
  480. else if (!(isInComment) && line.compare(i, 2, "/*") == 0)
  481. {
  482. // unindent if not in case braces
  483. if (sw.switchBraceCount == 1 && sw.unindentCase)
  484. shouldUnindentComment = true;
  485. isInComment = true;
  486. size_t commentEnd = line.find("*/", i);
  487. if (commentEnd == string::npos)
  488. i = line.length() - 1;
  489. else
  490. i = commentEnd - 1;
  491. continue;
  492. }
  493. else if ((isInComment) && line.compare(i, 2, "*/") == 0)
  494. {
  495. // unindent if not in case braces
  496. if (sw.switchBraceCount == 1 && sw.unindentCase)
  497. shouldUnindentComment = true;
  498. isInComment = false;
  499. i++;
  500. continue;
  501. }
  502. if (isInComment)
  503. {
  504. // unindent if not in case braces
  505. if (sw.switchBraceCount == 1 && sw.unindentCase)
  506. shouldUnindentComment = true;
  507. size_t commentEnd = line.find("*/", i);
  508. if (commentEnd == string::npos)
  509. i = line.length() - 1;
  510. else
  511. i = commentEnd - 1;
  512. continue;
  513. }
  514. // if we have reached this far then we are NOT in a comment or string of special characters
  515. if (line[i] == '{')
  516. braceCount++;
  517. if (line[i] == '}')
  518. braceCount--;
  519. // check for preprocessor within an event table
  520. if (isInEventTable && line[i] == '#' && preprocBlockIndent)
  521. {
  522. string preproc;
  523. preproc = line.substr(i + 1);
  524. if (preproc.substr(0, 2) == "if") // #if, #ifdef, #ifndef)
  525. eventPreprocDepth += 1;
  526. if (preproc.substr(0, 5) == "endif" && eventPreprocDepth > 0)
  527. eventPreprocDepth -= 1;
  528. }
  529. bool isPotentialKeyword = isCharPotentialHeader(line, i);
  530. // ---------------- wxWidgets and MFC macros ----------------------------------
  531. if (isPotentialKeyword)
  532. {
  533. for (size_t j = 0; j < indentableMacros->size(); j++)
  534. {
  535. // 'first' is the beginning macro
  536. if (findKeyword(line, i, indentableMacros->at(j)->first))
  537. {
  538. nextLineIsEventIndent = true;
  539. break;
  540. }
  541. }
  542. for (size_t j = 0; j < indentableMacros->size(); j++)
  543. {
  544. // 'second' is the ending macro
  545. if (findKeyword(line, i, indentableMacros->at(j)->second))
  546. {
  547. isInEventTable = false;
  548. eventPreprocDepth = 0;
  549. break;
  550. }
  551. }
  552. }
  553. // ---------------- process SQL -----------------------------------------------
  554. if (isInSQL)
  555. {
  556. if (isBeginDeclareSectionSQL(line, i))
  557. nextLineIsDeclareIndent = true;
  558. if (isEndDeclareSectionSQL(line, i))
  559. isInDeclareSection = false;
  560. break;
  561. }
  562. // ---------------- process switch statements ---------------------------------
  563. if (isPotentialKeyword && findKeyword(line, i, ASResource::AS_SWITCH))
  564. {
  565. switchDepth++;
  566. switchStack.emplace_back(sw); // save current variables
  567. sw.switchBraceCount = 0;
  568. sw.unindentCase = false; // don't clear case until end of switch
  569. i += 5; // bypass switch statement
  570. continue;
  571. }
  572. // just want unindented case statements from this point
  573. if (caseIndent
  574. || switchDepth == 0
  575. || (isInPreprocessor && !preprocDefineIndent))
  576. {
  577. // bypass the entire word
  578. if (isPotentialKeyword)
  579. {
  580. string name = getCurrentWord(line, i);
  581. i += name.length() - 1;
  582. }
  583. continue;
  584. }
  585. i = processSwitchBlock(line, i);
  586. } // end of for loop * end of for loop * end of for loop * end of for loop
  587. }
  588. /**
  589. * process the character at the current index in a switch block.
  590. *
  591. * @param line a reference to the line to indent.
  592. * @param index the current line index.
  593. * @return the new line index.
  594. */
  595. size_t ASEnhancer::processSwitchBlock(string& line, size_t index)
  596. {
  597. size_t i = index;
  598. bool isPotentialKeyword = isCharPotentialHeader(line, i);
  599. if (line[i] == '{')
  600. {
  601. sw.switchBraceCount++;
  602. if (lookingForCaseBrace) // if 1st after case statement
  603. {
  604. sw.unindentCase = true; // unindenting this case
  605. sw.unindentDepth++;
  606. lookingForCaseBrace = false; // not looking now
  607. }
  608. return i;
  609. }
  610. lookingForCaseBrace = false; // no opening brace, don't indent
  611. if (line[i] == '}')
  612. {
  613. sw.switchBraceCount--;
  614. assert(sw.switchBraceCount <= braceCount);
  615. if (sw.switchBraceCount == 0) // if end of switch statement
  616. {
  617. int lineUnindent = sw.unindentDepth;
  618. if (line.find_first_not_of(" \t") == i
  619. && !switchStack.empty())
  620. lineUnindent = switchStack[switchStack.size() - 1].unindentDepth;
  621. if (shouldUnindentLine)
  622. {
  623. if (lineUnindent > 0)
  624. i -= unindentLine(line, lineUnindent);
  625. shouldUnindentLine = false;
  626. }
  627. switchDepth--;
  628. sw = switchStack.back();
  629. switchStack.pop_back();
  630. }
  631. return i;
  632. }
  633. if (isPotentialKeyword
  634. && (findKeyword(line, i, ASResource::AS_CASE)
  635. || findKeyword(line, i, ASResource::AS_DEFAULT)))
  636. {
  637. if (sw.unindentCase) // if unindented last case
  638. {
  639. sw.unindentCase = false; // stop unindenting previous case
  640. sw.unindentDepth--;
  641. }
  642. i = findCaseColon(line, i);
  643. i++;
  644. for (; i < line.length(); i++) // bypass whitespace
  645. {
  646. if (!isWhiteSpace(line[i]))
  647. break;
  648. }
  649. if (i < line.length())
  650. {
  651. if (line[i] == '{')
  652. {
  653. braceCount++;
  654. sw.switchBraceCount++;
  655. if (!isOneLineBlockReached(line, i))
  656. unindentNextLine = true;
  657. return i;
  658. }
  659. }
  660. lookingForCaseBrace = true;
  661. i--; // need to process this char
  662. return i;
  663. }
  664. if (isPotentialKeyword)
  665. {
  666. string name = getCurrentWord(line, i); // bypass the entire name
  667. i += name.length() - 1;
  668. }
  669. return i;
  670. }
  671. /**
  672. * unindent a line by a given number of tabsets
  673. * by erasing the leading whitespace from the line argument.
  674. *
  675. * @param line a reference to the line to unindent.
  676. * @param unindent the number of tabsets to erase.
  677. * @return the number of characters erased.
  678. */
  679. int ASEnhancer::unindentLine(string& line, int unindent) const
  680. {
  681. size_t whitespace = line.find_first_not_of(" \t");
  682. if (whitespace == string::npos) // if line is blank
  683. whitespace = line.length(); // must remove padding, if any
  684. if (whitespace == 0)
  685. return 0;
  686. size_t charsToErase = 0;
  687. if (forceTab && indentLength != tabLength)
  688. {
  689. // replace tab indents with spaces
  690. convertForceTabIndentToSpaces(line);
  691. // remove the space indents
  692. size_t spaceIndentLength = line.find_first_not_of(" \t");
  693. charsToErase = unindent * indentLength;
  694. if (charsToErase <= spaceIndentLength)
  695. line.erase(0, charsToErase);
  696. else
  697. charsToErase = 0;
  698. // replace leading spaces with tab indents
  699. convertSpaceIndentToForceTab(line);
  700. }
  701. else if (useTabs)
  702. {
  703. charsToErase = unindent;
  704. if (charsToErase <= whitespace)
  705. line.erase(0, charsToErase);
  706. else
  707. charsToErase = 0;
  708. }
  709. else // spaces
  710. {
  711. charsToErase = unindent * indentLength;
  712. if (charsToErase <= whitespace)
  713. line.erase(0, charsToErase);
  714. else
  715. charsToErase = 0;
  716. }
  717. return charsToErase;
  718. }
  719. } // end namespace astyle