This is an automated email from the ASF dual-hosted git repository.
thiru pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new 9356b8d89 AVRO-4107: [C++] Remove boost::algorithm (#3284)
9356b8d89 is described below
commit 9356b8d8901e33ed50309b5c387fe1a77c54f96c
Author: Gang Wu <[email protected]>
AuthorDate: Sun Jan 12 07:30:36 2025 +0800
AVRO-4107: [C++] Remove boost::algorithm (#3284)
* AVRO-4107: [C++] Remove boost::algorithm
* fix pos
* implement unescape using O(n)
* address feedback
---
lang/c++/impl/Compiler.cc | 18 ++++++++++++++++--
lang/c++/impl/avrogencpp.cc | 30 ++++++++++++++++++++++++------
2 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc
index 73aaa9bbb..9fdd40c12 100644
--- a/lang/c++/impl/Compiler.cc
+++ b/lang/c++/impl/Compiler.cc
@@ -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>
@@ -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) {
diff --git a/lang/c++/impl/avrogencpp.cc b/lang/c++/impl/avrogencpp.cc
index d39764122..17a17e98a 100644
--- a/lang/c++/impl/avrogencpp.cc
+++ b/lang/c++/impl/avrogencpp.cc
@@ -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"
@@ -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";
@@ -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;
}