namespace format {
namespace {
+static const char *Blanks = " \t\v\f";
+static bool IsBlank(char C) {
+ switch (C) {
+ case ' ':
+ case '\t':
+ case '\v':
+ case '\f':
+ return true;
+ default:
+ return false;
+ }
+}
+
BreakableToken::Split getCommentSplit(StringRef Text,
unsigned ContentStartColumn,
unsigned ColumnLimit,
MaxSplitBytes +=
encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
- StringRef::size_type SpaceOffset = Text.rfind(' ', MaxSplitBytes);
+ StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
if (SpaceOffset == StringRef::npos ||
// Don't break at leading whitespace.
- Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos) {
+ Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
// Make sure that we don't break at leading whitespace that
// reaches past MaxSplit.
- StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(" ");
+ StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
if (FirstNonWhitespace == StringRef::npos)
// If the comment is only whitespace, we cannot split.
return BreakableToken::Split(StringRef::npos, 0);
- SpaceOffset =
- Text.find(' ', std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
+ SpaceOffset = Text.find_first_of(
+ Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
}
if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
- StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim();
- StringRef AfterCut = Text.substr(SpaceOffset).ltrim();
+ StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
+ StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
return BreakableToken::Split(BeforeCut.size(),
AfterCut.begin() - BeforeCut.end());
}
if (Chars > MaxSplit)
break;
- if (Text[0] == ' ')
+ if (IsBlank(Text[0]))
SpaceOffset = SplitPoint;
if (Text[0] == '/')
SlashOffset = SplitPoint;
- if (Text[0] != '\\' && !isAlphanumeric(Text[0]))
+ if (Advance == 1 && !isAlphanumeric(Text[0]))
WordStartOffset = SplitPoint;
SplitPoint += Advance;
// Calculate the end of the non-whitespace text in the previous line.
EndOfPreviousLine =
- Lines[LineIndex - 1].find_last_not_of(" \t", EndOfPreviousLine);
+ Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
if (EndOfPreviousLine == StringRef::npos)
EndOfPreviousLine = 0;
else
++EndOfPreviousLine;
// Calculate the start of the non-whitespace text in the current line.
- size_t StartOfLine = Lines[LineIndex].find_first_not_of(" \t");
+ size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
if (StartOfLine == StringRef::npos)
StartOfLine = Lines[LineIndex].size();