CMS 3D CMS Logo

DocFormatHelper.cc
Go to the documentation of this file.
1 
3 
4 #include <ostream>
5 #include <iomanip>
6 
7 namespace edm {
8 
9  namespace {
10  void wrapAndPrintLine(std::ostream& os, std::string const& text, size_t indent, size_t suggestedWidth) {
11  char oldFill = os.fill();
12 
13  size_t length = text.size();
14 
15  // The position in the text where we start printing the next line
16  size_t startLine = 0U;
17 
18  // The position in the text where we start looking for the next blank space
19  size_t startNextSearch = 0U;
20 
21  // Loop over spaces in the text
22  while (true) {
23  // If the rest of the text fits on the current line,
24  // then print it and we are done
25  if ((length - startLine) <= suggestedWidth) {
26  os << std::setfill(' ') << std::setw(indent) << "";
27  if (startLine == 0)
28  os << text;
29  else
30  os << text.substr(startLine);
31  os << "\n";
32  break;
33  }
34 
35  // Look for next space
36  size_t pos = text.find_first_of(' ', startNextSearch);
37 
38  // No more spaces
39  if (pos == std::string::npos) {
40  // The rest of the comment cannot fit in the width or we
41  // would have already printed it. Break the line at the
42  // end of the previous word if there is one and print the
43  // first part. Then print the rest whether it fits or not.
44  if (startNextSearch != startLine) {
45  os << std::setfill(' ') << std::setw(indent) << "";
46  os << text.substr(startLine, startNextSearch - startLine);
47  os << "\n";
48  startLine = startNextSearch;
49  }
50  os << std::setfill(' ') << std::setw(indent) << "";
51  os << text.substr(startLine);
52  os << "\n";
53  break;
54  }
55 
56  if ((pos + 1U - startLine) > suggestedWidth) {
57  // With this word the line is too long. Print out to
58  // the end of the previous word if there was one.
59  // If there was not, then print to the end of this word
60  // even though it exceeds the width.
61  if (startNextSearch != startLine) {
62  os << std::setfill(' ') << std::setw(indent) << "";
63  os << text.substr(startLine, startNextSearch - startLine);
64  os << "\n";
65  startLine = startNextSearch;
66  }
67  if ((pos + 1U - startLine) > suggestedWidth) {
68  os << std::setfill(' ') << std::setw(indent) << "";
69  os << text.substr(startLine, pos + 1U - startLine);
70  os << "\n";
71  startLine = pos + 1U;
72  }
73  }
74  startNextSearch = pos + 1U;
75  }
76  os.fill(oldFill);
77  }
78  } // namespace
79 
80  // Print and wrap text to an output stream.
81  // This function looks for new line chars in the text,
82  // and calls wrapAndPrintLine() to output each line,
83  // which wraps appropriately. That function
84  // inserts new lines to try to break the text to fit into
85  // the suggested width. At the beginning and after every
86  // inserted newline this will print "indent" blank spaces.
87  // The function will consider inserting a new line after
88  // every blank space. If the text to the next blank
89  // space will exceed the "suggestedWidth" it inserts a
90  // new line. If the text between two blank spaces (a "word")
91  // is longer than the suggested width, then it prints the whole
92  // word anyway (exceeding the "suggestedWidth" for lines).
93  // The output will look nice if the input has words separated
94  // by a single blank space with no extra space in the input text,
95  // otherwise the extra spaces and newlines get printed
96  // making the output not nicely formatted ...
97  void DocFormatHelper::wrapAndPrintText(std::ostream& os,
98  std::string const& text,
99  size_t indent,
100  size_t suggestedWidth) {
101  size_t pos = text.find_first_of('\n');
102  if (pos == std::string::npos) {
103  // no embedded newlines
104  wrapAndPrintLine(os, text, indent, suggestedWidth);
105  } else {
106  // print the first line.
107  wrapAndPrintLine(os, text.substr(0, pos), indent, suggestedWidth);
108  // print all lines after the first.
109  wrapAndPrintText(os, text.substr(pos + 1), indent, suggestedWidth);
110  }
111  }
112 
114  section_ = std::string();
115  pass_ = 0;
116  column1_ = 0;
117  column2_ = 0;
118  column3_ = 0;
119  counter_ = 0;
120  parent_ = OTHER;
121  }
122 
124  // Make the length of a comment at least 30 characters
125  // per line, longer if there is more space available
126  size_t width = 30U;
127  if (lineWidth() > startColumn2() + 30U) {
128  width = lineWidth() - startColumn2();
129  }
130  return width;
131  }
132 
133  void DocFormatHelper::indent(std::ostream& os) const {
134  char oldFill = os.fill();
135  os << std::setfill(' ') << std::setw(indentation_) << "";
136  os.fill(oldFill);
137  }
138 
139  void DocFormatHelper::indent2(std::ostream& os) const {
140  char oldFill = os.fill();
141  os << std::setfill(' ') << std::setw(startColumn2_) << "";
142  os.fill(oldFill);
143  }
144 } // namespace edm
edm::DocFormatHelper::column1_
size_t column1_
Definition: DocFormatHelper.h:95
ApeEstimator_cff.width
width
Definition: ApeEstimator_cff.py:24
edm::DocFormatHelper::init
void init()
Definition: DocFormatHelper.cc:113
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::DocFormatHelper::column2_
size_t column2_
Definition: DocFormatHelper.h:96
edm::DocFormatHelper::pass_
int pass_
Definition: DocFormatHelper.h:93
pos
Definition: PixelAliasList.h:18
edm::DocFormatHelper::indentation_
int indentation_
Definition: DocFormatHelper.h:88
edm::DocFormatHelper::column3_
size_t column3_
Definition: DocFormatHelper.h:97
edm::DocFormatHelper::counter_
int counter_
Definition: DocFormatHelper.h:99
edm::DocFormatHelper::lineWidth
size_t lineWidth() const
Definition: DocFormatHelper.h:35
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::DocFormatHelper::startColumn2
int startColumn2() const
Definition: DocFormatHelper.h:37
edm::DocFormatHelper::wrapAndPrintText
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
Definition: DocFormatHelper.cc:97
edm::DocFormatHelper::startColumn2_
size_t startColumn2_
Definition: DocFormatHelper.h:89
edm::DocFormatHelper::indent
void indent(std::ostream &os) const
Definition: DocFormatHelper.cc:133
edm::DocFormatHelper::commentWidth
size_t commentWidth() const
Definition: DocFormatHelper.cc:123
edm::DocFormatHelper::indent2
void indent2(std::ostream &os) const
Definition: DocFormatHelper.cc:139
DocFormatHelper.h
edm::DocFormatHelper::section_
std::string section_
Definition: DocFormatHelper.h:91
util.rrClient.indent
indent
Definition: rrClient.py:41
edm::DocFormatHelper::parent_
DescriptionParent parent_
Definition: DocFormatHelper.h:101
edm::DocFormatHelper::OTHER
Definition: DocFormatHelper.h:17
runonSM.text
text
Definition: runonSM.py:43