Skip to content

Commit 00977f2

Browse files
committed
in shorten() - keep parenthesis balanced since this messes up some simple latex parsers
1 parent 62706e1 commit 00977f2

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

src/strtools.h

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,48 @@ static inline bool is_suffix(const std::string& str, const std::string& match)
148148
}
149149

150150
/**
151-
* Shorten a string to width charaters, adding "..." at the end.
151+
* Shorten a string to width charaters, adding "..." at the end. Due to latex
152+
* parsing problems, balance parenthesis and brackets while shortening.
152153
*/
153154
static inline std::string shorten(const std::string& str, size_t width = 80)
154155
{
155-
if (str.size() > width)
156-
return str.substr(0, width - 3) + "...";
157-
else
156+
if (str.size() <= width)
158157
return str;
158+
159+
std::string out, suffix;
160+
std::string::const_iterator i = str.begin();
161+
while (i != str.end() && out.size() + suffix.size() + 3 < width) {
162+
if (*i == '(')
163+
out += *i, suffix += ')';
164+
else if (*i == '[')
165+
out += *i, suffix += ']';
166+
else if (*i == '{')
167+
out += *i, suffix += '}';
168+
else if (*i == ')') {
169+
out += *i;
170+
if (suffix.size() && *suffix.rbegin() == ')')
171+
suffix.resize(suffix.size() - 1);
172+
}
173+
else if (*i == ']') {
174+
out += *i;
175+
if (suffix.size() && *suffix.rbegin() == ']')
176+
suffix.resize(suffix.size() - 1);
177+
}
178+
else if (*i == '}') {
179+
out += *i;
180+
if (suffix.size() && *suffix.rbegin() == '}')
181+
suffix.resize(suffix.size() - 1);
182+
}
183+
else
184+
out += *i;
185+
186+
++i;
187+
}
188+
189+
// reverse order of closing brackets
190+
std::reverse(suffix.begin(), suffix.end());
191+
192+
return out + "..." + suffix;
159193
}
160194

161195
/**

0 commit comments

Comments
 (0)