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
This commit is contained in:
KongQun Yang 2021-05-02 22:53:50 -07:00
parent 23953bf931
commit 4528bdb330
1 changed files with 11 additions and 17 deletions

View File

@ -6,8 +6,6 @@
#include "packager/media/formats/webvtt/webvtt_parser.h" #include "packager/media/formats/webvtt/webvtt_parser.h"
#include <regex>
#include "packager/base/logging.h" #include "packager/base/logging.h"
#include "packager/base/strings/string_number_conversions.h" #include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/string_split.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) { bool ParsePercent(const std::string& str, float* value) {
// https://www.w3.org/TR/webvtt1/#webvtt-percentage // https://www.w3.org/TR/webvtt1/#webvtt-percentage
// E.g. "4%" or "1.5%" // E.g. "4%" or "1.5%"
std::regex re(R"((\d+(?:\.\d+)?)%)"); if (str[str.size() - 1] != '%') {
std::smatch match;
if (!std::regex_match(str, match, re)) {
return false; return false;
} }
double temp; double temp;
base::StringToDouble(match[1], &temp); if (!base::StringToDouble(str.substr(0, str.size() - 1), &temp) ||
if (temp >= 100) { temp >= 100) {
return false; return false;
} }
*value = temp; *value = temp;
@ -96,20 +92,18 @@ bool ParsePercent(const std::string& str, float* value) {
} }
bool ParseDoublePercent(const std::string& str, float* a, float* b) { bool ParseDoublePercent(const std::string& str, float* a, float* b) {
std::regex re(R"((\d+(?:\.\d+)?)%,(\d+(?:\.\d+)?)%)"); auto percents = base::SplitString(str, ",", base::TRIM_WHITESPACE,
std::smatch match; base::SPLIT_WANT_NONEMPTY);
if (!std::regex_match(str, match, re)) { if (percents.size() != 2) {
return false; return false;
} }
float temp_a, temp_b;
double tempA, tempB; if (!ParsePercent(percents[0], &temp_a) ||
base::StringToDouble(match[1], &tempA); !ParsePercent(percents[1], &temp_b)) {
base::StringToDouble(match[2], &tempB);
if (tempA >= 100 || tempB >= 100) {
return false; return false;
} }
*a = tempA; *a = temp_a;
*b = tempB; *b = temp_b;
return true; return true;
} }