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 debc68213 AVRO-4105: [C++] Remove boost::lexical_cast and boost::math 
(#3277)
debc68213 is described below

commit debc68213aa890d696f5f000893777fa74a100b5
Author: Gang Wu <[email protected]>
AuthorDate: Sat Jan 4 20:26:38 2025 +0800

    AVRO-4105: [C++] Remove boost::lexical_cast and boost::math (#3277)
    
    * AVRO-4105: [C++] Remove boost::lexical_cast and boost::math
    
    * fix precision
    
    * add missing #include <iomanip>
    
    * fix ut
    
    * add oss.imbue(std::locale::classic());
    
    * refactor template
---
 lang/c++/impl/avrogencpp.cc        |  9 +++------
 lang/c++/impl/json/JsonIO.hh       | 26 +++++++++++++++++---------
 lang/c++/impl/parsing/JsonCodec.cc |  5 ++---
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/lang/c++/impl/avrogencpp.cc b/lang/c++/impl/avrogencpp.cc
index 8fa145f81..323aa5168 100644
--- a/lang/c++/impl/avrogencpp.cc
+++ b/lang/c++/impl/avrogencpp.cc
@@ -27,7 +27,6 @@
 #include <set>
 
 #include <boost/algorithm/string.hpp>
-#include <boost/lexical_cast.hpp>
 #include <boost/program_options.hpp>
 
 #include <boost/random/mersenne_twister.hpp>
@@ -47,8 +46,6 @@ using std::set;
 using std::string;
 using std::vector;
 
-using boost::lexical_cast;
-
 using avro::compileJsonSchema;
 using avro::ValidSchema;
 
@@ -196,7 +193,7 @@ string CodeGen::cppTypeOf(const NodePtr &n) {
         case avro::AVRO_MAP:
             return "std::map<std::string, " + cppTypeOf(n->leafAt(1)) + " >";
         case avro::AVRO_FIXED:
-            return "std::array<uint8_t, " + 
lexical_cast<string>(n->fixedSize()) + ">";
+            return "std::array<uint8_t, " + std::to_string(n->fixedSize()) + 
">";
         case avro::AVRO_SYMBOLIC:
             return cppTypeOf(resolveSymbol(n));
         case avro::AVRO_UNION:
@@ -799,7 +796,7 @@ void CodeGen::emitGeneratedWarning() {
 string CodeGen::guard() {
     string h = headerFile_;
     makeCanonical(h, true);
-    return h + "_" + lexical_cast<string>(random_()) + "_H";
+    return h + "_" + std::to_string(random_()) + "_H";
 }
 
 void CodeGen::generate(const ValidSchema &schema) {
@@ -972,7 +969,7 @@ std::string UnionCodeTracker::generateNewUnionName(const 
std::vector<std::string
     }
     makeCanonical(s, false);
 
-    std::string result = s + "_Union__" + 
boost::lexical_cast<string>(unionNumber_++) + "__";
+    std::string result = s + "_Union__" + std::to_string(unionNumber_++) + 
"__";
     unionBranchNameMapping_.emplace(unionBranches, result);
     return result;
 }
diff --git a/lang/c++/impl/json/JsonIO.hh b/lang/c++/impl/json/JsonIO.hh
index 9fdc482ed..e0c8eeff3 100644
--- a/lang/c++/impl/json/JsonIO.hh
+++ b/lang/c++/impl/json/JsonIO.hh
@@ -19,12 +19,13 @@
 #ifndef avro_json_JsonIO_hh__
 #define avro_json_JsonIO_hh__
 
-#include <boost/lexical_cast.hpp>
-#include <boost/math/special_functions/fpclassify.hpp>
+#include <cmath>
+#include <iomanip>
 #include <locale>
 #include <sstream>
 #include <stack>
 #include <string>
+#include <type_traits>
 
 #include "Config.hh"
 #include "Stream.hh"
@@ -403,23 +404,30 @@ public:
     }
 
     template<typename T>
-    void encodeNumber(T t) {
+    std::enable_if_t<!std::is_floating_point_v<T>, void> encodeNumber(T t) {
         sep();
         std::ostringstream oss;
-        oss << boost::lexical_cast<std::string>(t);
+        oss.imbue(std::locale::classic());
+        oss << t;
         const std::string s = oss.str();
         out_.writeBytes(reinterpret_cast<const uint8_t *>(s.data()), s.size());
         sep2();
     }
 
-    void encodeNumber(double t) {
+    template<typename T>
+    std::enable_if_t<std::is_floating_point_v<T>, void> encodeNumber(T t) {
         sep();
         std::ostringstream oss;
-        if (boost::math::isfinite(t)) {
-            oss << boost::lexical_cast<std::string>(t);
-        } else if (boost::math::isnan(t)) {
+        if (std::isfinite(t)) {
+            oss.imbue(std::locale::classic());
+            if constexpr (std::is_same_v<T, float>) {
+                oss << std::setprecision(9) << t;
+            } else {
+                oss << std::setprecision(17) << t;
+            }
+        } else if (std::isnan(t)) {
             oss << "NaN";
-        } else if (t == std::numeric_limits<double>::infinity()) {
+        } else if (t == std::numeric_limits<T>::infinity()) {
             oss << "Infinity";
         } else {
             oss << "-Infinity";
diff --git a/lang/c++/impl/parsing/JsonCodec.cc 
b/lang/c++/impl/parsing/JsonCodec.cc
index 84b366606..fc073a989 100644
--- a/lang/c++/impl/parsing/JsonCodec.cc
+++ b/lang/c++/impl/parsing/JsonCodec.cc
@@ -17,7 +17,6 @@
  */
 
 #include <algorithm>
-#include <boost/math/special_functions/fpclassify.hpp>
 #include <map>
 #include <memory>
 #include <string>
@@ -539,7 +538,7 @@ void JsonEncoder<P, F>::encodeFloat(float f) {
         out_.encodeString("Infinity");
     } else if (-f == std::numeric_limits<float>::infinity()) {
         out_.encodeString("-Infinity");
-    } else if (boost::math::isnan(f)) {
+    } else if (std::isnan(f)) {
         out_.encodeString("NaN");
     } else {
         out_.encodeNumber(f);
@@ -553,7 +552,7 @@ void JsonEncoder<P, F>::encodeDouble(double d) {
         out_.encodeString("Infinity");
     } else if (-d == std::numeric_limits<double>::infinity()) {
         out_.encodeString("-Infinity");
-    } else if (boost::math::isnan(d)) {
+    } else if (std::isnan(d)) {
         out_.encodeString("NaN");
     } else {
         out_.encodeNumber(d);

Reply via email to