YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Vector.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/sig/Vector.h>
7
9
10#include <algorithm>
11#include <cstdio>
12
13using namespace yarp::sig;
14using namespace yarp::os;
15
16namespace {
17YARP_LOG_COMPONENT(VECTOR, "yarp.sig.Vector")
18
19// Upper bound for precision and width, to avoid huge allocations
20// (e.g. toFormattedString(INT_MAX)).
22
24std::string toFormattedStringImpl(const T* data, size_t len, int precision, int width)
25{
26 static_assert(std::is_floating_point_v<T>, "toFormattedString requires a floating point type");
27
28 std::string ret;
29 if (len == 0 || data == nullptr) {
30 return ret;
31 }
32
33 // A negative precision is treated by printf as omitted (default: 6)
35 const bool useWidth = (width >= 0);
36 width = std::min(width, maxFormatField);
37 const char separator = useWidth ? ' ' : '\t';
38
39 // float is promoted to double when passed through varargs
40 auto format = [&](char* buf, size_t bufSize, T value) {
41 return useWidth ? std::snprintf(buf, bufSize, "% *.*f", width, precision, static_cast<double>(value))
42 : std::snprintf(buf, bufSize, "% .*f", precision, static_cast<double>(value));
43 };
44
45 ret.reserve(len * 12);
46 char buf[64];
47 for (size_t i = 0; i < len; ++i) {
48 if (i != 0) {
49 ret += separator;
50 }
51 const int n = format(buf, sizeof(buf), data[i]);
52 if (n < 0) {
53 yCError(VECTOR, "toFormattedString: formatting error at index %zu", i);
54 continue;
55 }
56 if (static_cast<size_t>(n) < sizeof(buf)) {
57 ret.append(buf, static_cast<size_t>(n));
58 } else {
59 // Very large values (e.g. 1e300) or large precision/width:
60 // print directly into the output string, without truncation.
61 const size_t offset = ret.size();
62 ret.resize(offset + static_cast<size_t>(n) + 1);
63 format(&ret[offset], static_cast<size_t>(n) + 1, data[i]);
64 ret.resize(offset + static_cast<size_t>(n));
65 }
66 }
67 return ret;
68}
69} // namespace
70
71std::string VectorOfDouble::toFormattedString(int precision, int width) const
72{
73 return toFormattedStringImpl(data(), size(), precision, width);
74}
75
76std::string VectorOfFloat::toFormattedString(int precision, int width) const
77{
78 return toFormattedStringImpl(data(), size(), precision, width);
79}
contains the definition of a Vector type
A mini-server for performing network communication in the background.
double * data()
Return a pointer to the first element of the vector.
Definition VectorBase.h:245
size_t size() const
Returns the number of elements in the vector.
Definition VectorBase.h:167
std::string toFormattedString(int precision=-1, int width=-1) const
Returns a human readable representation of the vector, with formatted numbers.
Definition Vector.cpp:71
std::string toFormattedString(int precision=-1, int width=-1) const
Returns a human readable representation of the vector, with formatted numbers.
Definition Vector.cpp:76
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
An interface to the operating system, including Port based communication.