From 4528bdb330823f7bf8c9ccb3365731cfcd62d619 Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Sun, 2 May 2021 22:53:50 -0700 Subject: [PATCH] Remove the use of regex library It is not working correctly in gcc 4.8 or earlier, which is still popular (e.g. bundled by default in CentOS 7). Fixes #865, #929. Change-Id: I55a42428dbd2a12fc2c3b1e6a49fdd662a295dca --- .../media/formats/webvtt/webvtt_parser.cc | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/packager/media/formats/webvtt/webvtt_parser.cc b/packager/media/formats/webvtt/webvtt_parser.cc index 09e8e1c107..117b7eab7c 100644 --- a/packager/media/formats/webvtt/webvtt_parser.cc +++ b/packager/media/formats/webvtt/webvtt_parser.cc @@ -6,8 +6,6 @@ #include "packager/media/formats/webvtt/webvtt_parser.h" -#include - #include "packager/base/logging.h" #include "packager/base/strings/string_number_conversions.h" #include "packager/base/strings/string_split.h" @@ -80,15 +78,13 @@ bool IsLikelyRegion(const std::string& line) { bool ParsePercent(const std::string& str, float* value) { // https://www.w3.org/TR/webvtt1/#webvtt-percentage // E.g. "4%" or "1.5%" - std::regex re(R"((\d+(?:\.\d+)?)%)"); - std::smatch match; - if (!std::regex_match(str, match, re)) { + if (str[str.size() - 1] != '%') { return false; } double temp; - base::StringToDouble(match[1], &temp); - if (temp >= 100) { + if (!base::StringToDouble(str.substr(0, str.size() - 1), &temp) || + temp >= 100) { return false; } *value = temp; @@ -96,20 +92,18 @@ bool ParsePercent(const std::string& str, float* value) { } bool ParseDoublePercent(const std::string& str, float* a, float* b) { - std::regex re(R"((\d+(?:\.\d+)?)%,(\d+(?:\.\d+)?)%)"); - std::smatch match; - if (!std::regex_match(str, match, re)) { + auto percents = base::SplitString(str, ",", base::TRIM_WHITESPACE, + base::SPLIT_WANT_NONEMPTY); + if (percents.size() != 2) { return false; } - - double tempA, tempB; - base::StringToDouble(match[1], &tempA); - base::StringToDouble(match[2], &tempB); - if (tempA >= 100 || tempB >= 100) { + float temp_a, temp_b; + if (!ParsePercent(percents[0], &temp_a) || + !ParsePercent(percents[1], &temp_b)) { return false; } - *a = tempA; - *b = tempB; + *a = temp_a; + *b = temp_b; return true; }