mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-05 19:19:39 -06:00
Pass the format string for the ephemeris conversion as a string_view to prevent an extra memory allocation
This commit is contained in:
@@ -469,15 +469,18 @@ public:
|
||||
*
|
||||
* \param ephemerisTime The ephemeris time, that is the number of TDB seconds past the
|
||||
* J2000 epoch
|
||||
* \param formatString The format string describing the output format
|
||||
* \param format The format string describing the output format
|
||||
* \return The destination for the converted date.
|
||||
*
|
||||
* \pre \p formatString must not be empty
|
||||
* \pre \p format must not be empty
|
||||
*
|
||||
* \sa http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html
|
||||
*/
|
||||
std::string dateFromEphemerisTime(double ephemerisTime,
|
||||
const std::string& formatString = "YYYY MON DDTHR:MN:SC.### ::RND") const;
|
||||
std::string_view format = "YYYY MON DDTHR:MN:SC.### ::RND") const;
|
||||
|
||||
void dateFromEphemerisTime(double ephemerisTime, char* outBuf, int bufferSize,
|
||||
std::string_view format = "YYYY MON DDTHR:MN:SC.### ::RND") const;
|
||||
|
||||
/**
|
||||
* Returns the \p position of a \p target body relative to an \p observer in a
|
||||
|
||||
@@ -483,13 +483,40 @@ double SpiceManager::ephemerisTimeFromDate(const std::string& timeString) const
|
||||
}
|
||||
|
||||
std::string SpiceManager::dateFromEphemerisTime(double ephemerisTime,
|
||||
const std::string& formatString) const
|
||||
std::string_view format) const
|
||||
{
|
||||
ghoul_assert(!formatString.empty(), "Format is empty");
|
||||
ghoul_assert(formatString.back() == '\0', "Format string must be null-terminated");
|
||||
|
||||
constexpr const int BufferSize = 256;
|
||||
SpiceChar buffer[BufferSize];
|
||||
timout_c(ephemerisTime, formatString.c_str(), BufferSize - 1, buffer);
|
||||
if (format.size() < 32) {
|
||||
constexpr const int BufferSize = 32;
|
||||
SpiceChar buffer[BufferSize];
|
||||
dateFromEphemerisTime(ephemerisTime, buffer, BufferSize, format);
|
||||
return std::string(buffer);
|
||||
}
|
||||
else if (format.size() < 256) {
|
||||
constexpr const int BufferSize = 256;
|
||||
SpiceChar buffer[BufferSize];
|
||||
dateFromEphemerisTime(ephemerisTime, buffer, BufferSize, format);
|
||||
return std::string(buffer);
|
||||
}
|
||||
else {
|
||||
std::string res;
|
||||
res.resize(format.size() + 1);
|
||||
dateFromEphemerisTime(ephemerisTime, res.data(), format.size(), format);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
void SpiceManager::dateFromEphemerisTime(double ephemerisTime, char* outBuf,
|
||||
int bufferSize,
|
||||
std::string_view formatString) const
|
||||
{
|
||||
ghoul_assert(!formatString.empty(), "Format is empty");
|
||||
ghoul_assert(formatString.back() == '\0', "Format string must be null-terminated");
|
||||
ghoul_assert(formatString.size() > bufferSize - 1, "Buffer size too small");
|
||||
|
||||
timout_c(ephemerisTime, formatString.data(), bufferSize - 1, outBuf);
|
||||
if (failed_c()) {
|
||||
throwSpiceError(
|
||||
fmt::format("Error converting ephemeris time '{}' to date with format '{}'",
|
||||
@@ -497,8 +524,6 @@ std::string SpiceManager::dateFromEphemerisTime(double ephemerisTime,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
glm::dvec3 SpiceManager::targetPosition(const std::string& target,
|
||||
|
||||
Reference in New Issue
Block a user