KalleOlaviNiemitalo commented on code in PR #3284:
URL: https://github.com/apache/avro/pull/3284#discussion_r1905592160
##########
lang/c++/impl/avrogencpp.cc:
##########
@@ -859,12 +869,19 @@ static string readGuard(const string &filename) {
string buf;
string candidate;
while (std::getline(ifs, buf)) {
- boost::algorithm::trim(buf);
+ if (!buf.empty()) {
+ size_t start = 0, end = buf.length();
+ while (start < end && std::isspace(buf[start])) start++;
+ while (start < end && std::isspace(buf[end - 1])) end--;
Review Comment:
If `buf[start]` or `buf[end - 1]` is a negative `char` other than `EOF`,
then passing it to `std::isspace(int ch)` causes undefined behavior. To avoid
the risk, this should cast the `char` values to `unsigned char`.
(In principle, this might also be expected to recognize and trim multibyte
whitespace characters as defined by the current locale. But it seems unlikely
that such characters would actually be used in the preprocessor directives for
which this function searches. And `boost::algorithm::trim` doesn't seem to
recognize them either, as it checks each `char` individually.)
##########
lang/c++/impl/avrogencpp.cc:
##########
@@ -859,12 +869,19 @@ static string readGuard(const string &filename) {
string buf;
string candidate;
while (std::getline(ifs, buf)) {
- boost::algorithm::trim(buf);
+ if (!buf.empty()) {
+ size_t start = 0, end = buf.length();
+ while (start < end && std::isspace(buf[start])) start++;
+ while (start < end && std::isspace(buf[end - 1])) end--;
+ if (start > 0 || end < buf.length()) {
+ buf = buf.substr(start, end - start);
+ }
+ }
if (candidate.empty()) {
- if (boost::algorithm::starts_with(buf, "#ifndef ")) {
+ if (buf.substr(0, 8) == "#ifndef ") {
Review Comment:
A remark rather than a recommendation: `buf.compare(0, 8, "#ifndef ") == 0`
might be faster here, because of less copying. But also a bit harder to
understand and the performance difference might not be significant.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]