Replace std::regex with sscanf calls (#6468)

* Replace std::regex with sscanf calls

* Fix CI

* Use regular formatting placeholders

* Fix
This commit is contained in:
Jesse Hills 2024-04-04 13:41:41 +13:00 committed by GitHub
parent f09bfa7311
commit 0148ebcaa6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 44 deletions

View file

@ -1,9 +1,7 @@
#ifdef USE_DATETIME
#include <regex>
#endif
#include "helpers.h"
#include "time.h" // NOLINT
#include "helpers.h"
#include <cinttypes>
namespace esphome {
@ -66,48 +64,47 @@ std::string ESPTime::strftime(const std::string &format) {
return timestr;
}
#ifdef USE_DATETIME
bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
// clang-format off
std::regex dt_regex(R"(^
(
(\d{4})-(\d{1,2})-(\d{1,2})
(?:\s(?=.+))
)?
(
(\d{1,2}):(\d{2})
(?::(\d{2}))?
)?
$)");
// clang-format on
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
int num;
std::smatch match;
if (std::regex_match(time_to_parse, match, dt_regex) == 0)
if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &year, &month, &day, // NOLINT
&hour, // NOLINT
&minute, // NOLINT
&second, &num) == 6 && // NOLINT
num == time_to_parse.size()) {
esp_time.year = year;
esp_time.month = month;
esp_time.day_of_month = day;
esp_time.hour = hour;
esp_time.minute = minute;
esp_time.second = second;
} else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
num == time_to_parse.size()) {
esp_time.hour = hour;
esp_time.minute = minute;
esp_time.second = second;
} else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
num == time_to_parse.size()) {
esp_time.hour = hour;
esp_time.minute = minute;
esp_time.second = 0;
} else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
num == time_to_parse.size()) {
esp_time.year = year;
esp_time.month = month;
esp_time.day_of_month = day;
} else {
return false;
if (match[1].matched) { // Has date parts
esp_time.year = parse_number<uint16_t>(match[2].str()).value_or(0);
esp_time.month = parse_number<uint8_t>(match[3].str()).value_or(0);
esp_time.day_of_month = parse_number<uint8_t>(match[4].str()).value_or(0);
}
if (match[5].matched) { // Has time parts
esp_time.hour = parse_number<uint8_t>(match[6].str()).value_or(0);
esp_time.minute = parse_number<uint8_t>(match[7].str()).value_or(0);
if (match[8].matched) {
esp_time.second = parse_number<uint8_t>(match[8].str()).value_or(0);
} else {
esp_time.second = 0;
}
}
return true;
}
#endif
void ESPTime::increment_second() {
this->timestamp++;
if (!increment_time_value(this->second, 0, 60))

View file

@ -67,8 +67,6 @@ struct ESPTime {
this->day_of_year < 367 && this->month > 0 && this->month < 13;
}
#ifdef USE_DATETIME
/** Convert a string to ESPTime struct as specified by the format argument.
* @param time_to_parse null-terminated c string formatet like this: 2020-08-25 05:30:00.
* @param esp_time an instance of a ESPTime struct
@ -76,8 +74,6 @@ struct ESPTime {
*/
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);
#endif
/// Convert a C tm struct instance with a C unix epoch timestamp to an ESPTime instance.
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);