Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_parser.h" | 5 #include "base/json/json_parser.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 673 if (*pos_ == '-') | 673 if (*pos_ == '-') |
| 674 NextChar(); | 674 NextChar(); |
| 675 | 675 |
| 676 if (!ReadInt(false)) { | 676 if (!ReadInt(false)) { |
| 677 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 677 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 678 return nullptr; | 678 return nullptr; |
| 679 } | 679 } |
| 680 end_index = index_; | 680 end_index = index_; |
| 681 | 681 |
| 682 // The optional fraction part. | 682 // The optional fraction part. |
| 683 if (*pos_ == '.') { | 683 if (CanConsume(1) && *pos_ == '.') { |
| 684 if (!CanConsume(1)) { | |
| 685 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | |
| 686 return nullptr; | |
| 687 } | |
| 688 NextChar(); | 684 NextChar(); |
| 689 if (!ReadInt(true)) { | 685 if (!ReadInt(true)) { |
| 690 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 686 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 691 return nullptr; | 687 return nullptr; |
| 692 } | 688 } |
| 693 end_index = index_; | 689 end_index = index_; |
| 694 } | 690 } |
| 695 | 691 |
| 696 // Optional exponent part. | 692 // Optional exponent part. |
| 697 if (*pos_ == 'e' || *pos_ == 'E') { | 693 if (CanConsume(1) && (*pos_ == 'e' || *pos_ == 'E')) { |
| 698 NextChar(); | 694 NextChar(); |
| 699 if (*pos_ == '-' || *pos_ == '+') | 695 if (!CanConsume(1)) { |
|
dcheng
2017/02/24 23:05:33
In theory, we don't need 696-697, and we can fold
Robert Sesek
2017/02/24 23:07:21
Yeah, I agree that this is easier to read than fol
| |
| 696 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | |
| 697 return nullptr; | |
| 698 } | |
| 699 if (*pos_ == '-' || *pos_ == '+') { | |
| 700 NextChar(); | 700 NextChar(); |
| 701 } | |
| 701 if (!ReadInt(true)) { | 702 if (!ReadInt(true)) { |
| 702 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); | 703 ReportError(JSONReader::JSON_SYNTAX_ERROR, 1); |
| 703 return nullptr; | 704 return nullptr; |
| 704 } | 705 } |
| 705 end_index = index_; | 706 end_index = index_; |
| 706 } | 707 } |
| 707 | 708 |
| 708 // ReadInt is greedy because numbers have no easily detectable sentinel, | 709 // ReadInt is greedy because numbers have no easily detectable sentinel, |
| 709 // so save off where the parser should be on exit (see Consume invariant at | 710 // so save off where the parser should be on exit (see Consume invariant at |
| 710 // the top of the header), then make sure the next token is one which is | 711 // the top of the header), then make sure the next token is one which is |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 735 double num_double; | 736 double num_double; |
| 736 if (StringToDouble(num_string.as_string(), &num_double) && | 737 if (StringToDouble(num_string.as_string(), &num_double) && |
| 737 std::isfinite(num_double)) { | 738 std::isfinite(num_double)) { |
| 738 return base::MakeUnique<FundamentalValue>(num_double); | 739 return base::MakeUnique<FundamentalValue>(num_double); |
| 739 } | 740 } |
| 740 | 741 |
| 741 return nullptr; | 742 return nullptr; |
| 742 } | 743 } |
| 743 | 744 |
| 744 bool JSONParser::ReadInt(bool allow_leading_zeros) { | 745 bool JSONParser::ReadInt(bool allow_leading_zeros) { |
| 745 char first = *pos_; | 746 char first = *pos_; |
|
jdoerrie
2017/02/27 10:17:03
You should probably add
if (!CanConsume(1))
r
Robert Sesek
2017/02/27 19:43:37
Yeah, I noticed that as well after I ran this thro
| |
| 746 int len = 0; | 747 int len = 0; |
| 747 | 748 |
| 748 char c = first; | 749 char c = first; |
| 749 while (CanConsume(1) && IsAsciiDigit(c)) { | 750 while (CanConsume(1) && IsAsciiDigit(c)) { |
| 750 c = *NextChar(); | 751 c = *NextChar(); |
| 751 ++len; | 752 ++len; |
| 752 } | 753 } |
| 753 | 754 |
| 754 if (len == 0) | 755 if (len == 0) |
| 755 return false; | 756 return false; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 818 const std::string& description) { | 819 const std::string& description) { |
| 819 if (line || column) { | 820 if (line || column) { |
| 820 return StringPrintf("Line: %i, column: %i, %s", | 821 return StringPrintf("Line: %i, column: %i, %s", |
| 821 line, column, description.c_str()); | 822 line, column, description.c_str()); |
| 822 } | 823 } |
| 823 return description; | 824 return description; |
| 824 } | 825 } |
| 825 | 826 |
| 826 } // namespace internal | 827 } // namespace internal |
| 827 } // namespace base | 828 } // namespace base |
| OLD | NEW |