Skip to content

Commit ba2544b

Browse files
committed
Evict large function definitions from the Helpers.hh header file
Signed-off-by: Jeremy Nimmer <jeremy.nimmer@tri.global>
1 parent 12f5f7f commit ba2544b

File tree

3 files changed

+244
-216
lines changed

3 files changed

+244
-216
lines changed

Changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
1. Prevent -0 with out stream operator
2525
* [Pull request 206](https://github.com/ignitionrobotics/ign-math/pull/206)
2626

27+
1. Evict large function definitions from the Helpers.hh header file.
28+
* [Pull request 288](https://github.com/ignitionrobotics/ign-math/pull/288)
29+
2730
## Ignition Math 6.x
2831

2932
### Ignition Math 6.x.x

include/ignition/math/Helpers.hh

+10-216
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
#include <chrono>
2222
#include <cmath>
2323
#include <cstdint>
24-
#include <iomanip>
2524
#include <iostream>
2625
#include <limits>
27-
#include <regex>
28-
#include <sstream>
2926
#include <string>
3027
#include <tuple>
3128
#include <utility>
@@ -687,81 +684,14 @@ namespace ignition
687684
/// \brief Convert a std::chrono::steady_clock::time_point to a string
688685
/// \param[in] _point The std::chrono::steady_clock::time_point to convert.
689686
/// \return A string formatted with the time_point
690-
inline std::string timePointToString(
691-
const std::chrono::steady_clock::time_point &_point)
692-
{
693-
auto duration = _point - secNsecToTimePoint(0, 0);
694-
auto cleanDuration = breakDownDurations<days,
695-
std::chrono::hours,
696-
std::chrono::minutes,
697-
std::chrono::seconds,
698-
std::chrono::milliseconds>(
699-
duration);
700-
std::ostringstream output_string;
701-
output_string << std::setw(2) << std::setfill('0')
702-
<< std::get<0>(cleanDuration).count() << " "
703-
<< std::setw(2) << std::setfill('0')
704-
<< std::get<1>(cleanDuration).count() << ":"
705-
<< std::setw(2) << std::setfill('0')
706-
<< std::get<2>(cleanDuration).count() << ":"
707-
<< std::setfill('0') << std::setw(6)
708-
<< std::fixed << std::setprecision(3)
709-
<< std::get<3>(cleanDuration).count() +
710-
std::get<4>(cleanDuration).count()/1000.0;
711-
return output_string.str();
712-
}
687+
std::string timePointToString(
688+
const std::chrono::steady_clock::time_point &_point);
713689

714690
/// \brief Convert a std::chrono::steady_clock::duration to a string
715691
/// \param[in] _duration The std::chrono::steady_clock::duration to convert.
716692
/// \return A string formatted with the duration
717-
inline std::string durationToString(
718-
const std::chrono::steady_clock::duration &_duration)
719-
{
720-
auto cleanDuration = breakDownDurations<days,
721-
std::chrono::hours,
722-
std::chrono::minutes,
723-
std::chrono::seconds,
724-
std::chrono::milliseconds>(
725-
_duration);
726-
std::ostringstream outputString;
727-
outputString << std::setw(2) << std::setfill('0')
728-
<< std::get<0>(cleanDuration).count() << " "
729-
<< std::setw(2) << std::setfill('0')
730-
<< std::get<1>(cleanDuration).count() << ":"
731-
<< std::setw(2) << std::setfill('0')
732-
<< std::get<2>(cleanDuration).count() << ":"
733-
<< std::setfill('0') << std::setw(6)
734-
<< std::fixed << std::setprecision(3)
735-
<< std::get<3>(cleanDuration).count() +
736-
std::get<4>(cleanDuration).count()/1000.0;
737-
return outputString.str();
738-
}
739-
740-
// The following regex takes a time string in the general format of
741-
// "dd hh:mm:ss.nnn" where n is milliseconds, if just one number is
742-
// provided, it is assumed to be seconds
743-
static const std::regex time_regex(
744-
"^([0-9]+ ){0,1}" // day:
745-
// Any positive integer
746-
747-
"(?:([1-9]:|[0-1][0-9]:|2[0-3]:){0,1}" // hour:
748-
// 1 - 9:
749-
// 01 - 19:
750-
// 20 - 23:
751-
752-
"([0-9]:|[0-5][0-9]:)){0,1}" // minute:
753-
// 0 - 9:
754-
// 00 - 59:
755-
756-
"(?:([0-9]|[0-5][0-9]){0,1}" // second:
757-
// 0 - 9
758-
// 00 - 59
759-
760-
"(\\.[0-9]{1,3}){0,1})$"); // millisecond:
761-
// .0 - .9
762-
// .00 - .99
763-
// .000 - 0.999
764-
693+
std::string durationToString(
694+
const std::chrono::steady_clock::duration &_duration);
765695

766696
/// \brief Split a std::chrono::steady_clock::duration to a string
767697
/// \param[in] _timeString The string to convert in general format
@@ -771,165 +701,29 @@ namespace ignition
771701
/// \param[out] numberSeconds number of seconds in the string
772702
/// \param[out] numberMilliseconds number of milliseconds in the string
773703
/// \return True if the regex was able to split the string otherwise False
774-
inline bool splitTimeBasedOnTimeRegex(
704+
bool splitTimeBasedOnTimeRegex(
775705
const std::string &_timeString,
776706
uint64_t & numberDays, uint64_t & numberHours,
777707
uint64_t & numberMinutes, uint64_t & numberSeconds,
778-
uint64_t & numberMilliseconds)
779-
{
780-
std::smatch matches;
781-
782-
// `matches` should always be a size of 6 as there are 6 matching
783-
// groups in the regex.
784-
// 1. The whole regex
785-
// 2. The days
786-
// 3. The hours
787-
// 4. The minutes
788-
// 5. The seconds
789-
// 6. The milliseconds
790-
// We can also index them as such below.
791-
// Note that the space will remain in the day match, the colon
792-
// will remain in the hour and minute matches, and the period will
793-
// remain in the millisecond match
794-
if (!std::regex_search(_timeString, matches, time_regex) ||
795-
matches.size() != 6)
796-
return false;
797-
798-
std::string dayString = matches[1];
799-
std::string hourString = matches[2];
800-
std::string minuteString = matches[3];
801-
std::string secondString = matches[4];
802-
std::string millisecondString = matches[5];
803-
804-
// Days are the only unbounded number, so check first to see if stoi
805-
// runs successfully
806-
if (!dayString.empty())
807-
{
808-
// Erase the space
809-
dayString.erase(dayString.length() - 1);
810-
try
811-
{
812-
numberDays = std::stoi(dayString);
813-
}
814-
catch (const std::out_of_range &)
815-
{
816-
return false;
817-
}
818-
}
819-
820-
if (!hourString.empty())
821-
{
822-
// Erase the colon
823-
hourString.erase(hourString.length() - 1);
824-
numberHours = std::stoi(hourString);
825-
}
826-
827-
if (!minuteString.empty())
828-
{
829-
// Erase the colon
830-
minuteString.erase(minuteString.length() - 1);
831-
numberMinutes = std::stoi(minuteString);
832-
}
833-
834-
if (!secondString.empty())
835-
{
836-
numberSeconds = std::stoi(secondString);
837-
}
838-
839-
if (!millisecondString.empty())
840-
{
841-
// Erase the period
842-
millisecondString.erase(0, 1);
843-
844-
// Multiplier because "4" = 400 ms, "04" = 40 ms, and "004" = 4 ms
845-
numberMilliseconds = std::stoi(millisecondString) *
846-
static_cast<uint64_t>(1000 / pow(10, millisecondString.length()));
847-
}
848-
return true;
849-
}
708+
uint64_t & numberMilliseconds);
850709

851710
/// \brief Convert a string to a std::chrono::steady_clock::duration
852711
/// \param[in] _timeString The string to convert in general format
853712
/// "dd hh:mm:ss.nnn" where n is millisecond value
854713
/// \return A std::chrono::steady_clock::duration containing the
855714
/// string's time value. If it isn't possible to convert, the duration will
856715
/// be zero.
857-
inline std::chrono::steady_clock::duration stringToDuration(
858-
const std::string &_timeString)
859-
{
860-
using namespace std::chrono_literals;
861-
std::chrono::steady_clock::duration duration{
862-
std::chrono::steady_clock::duration::zero()};
863-
864-
if (_timeString.empty())
865-
return duration;
866-
867-
uint64_t numberDays = 0;
868-
uint64_t numberHours = 0;
869-
uint64_t numberMinutes = 0;
870-
uint64_t numberSeconds = 0;
871-
uint64_t numberMilliseconds = 0;
872-
873-
if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
874-
numberMinutes, numberSeconds,
875-
numberMilliseconds))
876-
{
877-
return duration;
878-
}
879-
880-
// TODO(anyone): Replace below day conversion with std::chrono::days.
881-
/// This will exist in C++-20
882-
duration = std::chrono::steady_clock::duration::zero();
883-
auto delta = std::chrono::milliseconds(numberMilliseconds) +
884-
std::chrono::seconds(numberSeconds) +
885-
std::chrono::minutes(numberMinutes) +
886-
std::chrono::hours(numberHours) +
887-
std::chrono::hours(24 * numberDays);
888-
duration += delta;
889-
890-
return duration;
891-
}
716+
std::chrono::steady_clock::duration stringToDuration(
717+
const std::string &_timeString);
892718

