Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-4107: [C++] Remove boost::algorithm #3284

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lang/c++/impl/Compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <boost/algorithm/string/replace.hpp>

#include <sstream>
#include <unordered_set>
#include <utility>
Expand Down Expand Up @@ -136,7 +136,11 @@ int64_t getLongField(const Entity &e, const Object &m,
// Unescape double quotes (") for de-serialization. This method complements the
// method NodeImpl::escape() which is used for serialization.
wgtmac marked this conversation as resolved.
Show resolved Hide resolved
static void unescape(string &s) {
boost::replace_all(s, "\\\"", "\"");
std::string::size_type pos = 0;
while ((pos = s.find("\\\"", pos)) != std::string::npos) {
s.replace(pos, 2, "\"");
pos += 2;
wgtmac marked this conversation as resolved.
Show resolved Hide resolved
}
wgtmac marked this conversation as resolved.
Show resolved Hide resolved
}

string getDocField(const Entity &e, const Object &m) {
Expand Down
29 changes: 23 additions & 6 deletions lang/c++/impl/avrogencpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <set>
#include <utility>

#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>

#include "Compiler.hh"
Expand Down Expand Up @@ -741,9 +740,20 @@ void CodeGen::generateTraits(const NodePtr &n) {
void CodeGen::generateDocComment(const NodePtr &n, const char *indent) {
if (!n->getDoc().empty()) {
std::vector<std::string> lines;
boost::algorithm::split(lines, n->getDoc(), boost::algorithm::is_any_of("\n"));
{
const std::string &doc = n->getDoc();
size_t pos = 0;
size_t found;
while ((found = doc.find('\n', pos)) != std::string::npos) {
lines.push_back(doc.substr(pos, found - pos));
pos = found + 1;
}
if (pos < doc.size()) {
lines.push_back(doc.substr(pos));
}
}
for (auto &line : lines) {
boost::algorithm::replace_all(line, "\r", "");
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());

if (line.empty()) {
os_ << indent << "//\n";
Expand Down Expand Up @@ -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--;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it is better to call std::isspace(buf[start], std::locale::classic()) instead of std::isspace(static_cast<unsigned char>(buf[start]))

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 ") {
wgtmac marked this conversation as resolved.
Show resolved Hide resolved
candidate = buf.substr(8);
}
} else if (boost::algorithm::starts_with(buf, "#define ")) {
} else if (buf.substr(0, 8) == "#define ") {
if (candidate == buf.substr(8)) {
break;
}
Expand Down
Loading