Skip to content

Commit

Permalink
AVRO-4105: [C++] Remove boost::lexical_cast and boost::math (#3277)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wgtmac authored Jan 4, 2025
1 parent 2943ea7 commit debc682
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
9 changes: 3 additions & 6 deletions lang/c++/impl/avrogencpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -47,8 +46,6 @@ using std::set;
using std::string;
using std::vector;

using boost::lexical_cast;

using avro::compileJsonSchema;
using avro::ValidSchema;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
26 changes: 17 additions & 9 deletions lang/c++/impl/json/JsonIO.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 2 additions & 3 deletions lang/c++/impl/parsing/JsonCodec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

#include <algorithm>
#include <boost/math/special_functions/fpclassify.hpp>
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit debc682

Please sign in to comment.