Skip to content

Commit

Permalink
AVRO-4107: [C++] Remove boost::algorithm (#3284)
Browse files Browse the repository at this point in the history
* AVRO-4107: [C++] Remove boost::algorithm

* fix pos

* implement unescape using  O(n)

* address feedback
  • Loading branch information
wgtmac authored Jan 11, 2025
1 parent b8bf9a1 commit 9356b8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
18 changes: 16 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,21 @@ 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.
static void unescape(string &s) {
boost::replace_all(s, "\\\"", "\"");
size_t writePos = 0, readPos = 0;
while (readPos < s.length()) {
if (readPos + 1 < s.length() && s[readPos] == '\\' && s[readPos + 1] == '\"') {
s[writePos++] = '\"';
readPos += 2;
} else if (writePos != readPos) {
s[writePos++] = s[readPos++];
} else {
writePos++;
readPos++;
}
}
if (writePos != s.length()) {
s.resize(writePos);
}
}

string getDocField(const Entity &e, const Object &m) {
Expand Down
30 changes: 24 additions & 6 deletions lang/c++/impl/avrogencpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#endif
#include <fstream>
#include <iostream>
#include <locale>
#include <map>
#include <optional>
#include <random>
#include <set>
#include <utility>

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

#include "Compiler.hh"
Expand Down Expand Up @@ -741,9 +741,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 +870,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], std::locale::classic())) start++;
while (start < end && std::isspace(buf[end - 1], std::locale::classic())) 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.compare(0, 8, "#ifndef ") == 0) {
candidate = buf.substr(8);
}
} else if (boost::algorithm::starts_with(buf, "#define ")) {
} else if (buf.compare(0, 8, "#define ") == 0) {
if (candidate == buf.substr(8)) {
break;
}
Expand Down

0 comments on commit 9356b8d

Please sign in to comment.