@@ -69,6 +69,12 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
6969 if (SkipBinary && SkipDecimal && SkipHex)
7070 return {};
7171
72+ const auto BinaryMinDigits =
73+ std::max ((int )Option.BinaryMinDigits , Binary + 1 );
74+ const auto DecimalMinDigits =
75+ std::max ((int )Option.DecimalMinDigits , Decimal + 1 );
76+ const auto HexMinDigits = std::max ((int )Option.HexMinDigits , Hex + 1 );
77+
7278 const auto &SourceMgr = Env.getSourceManager ();
7379 AffectedRangeManager AffectedRangeMgr (SourceMgr, Env.getCharRanges ());
7480
@@ -116,11 +122,6 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
116122 (IsBase16 && Text.find_last_of (" .pP" ) != StringRef::npos)) {
117123 continue ;
118124 }
119- if (((IsBase2 && Binary < 0 ) || (IsBase10 && Decimal < 0 ) ||
120- (IsBase16 && Hex < 0 )) &&
121- Text.find (Separator) == StringRef::npos) {
122- continue ;
123- }
124125 const auto Start = Text[0 ] == ' 0' ? 2 : 0 ;
125126 auto End = Text.find_first_of (" uUlLzZn" , Start);
126127 if (End == StringRef::npos)
@@ -130,13 +131,25 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
130131 Text = Text.substr (Start, Length);
131132 }
132133 auto DigitsPerGroup = Decimal;
133- if (IsBase2)
134+ auto MinDigits = DecimalMinDigits;
135+ if (IsBase2) {
134136 DigitsPerGroup = Binary;
135- else if (IsBase16)
137+ MinDigits = BinaryMinDigits;
138+ } else if (IsBase16) {
136139 DigitsPerGroup = Hex;
137- if (DigitsPerGroup > 0 && checkSeparator (Text, DigitsPerGroup))
140+ MinDigits = HexMinDigits;
141+ }
142+ const auto SeparatorCount = Text.count (Separator);
143+ const int DigitCount = Length - SeparatorCount;
144+ const bool RemoveSeparator = DigitsPerGroup < 0 || DigitCount < MinDigits;
145+ if (RemoveSeparator && SeparatorCount == 0 )
138146 continue ;
139- const auto &Formatted = format (Text, DigitsPerGroup);
147+ if (!RemoveSeparator && SeparatorCount > 0 &&
148+ checkSeparator (Text, DigitsPerGroup)) {
149+ continue ;
150+ }
151+ const auto &Formatted =
152+ format (Text, DigitsPerGroup, DigitCount, RemoveSeparator);
140153 assert (Formatted != Text);
141154 if (Start > 0 )
142155 Location = Location.getLocWithOffset (Start);
@@ -168,23 +181,20 @@ bool IntegerLiteralSeparatorFixer::checkSeparator(
168181}
169182
170183std::string IntegerLiteralSeparatorFixer::format (const StringRef IntegerLiteral,
171- int DigitsPerGroup) const {
184+ int DigitsPerGroup,
185+ int DigitCount,
186+ bool RemoveSeparator) const {
172187 assert (DigitsPerGroup != 0 );
173188
174189 std::string Formatted;
175190
176- if (DigitsPerGroup < 0 ) {
191+ if (RemoveSeparator ) {
177192 for (auto C : IntegerLiteral)
178193 if (C != Separator)
179194 Formatted.push_back (C);
180195 return Formatted;
181196 }
182197
183- int DigitCount = 0 ;
184- for (auto C : IntegerLiteral)
185- if (C != Separator)
186- ++DigitCount;
187-
188198 int Remainder = DigitCount % DigitsPerGroup;
189199
190200 int I = 0 ;
0 commit comments