summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Turbov <i.zaufi@gmail.com>2022-08-23 05:40:22 (GMT)
committerAlex Turbov <i.zaufi@gmail.com>2022-11-17 12:37:07 (GMT)
commitcf39773df94d474f9618f046be8fbc503dc7159d (patch)
treef4e39d1a16f6c5b00aef862dd056341d8ac2334c
parent1d6c8797fc5ec28f548defd9e8e2d3131e6f3036 (diff)
downloadCMake-cf39773df94d474f9618f046be8fbc503dc7159d.zip
CMake-cf39773df94d474f9618f046be8fbc503dc7159d.tar.gz
CMake-cf39773df94d474f9618f046be8fbc503dc7159d.tar.bz2
cmDocumentationFormatter: Turn `TextIndent` member into `size_t`
… instead of `std::string`.
-rw-r--r--Source/cmDocumentation.cxx2
-rw-r--r--Source/cmDocumentationFormatter.cxx52
-rw-r--r--Source/cmDocumentationFormatter.h4
-rw-r--r--Source/cmMessenger.cxx2
4 files changed, 33 insertions, 27 deletions
diff --git a/Source/cmDocumentation.cxx b/Source/cmDocumentation.cxx
index 77bc7c6..e8558fd 100644
--- a/Source/cmDocumentation.cxx
+++ b/Source/cmDocumentation.cxx
@@ -605,7 +605,7 @@ bool cmDocumentation::PrintHelpListGenerators(std::ostream& os)
{
const auto si = this->AllSections.find("Generators");
if (si != this->AllSections.end()) {
- this->Formatter.SetIndent(" ");
+ this->Formatter.SetIndent(2u);
this->Formatter.PrintSection(os, si->second);
}
return true;
diff --git a/Source/cmDocumentationFormatter.cxx b/Source/cmDocumentationFormatter.cxx
index 527e755..e2c521d 100644
--- a/Source/cmDocumentationFormatter.cxx
+++ b/Source/cmDocumentationFormatter.cxx
@@ -7,17 +7,13 @@
#include <iomanip>
#include <ostream>
#include <string>
+#include <utility>
#include <vector>
#include "cmDocumentationEntry.h"
#include "cmDocumentationSection.h"
-#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
-namespace {
-const std::string NAME_SIZED_PADDING = " ";
-}
-
void cmDocumentationFormatter::PrintFormatted(std::ostream& os,
const char* text)
{
@@ -59,11 +55,11 @@ void cmDocumentationFormatter::PrintFormatted(std::ostream& os,
void cmDocumentationFormatter::PrintPreformatted(std::ostream& os,
std::string const& text) const
{
- if (!this->TextIndent.empty()) {
+ if (this->TextIndent) {
auto indented = text;
- cmSystemTools::ReplaceString(indented, "\n",
- cmStrCat('\n', this->TextIndent));
- indented.insert(0u, this->TextIndent);
+ auto padding = std::string(this->TextIndent, ' ');
+ cmSystemTools::ReplaceString(indented, "\n", "\n" + padding);
+ indented = std::move(padding) + indented;
os << indented << '\n';
} else {
os << text << '\n';
@@ -73,7 +69,9 @@ void cmDocumentationFormatter::PrintPreformatted(std::ostream& os,
void cmDocumentationFormatter::PrintParagraph(std::ostream& os,
const char* text)
{
- os << this->TextIndent;
+ if (this->TextIndent) {
+ os << std::string(this->TextIndent, ' ');
+ }
this->PrintColumn(os, text);
os << '\n';
}
@@ -85,7 +83,7 @@ void cmDocumentationFormatter::PrintColumn(std::ostream& os, const char* text)
long column = 0;
bool newSentence = false;
bool firstLine = true;
- int width = this->TextWidth - static_cast<int>(this->TextIndent.size());
+ int width = this->TextWidth - static_cast<int>(this->TextIndent);
// Loop until the end of the text.
while (*l) {
@@ -109,10 +107,10 @@ void cmDocumentationFormatter::PrintColumn(std::ostream& os, const char* text)
os << ' ';
column += 1;
}
- } else if (!firstLine && !this->TextIndent.empty()) {
+ } else if (!firstLine && this->TextIndent) {
// First word on line. Print indentation unless this is the
// first line.
- os << this->TextIndent;
+ os << std::string(this->TextIndent, ' ');
}
// Print the word.
@@ -135,7 +133,7 @@ void cmDocumentationFormatter::PrintColumn(std::ostream& os, const char* text)
os << '\n';
firstLine = false;
if (r > l) {
- os << this->TextIndent;
+ os << std::string(this->TextIndent, ' ');
os.write(l, static_cast<long>(r - l));
column = static_cast<long>(r - l);
newSentence = (*(r - 1) == '.');
@@ -157,23 +155,31 @@ void cmDocumentationFormatter::PrintSection(
{
os << section.GetName() << '\n';
- const std::vector<cmDocumentationEntry>& entries = section.GetEntries();
- for (cmDocumentationEntry const& entry : entries) {
+ const std::size_t PREFIX_SIZE =
+ sizeof(cmDocumentationEntry::CustomNamePrefix) + 1u;
+ // length of the "= " literal (see below)
+ const std::size_t SUFFIX_SIZE = 2u;
+ // legacy magic number ;-)
+ const std::size_t NAME_SIZE = 29u;
+
+ const std::size_t PADDING_SIZE = PREFIX_SIZE + SUFFIX_SIZE;
+ const std::size_t TITLE_SIZE = NAME_SIZE + PADDING_SIZE;
+
+ for (cmDocumentationEntry const& entry : section.GetEntries()) {
if (!entry.Name.empty()) {
- this->TextIndent = NAME_SIZED_PADDING;
- os << std::setw(2) << std::left << entry.CustomNamePrefix
- << std::setw(
- int(std::max(this->TextIndent.size() - 4, entry.Name.size())))
+ this->TextIndent = TITLE_SIZE;
+ os << std::setw(PREFIX_SIZE) << std::left << entry.CustomNamePrefix
+ << std::setw(int(std::max(NAME_SIZE, entry.Name.size())))
<< entry.Name;
- if (entry.Name.size() > (this->TextIndent.size() - 4)) {
- os << '\n' << std::setw(int(this->TextIndent.size() - 2)) << ' ';
+ if (entry.Name.size() > NAME_SIZE) {
+ os << '\n' << std::setw(int(this->TextIndent - PREFIX_SIZE)) << ' ';
}
os << "= ";
this->PrintColumn(os, entry.Brief.c_str());
os << '\n';
} else {
os << '\n';
- this->TextIndent = {};
+ this->TextIndent = 0u;
this->PrintFormatted(os, entry.Brief.c_str());
}
}
diff --git a/Source/cmDocumentationFormatter.h b/Source/cmDocumentationFormatter.h
index 003cf89..5293033 100644
--- a/Source/cmDocumentationFormatter.h
+++ b/Source/cmDocumentationFormatter.h
@@ -18,9 +18,9 @@ public:
void PrintSection(std::ostream& os, cmDocumentationSection const& section);
void PrintParagraph(std::ostream& os, const char* text);
void PrintColumn(std::ostream& os, const char* text);
- void SetIndent(std::string indent) { this->TextIndent = std::move(indent); }
+ void SetIndent(std::size_t indent) { this->TextIndent = indent; }
private:
int TextWidth = 77;
- std::string TextIndent = {};
+ std::size_t TextIndent = 0u;
};
diff --git a/Source/cmMessenger.cxx b/Source/cmMessenger.cxx
index 333003b..cae7216 100644
--- a/Source/cmMessenger.cxx
+++ b/Source/cmMessenger.cxx
@@ -107,7 +107,7 @@ static void printMessageText(std::ostream& msg, std::string const& text)
{
msg << ":\n";
cmDocumentationFormatter formatter;
- formatter.SetIndent(" ");
+ formatter.SetIndent(2u);
formatter.PrintFormatted(msg, text.c_str());
}