-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Fix regression about parsing leading decimal points #93989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-libcxx Author: A. Jiang (frederick-vs-ja) ChangesPR #77948 mistakenly rejected floating-point representation with a leading decimal point, e.g. ".5". This PR fixes the regression mentioned in #77948 (comment). Full diff: https://github.com/llvm/llvm-project/pull/93989.diff 4 Files Affected:
diff --git a/libcxx/include/locale b/libcxx/include/locale
index 041d7bcd27fc5..c25861ff66987 100644
--- a/libcxx/include/locale
+++ b/libcxx/include/locale
@@ -986,12 +986,12 @@ _InputIterator num_get<_CharT, _InputIterator>::__do_get_floating_point(
// the leading character excluding the sign must be a decimal digit
if (!__is_leading_parsed) {
if (__a_end - __a >= 1 && __a[0] != '-' && __a[0] != '+') {
- if ('0' <= __a[0] && __a[0] <= '9')
+ if (('0' <= __a[0] && __a[0] <= '9') || __a[0] == '.')
__is_leading_parsed = true;
else
break;
} else if (__a_end - __a >= 2 && (__a[0] == '-' || __a[0] == '+')) {
- if ('0' <= __a[1] && __a[1] <= '9')
+ if (('0' <= __a[1] && __a[1] <= '9') || __a[1] == '.')
__is_leading_parsed = true;
else
break;
diff --git a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_double.pass.cpp b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_double.pass.cpp
index fbd1c7c5715ea..b16be0bdaacd6 100644
--- a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_double.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_double.pass.cpp
@@ -401,6 +401,66 @@ int main(int, char**)
assert(err == ios.goodbit);
assert(std::abs(v - 3.14159265358979e+10)/3.14159265358979e+10 < 1.e-8);
}
+ {
+ v = -1;
+ const char str[] = ".5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 2);
+ assert(err == ios.goodbit);
+ assert(v == 0.5);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 3);
+ assert(err == ios.goodbit);
+ assert(v == -0.5);
+ }
+ {
+ v = -1;
+ const char str[] = ".5E1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 4);
+ assert(err == ios.goodbit);
+ assert(v == 5.0);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5e+1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 6);
+ assert(err == ios.goodbit);
+ assert(v == -5.0);
+ }
+ {
+ v = -1;
+ const char str[] = ".625E-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 7);
+ assert(err == ios.goodbit);
+ assert(v == 0.0625);
+ }
+ {
+ v = -1;
+ const char str[] = "-.3125e-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 9);
+ assert(err == ios.goodbit);
+ assert(v == -0.03125);
+ }
return 0;
}
diff --git a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_float.pass.cpp b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_float.pass.cpp
index b5ac7d876157c..bc6cec45d89a3 100644
--- a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_float.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_float.pass.cpp
@@ -331,6 +331,66 @@ int main(int, char**)
assert(err == ios.goodbit);
assert(v == 2);
}
+ {
+ v = -1;
+ const char str[] = ".5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 2);
+ assert(err == ios.goodbit);
+ assert(v == 0.5f);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 3);
+ assert(err == ios.goodbit);
+ assert(v == -0.5f);
+ }
+ {
+ v = -1;
+ const char str[] = ".5E1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 4);
+ assert(err == ios.goodbit);
+ assert(v == 5.0f);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5e+1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 6);
+ assert(err == ios.goodbit);
+ assert(v == -5.0f);
+ }
+ {
+ v = -1;
+ const char str[] = ".625E-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 7);
+ assert(err == ios.goodbit);
+ assert(v == 0.0625f);
+ }
+ {
+ v = -1;
+ const char str[] = "-.3125e-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 9);
+ assert(err == ios.goodbit);
+ assert(v == -0.03125f);
+ }
return 0;
}
diff --git a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp
index 9617899f749c6..f455981e89780 100644
--- a/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp
+++ b/libcxx/test/std/localization/locale.categories/category.numeric/locale.num.get/facet.num.get.members/get_long_double.pass.cpp
@@ -390,6 +390,66 @@ int main(int, char**)
assert(err == ios.goodbit);
assert(v == 2);
}
+ {
+ v = -1;
+ const char str[] = ".5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 2);
+ assert(err == ios.goodbit);
+ assert(v == 0.5l);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 3);
+ assert(err == ios.goodbit);
+ assert(v == -0.5l);
+ }
+ {
+ v = -1;
+ const char str[] = ".5E1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 4);
+ assert(err == ios.goodbit);
+ assert(v == 5.0l);
+ }
+ {
+ v = -1;
+ const char str[] = "-.5e+1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 6);
+ assert(err == ios.goodbit);
+ assert(v == -5.0l);
+ }
+ {
+ v = -1;
+ const char str[] = ".625E-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 7);
+ assert(err == ios.goodbit);
+ assert(v == 0.0625l);
+ }
+ {
+ v = -1;
+ const char str[] = "-.3125e-1";
+ std::ios_base::iostate err = ios.goodbit;
+ cpp17_input_iterator<const char*> iter = f.get(
+ cpp17_input_iterator<const char*>(str), cpp17_input_iterator<const char*>(str + sizeof(str)), ios, err, v);
+ assert(base(iter) == str + 9);
+ assert(err == ios.goodbit);
+ assert(v == -0.03125l);
+ }
return 0;
}
|
PR llvm#77948 mistakenly rejected floating-point representation with a leading decimal point, e.g. ".5". This PR fixes the regression.
ef8143d
to
1949f21
Compare
ldionne
approved these changes
Jun 3, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #77948 mistakenly rejected floating-point representation with a leading decimal point, e.g. ".5".
This PR fixes the regression mentioned in #77948 (comment).