Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions re2/re2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <atomic>
#include <map>
#include <string>
#include <system_error>
#include <utility>
#include <vector>

Expand All @@ -28,6 +29,7 @@
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/ascii.h"
#include "absl/strings/charconv.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "re2/prog.h"
Expand Down Expand Up @@ -1176,11 +1178,12 @@ bool Parse(const char* str, size_t n, float* dest) {
static const int kMaxLength = 200;
char buf[kMaxLength+1];
str = TerminateNumber(buf, sizeof buf, str, &n, true);
char* end;
errno = 0;
float r = strtof(str, &end);
if (end != str + n) return false; // Leftover junk
if (errno) return false;
if (str[0] == '\0') return false;
// Use absl::from_chars() instead of strtof() to avoid the radix character
// (and thus the accepted grammar) depending on the process's LC_NUMERIC.
float r;
absl::from_chars_result res = absl::from_chars(str, str + n, r);
if (res.ec != std::errc() || res.ptr != str + n) return false; // Leftover junk
if (dest == NULL) return true;
*dest = r;
return true;
Expand All @@ -1192,11 +1195,12 @@ bool Parse(const char* str, size_t n, double* dest) {
static const int kMaxLength = 200;
char buf[kMaxLength+1];
str = TerminateNumber(buf, sizeof buf, str, &n, true);
char* end;
errno = 0;
double r = strtod(str, &end);
if (end != str + n) return false; // Leftover junk
if (errno) return false;
if (str[0] == '\0') return false;
// Use absl::from_chars() instead of strtod() to avoid the radix character
// (and thus the accepted grammar) depending on the process's LC_NUMERIC.
double r;
absl::from_chars_result res = absl::from_chars(str, str + n, r);
if (res.ec != std::errc() || res.ptr != str + n) return false; // Leftover junk
if (dest == NULL) return true;
*dest = r;
return true;
Expand Down
32 changes: 32 additions & 0 deletions re2/testing/re2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "re2/re2.h"

#include <errno.h>
#include <locale.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -949,6 +950,37 @@ TEST(RE2, FloatingPointFullMatchTypes) {
}
}

TEST(RE2, FloatingPointFullMatchIsLocaleIndependent) {
// Parsing must not depend on LC_NUMERIC: '.' is always the radix character
// regardless of the process locale. Skip if no comma-radix locale is present.
const char* saved = setlocale(LC_NUMERIC, NULL);
std::string savedcopy = saved != NULL ? saved : "C";
static const char* const kCommaLocales[] = {
"de_DE.UTF-8", "de_DE.utf8", "de_DE", "fr_FR.UTF-8", "nl_NL.UTF-8",
};
bool have_locale = false;
for (const char* name : kCommaLocales) {
if (setlocale(LC_NUMERIC, name) != NULL) {
have_locale = true;
break;
}
}
if (!have_locale)
GTEST_SKIP() << "no comma-radix locale available";

double d = 0;
EXPECT_TRUE(RE2::FullMatch("1.5", "(.*)", &d));
EXPECT_EQ(d, 1.5);
EXPECT_FALSE(RE2::FullMatch("1,5", "(.*)", &d));

float f = 0;
EXPECT_TRUE(RE2::FullMatch("1.5", "(.*)", &f));
EXPECT_EQ(f, 1.5f);
EXPECT_FALSE(RE2::FullMatch("1,5", "(.*)", &f));

setlocale(LC_NUMERIC, savedcopy.c_str());
}

TEST(RE2, FullMatchAnchored) {
int i;
// Check that matching is fully anchored
Expand Down