893719
/// \brief Convert a string to a std::chrono::steady_clock::time_point
894720
/// \param[in] _timeString The string to convert in general format
895721
/// "dd hh:mm:ss.nnn" where n is millisecond value
896722
/// \return A std::chrono::steady_clock::time_point containing the
897723
/// string's time value. If it isn't possible to convert, the time will
898724
/// be negative 1 second.
899-
inline std::chrono::steady_clock::time_point stringToTimePoint(
900-
const std::string &_timeString)
901-
{
902-
using namespace std::chrono_literals;
903-
std::chrono::steady_clock::time_point timePoint{-1s};
904-
905-
if (_timeString.empty())
906-
return timePoint;
907-
908-
uint64_t numberDays = 0;
909-
uint64_t numberHours = 0;
910-
uint64_t numberMinutes = 0;
911-
uint64_t numberSeconds = 0;
912-
uint64_t numberMilliseconds = 0;
913-
914-
if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
915-
numberMinutes, numberSeconds,
916-
numberMilliseconds))
917-
{
918-
return timePoint;
919-
}
920-
921-
// TODO(anyone): Replace below day conversion with std::chrono::days.
922-
/// This will exist in C++-20
923-
timePoint = math::secNsecToTimePoint(0, 0);
924-
auto duration = std::chrono::milliseconds(numberMilliseconds) +
925-
std::chrono::seconds(numberSeconds) +
926-
std::chrono::minutes(numberMinutes) +
927-
std::chrono::hours(numberHours) +
928-
std::chrono::hours(24 * numberDays);
929-
timePoint += duration;
930-
931-
return timePoint;
932-
}
725+
std::chrono::steady_clock::time_point stringToTimePoint(
726+
const std::string &_timeString);
933727

934728
// Degrade precision on Windows, which cannot handle 'long double'
935729
// values properly. See the implementation of Unpair.

0 commit comments

Comments
 (0)