提交 38e5c5ee 编写于 作者: A Aaron Xiao 提交者: Dong Li

Use StrCat to construct string. (#259)

上级 6405bfa1
......@@ -49,6 +49,7 @@ cc_library(
],
deps = [
"//modules/common:log",
"//modules/common/util:string_util",
],
)
......@@ -165,6 +166,7 @@ cc_library(
deps = [
"//modules/common:log",
"//modules/common/math:matrix_operations",
"//modules/common/util:string_util",
"@eigen//:eigen",
],
)
......
......@@ -18,9 +18,9 @@
#include <algorithm>
#include <cmath>
#include <sstream>
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
#include "modules/common/math/math_utils.h"
......@@ -146,11 +146,9 @@ void AABox2d::MergeFrom(const Vec2d &other_point) {
}
std::string AABox2d::DebugString() const {
std::ostringstream sout;
sout << "aabox2d ( center = " << center_.DebugString()
<< " length = " << length_ << " width = " << width_ << " )";
sout.flush();
return sout.str();
return util::StrCat(
"aabox2d ( center = ", center_.DebugString(),
" length = ", length_, " width = ", width_, " )");
}
} // namespace math
......
......@@ -18,10 +18,10 @@
#include <algorithm>
#include <cmath>
#include <sstream>
#include <utility>
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
#include "modules/common/math/math_utils.h"
#include "modules/common/math/polygon2d.h"
......@@ -303,12 +303,10 @@ void Box2d::RotateFromCenter(const double rotate_angle) {
void Box2d::Shift(const Vec2d &shift_vec) { center_ += shift_vec; }
std::string Box2d::DebugString() const {
std::ostringstream sout;
sout << "box2d ( center = " << center_.DebugString()
<< " heading = " << heading_ << " length = " << length_
<< " width = " << width_ << " )";
sout.flush();
return sout.str();
return util::StrCat(
"box2d ( center = ", center_.DebugString(),
" heading = ", heading_, " length = ", length_,
" width = ", width_, " )");
}
} // namespace math
......
......@@ -22,13 +22,13 @@
#ifndef MODULES_COMMON_MATH_KALMAN_FILTER_H_
#define MODULES_COMMON_MATH_KALMAN_FILTER_H_
#include <sstream>
#include <string>
#include "Eigen/Dense"
#include "modules/common/log.h"
#include "modules/common/math/matrix_operations.h"
#include "modules/common/util/string_util.h"
/**
* @namespace apollo::common::math
......@@ -266,15 +266,14 @@ inline void KalmanFilter<T, XN, ZN, UN>::Correct(
template <typename T, unsigned int XN, unsigned int ZN, unsigned int UN>
inline std::string KalmanFilter<T, XN, ZN, UN>::DebugString() const {
Eigen::IOFormat clean_fmt(4, 0, ", ", " ", "[", "]");
std::ostringstream strs;
strs << "F = " << F_.format(clean_fmt) << "\n";
strs << "B = " << B_.format(clean_fmt) << "\n";
strs << "H = " << H_.format(clean_fmt) << "\n";
strs << "Q = " << Q_.format(clean_fmt) << "\n";
strs << "R = " << R_.format(clean_fmt) << "\n";
strs << "x = " << x_.format(clean_fmt) << "\n";
strs << "P = " << P_.format(clean_fmt) << "\n";
return strs.str();
return util::StrCat(
"F = ", F_.format(clean_fmt), "\n"
"B = ", B_.format(clean_fmt), "\n"
"H = ", H_.format(clean_fmt), "\n"
"Q = ", Q_.format(clean_fmt), "\n"
"R = ", R_.format(clean_fmt), "\n"
"x = ", x_.format(clean_fmt), "\n"
"P = ", P_.format(clean_fmt), "\n");
}
} // namespace math
......
......@@ -18,10 +18,10 @@
#include <algorithm>
#include <cmath>
#include <sstream>
#include <utility>
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
#include "modules/common/math/math_utils.h"
......@@ -214,11 +214,8 @@ double LineSegment2d::GetPerpendicularFoot(const Vec2d &point,
}
std::string LineSegment2d::DebugString() const {
std::ostringstream sout;
sout << "segment2d ( start = " << start_.DebugString()
<< " end = " << end_.DebugString() << " )";
sout.flush();
return sout.str();
return util::StrCat("segment2d ( start = ", start_.DebugString(),
" end = ", end_.DebugString(), " )");
}
} // namespace math
......
......@@ -17,8 +17,9 @@
#include "modules/common/math/vec2d.h"
#include <cmath>
#include <sstream>
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
namespace apollo {
namespace common {
......@@ -110,10 +111,7 @@ bool Vec2d::operator==(const Vec2d &other) const {
Vec2d operator*(const double ratio, const Vec2d &vec) { return vec * ratio; }
std::string Vec2d::DebugString() const {
std::ostringstream sout;
sout << "vec2d ( x = " << x_ << " y = " << y_ << " )";
sout.flush();
return sout.str();
return util::StrCat("vec2d ( x = ", x_, " y = ", y_, " )");
}
} // namespace math
......
......@@ -26,14 +26,24 @@ cc_library(
hdrs = ["lru_cache.h"],
)
cc_library(
name = "string_util",
srcs = [
"string_util.cc",
],
hdrs = [
"string_util.h",
],
)
cc_test(
name = "util_test",
name = "string_util_test",
size = "small",
srcs = [
"util_test.cc",
"string_util_test.cc",
],
deps = [
":util",
":string_util",
"@gtest//:main",
],
)
......
......@@ -32,7 +32,7 @@
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/text_format.h"
#include "modules/common/log.h"
#include "modules/common/util/util.h"
#include "modules/common/util/string_util.h"
/**
* @namespace apollo::common::util
......
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/common/util/string_util.h"
namespace apollo {
namespace common {
namespace util {
bool EndWith(const std::string& original, const std::string& pattern) {
return original.length() >= pattern.length() &&
original.substr(original.length() - pattern.length()) == pattern;
}
} // namespace util
} // namespace common
} // namespace apollo
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
/**
* @file
* @brief Some string util functions.
*/
#ifndef MODULES_COMMON_STRING_UTIL_H_
#define MODULES_COMMON_STRING_UTIL_H_
#include <sstream>
#include <string>
/**
* @namespace apollo::common::util
* @brief apollo::common::util
*/
namespace apollo {
namespace common {
namespace util {
/**
* @brief Check if a string ends with a pattern.
* @param original The original string. To see if it ends with some
* specified pattern.
* @param pattern The target pattern. To see if the original string ends
* with it.
* @return Whether the original string ends with the specified pattern.
*/
bool EndWith(const std::string& original, const std::string& pattern);
/**
* @brief Concat parameters to a string, e.g.: StrCat("age = ", 32)
* @return String of concated parameters.
*/
template <typename ...T>
std::string StrCat(const T& ...args) {
std::ostringstream oss;
// Expand args and pass to oss.
std::initializer_list<char>{(oss << args, ' ') ... };
return oss.str();
}
} // namespace util
} // namespace common
} // namespace apollo
#endif // MODULES_COMMON_STRING_UTIL_H_
......@@ -14,7 +14,8 @@
* limitations under the License.
*****************************************************************************/
#include "modules/common/util/util.h"
#include "modules/common/util/string_util.h"
#include "gtest/gtest.h"
namespace apollo {
......
......@@ -24,11 +24,6 @@ namespace util {
using SLPoint = apollo::common::SLPoint;
bool EndWith(const std::string& original, const std::string& pattern) {
return original.length() >= pattern.length() &&
original.substr(original.length() - pattern.length()) == pattern;
}
SLPoint MakeSLPoint(const double s, const double l) {
SLPoint sl;
sl.set_s(s);
......
......@@ -23,8 +23,6 @@
#define MODULES_COMMON_UTIL_H_
#include <iostream>
#include <string>
#include <sstream>
#include <utility>
#include "modules/common/proto/path_point.pb.h"
......@@ -37,28 +35,6 @@ namespace apollo {
namespace common {
namespace util {
/**
* @brief Check if a string ends with a pattern.
* @param original The original string. To see if it ends with some
* specified pattern.
* @param pattern The target pattern. To see if the original string ends
* with it.
* @return Whether the original string ends with the specified pattern.
*/
bool EndWith(const std::string& original, const std::string& pattern);
/**
* @brief Concat parameters to a string, e.g.: StrCat("age = ", 32)
* @return String of concated parameters.
*/
template <typename ...T>
std::string StrCat(const T& ...args) {
std::ostringstream oss;
// Expand args and pass to oss.
std::initializer_list<char>{(oss << args, ' ') ... };
return oss.str();
}
/**
* @brief create a SL point
* @param s the s value
......
......@@ -19,7 +19,6 @@
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
......@@ -30,6 +29,7 @@
#include "modules/common/math/linear_quadratic_regulator.h"
#include "modules/common/math/math_utils.h"
#include "modules/common/time/time.h"
#include "modules/common/util/string_util.h"
#include "modules/control/common/control_gflags.h"
namespace apollo {
......@@ -119,24 +119,27 @@ bool LatController::LoadControlConf(const ControlConf *control_conf) {
void LatController::ProcessLogs(const SimpleLateralDebug *debug,
const canbus::Chassis *chassis) {
std::stringstream log_stream;
log_stream << debug->lateral_error() << "," << debug->ref_heading() << ","
<< VehicleState::instance()->heading() << ","
<< debug->heading_error() << "," << debug->heading_error_rate()
<< "," << debug->lateral_error_rate() << "," << debug->curvature()
<< "," << debug->steer_angle() << ","
<< debug->steer_angle_feedforward() << ","
<< debug->steer_angle_lateral_contribution() << ","
<< debug->steer_angle_lateral_rate_contribution() << ","
<< debug->steer_angle_heading_contribution() << ","
<< debug->steer_angle_heading_rate_contribution() << ","
<< debug->steer_angle_feedback() << ","
<< chassis->steering_percentage() << ","
<< VehicleState::instance()->linear_velocity();
const std::string log_str = apollo::common::util::StrCat(
debug->lateral_error(), ",",
debug->ref_heading(), ",",
VehicleState::instance()->heading(), ",",
debug->heading_error(), ",",
debug->heading_error_rate(), ",",
debug->lateral_error_rate(), ",",
debug->curvature(), ",",
debug->steer_angle(), ",",
debug->steer_angle_feedforward(), ",",
debug->steer_angle_lateral_contribution(), ",",
debug->steer_angle_lateral_rate_contribution(), ",",
debug->steer_angle_heading_contribution(), ",",
debug->steer_angle_heading_rate_contribution(), ",",
debug->steer_angle_feedback(), ",",
chassis->steering_percentage(), ",",
VehicleState::instance()->linear_velocity());
if (FLAGS_enable_csv_debug) {
steer_log_file_ << log_stream.str() << std::endl;
steer_log_file_ << log_str << std::endl;
}
AINFO << "Steer_Control_Detail: " << log_stream.str();
AINFO << "Steer_Control_Detail: " << log_str;
}
void LatController::LogInitParameters() {
......
......@@ -12,6 +12,7 @@ cc_library(
],
deps = [
"//modules/common:log",
"//modules/common/util:string_util",
"//third_party/json",
"@civetweb//:civetweb++",
],
......
......@@ -15,10 +15,10 @@ limitations under the License.
#include "modules/dreamview/backend/websocket/websocket.h"
#include <sstream>
#include <vector>
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
namespace apollo {
namespace dreamview {
......@@ -74,13 +74,10 @@ bool WebSocketHandler::SendData(const std::string &data, Connection *conn) {
} else if (ret < 0) {
msg = "Send Error";
} else {
std::ostringstream os;
os << "Expect to send " << data.size() << " bytes. But sent " << ret
<< " bytes";
msg = os.str();
msg = apollo::common::util::StrCat(
"Expect to send ", data.size(), " bytes. But sent ", ret, " bytes");
}
AWARN << "Failed to send data via websocket connection. Reason: "
<< msg;
AWARN << "Failed to send data via websocket connection. Reason: " << msg;
return false;
}
......
......@@ -25,6 +25,7 @@
#include "modules/common/math/math_utils.h"
#include "modules/common/math/polygon2d.h"
#include "modules/common/math/vec2d.h"
#include "modules/common/util/string_util.h"
namespace apollo {
namespace hdmap {
......@@ -60,21 +61,17 @@ std::string LaneWaypoint::debug_string() const {
if (lane == nullptr) {
return "(lane is null)";
}
std::ostringstream sout;
sout << "id = " << lane->id().id() << " s = " << s;
sout.flush();
return sout.str();
return apollo::common::util::StrCat("id = ", lane->id().id(), " s = ", s);
}
std::string LaneSegment::debug_string() const {
if (lane == nullptr) {
return "(lane is null)";
}
std::ostringstream sout;
sout << "id = " << lane->id().id() << " start_s = " << start_s
<< " end_s = " << end_s;
sout.flush();
return sout.str();
return apollo::common::util::StrCat(
"id = ", lane->id().id(), " "
"start_s = ", start_s, " "
"end_s = ", end_s);
}
std::string MapPathPoint::debug_string() const {
......@@ -121,10 +118,7 @@ std::string Path::debug_string() const {
}
std::string PathOverlap::debug_string() const {
std::ostringstream sout;
sout << object_id << " " << start_s << " " << end_s;
sout.flush();
return sout.str();
return apollo::common::util::StrCat(object_id, " ", start_s, " ", end_s);
}
Path::Path(std::vector<MapPathPoint> path_points)
......
......@@ -16,10 +16,10 @@
#include "modules/monitor/hwmonitor/hw/esdcan/esdcan_checker.h"
#include <sstream>
#include <utility>
#include <vector>
#include "modules/common/util/string_util.h"
#include "modules/monitor/hwmonitor/hw/esdcan/esdcan_err_str.h"
#include "modules/monitor/hwmonitor/hw/hw_log_module.h"
......@@ -30,9 +30,7 @@ namespace hw {
const char EsdCanChecker::ESD_CAN_NAME[] = "ESD_CAN";
EsdCanChecker::EsdCanChecker(int id) : can_id_(id) {
std::ostringstream os;
os << ESD_CAN_NAME << "-" << id;
name_ = os.str();
name_ = apollo::common::util::StrCat(ESD_CAN_NAME, "-", id);
}
hw::Status EsdCanChecker::esdcan_result_to_hw_status(NTCAN_RESULT ntstatus) {
......
......@@ -11,6 +11,7 @@ cc_library(
"//modules/perception/common:perception_common",
"//modules/perception/proto:perception_proto",
"//modules/common:common",
"//modules/common/util:string_util",
"@eigen//:eigen",
],
)
......
......@@ -15,15 +15,13 @@
*****************************************************************************/
#include "modules/perception/obstacle/base/object.h"
#include <sstream>
#include "modules/common/macro.h"
#include "modules/common/log.h"
#include "modules/common/util/string_util.h"
namespace apollo {
namespace perception {
using std::ostringstream;
using std::string;
using std::vector;
using Eigen::Vector3d;
......@@ -95,18 +93,21 @@ void Object::clone(const Object& rhs) {
}
string Object::to_string() const {
ostringstream oss;
oss << "Object[id: " << id << ", track_id: " << track_id
<< ", cloud_size: " << cloud->size() << ", direction: "
<< direction.transpose() << ", center: " << center.transpose()
<< " velocity: " << velocity.transpose() << ", width: " << width
<< ", length: " << length << ", height: " << height
<< ", polygon_size: " << polygon.size()
<< ", type: " << type << ", is_background: " << is_background << "]";
// << ", tracking_time: " << GLOG_TIMESTAMP(tracking_time)
// << ", latest_tracked_time: " << GLOG_TIMESTAMP(latest_tracked_time) << "]";
return oss.str();
return apollo::common::util::StrCat(
"Object[id: ", id, ", "
"track_id: ", track_id, ", "
"cloud_size: ", cloud->size(), ", "
"direction: ", direction.transpose(), ", "
"center: ", center.transpose(), ", "
"velocity: ", velocity.transpose(), ", "
"width: ", width, ", "
"length: ", length, ", "
"height: ", height, ", "
"polygon_size: ", polygon.size(), ", "
"type: ", type, ", "
"is_background: ", is_background, "]");
// "tracking_time: ", GLOG_TIMESTAMP(tracking_time)
// "latest_tracked_time: ", GLOG_TIMESTAMP(latest_tracked_time)
}
bool Object::serialize(PerceptionObstacle* pb_obj) const {
......
......@@ -21,7 +21,8 @@
#include "modules/planning/common/speed/st_point.h"
#include <iomanip>
#include <sstream>
#include "modules/common/util/string_util.h"
namespace apollo {
namespace planning {
......@@ -37,10 +38,9 @@ void STPoint::set_s(const double s) { return set_y(s); }
void STPoint::set_t(const double t) { return set_x(t); }
std::string STPoint::DebugString() const {
std::ostringstream sout;
sout << "{ \"s\" : " << std::setprecision(6) << s()
<< ", \"t\" : " << std::setprecision(6) << t() << " }";
return sout.str();
return apollo::common::util::StrCat(
"{ \"s\" : ", std::setprecision(6), s(), ", "
"\"t\" : ", std::setprecision(6), t(), " }");
}
} // namespace planning
......
......@@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include "modules/common/util/string_util.h"
#include "modules/planning/reference_line/reference_point.h"
namespace apollo {
......@@ -77,12 +78,14 @@ double ReferencePoint::lower_bound() const { return lower_bound_; }
double ReferencePoint::upper_bound() const { return upper_bound_; }
const std::string ReferencePoint::DebugString() const {
std::stringstream ss;
ss << "{x: " << std::fixed << x() << ", y: " << y() << ", theta: " << heading()
<< ", kappa: " << kappa() << ", dkappa: " << dkappa()
<< ", upper_bound: " << upper_bound() << ", lower_bound: " << lower_bound()
<< "}";
return ss.str();
return apollo::common::util::StrCat(
"{x: ", std::fixed, x(), ", "
"y: ", y(), ", "
"theta: ", heading(), ", "
"kappa: ", kappa(), ", "
"dkappa: ", dkappa(), ", "
"upper_bound: ", upper_bound(), ", "
"lower_bound: ", lower_bound(), "}");
}
} // namespace planning
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册