• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "number.h"
17 #include "lexer/lexer.h"
18 
19 namespace ark::es2panda::lexer {
20 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,bugprone-exception-escape)
Number(util::StringView str,NumberFlags flags)21 Number::Number(util::StringView str, NumberFlags flags) noexcept : str_(str), flags_(flags)
22 {
23     Lexer::ConversionResult res {};
24 
25     if ((flags & (NumberFlags::DECIMAL_POINT | NumberFlags::EXPONENT)) == std::underlying_type_t<TokenFlags>(0)) {
26         const int64_t temp = Lexer::StrToNumeric(&std::strtoll, str.Utf8().data(), res, 10);
27 
28         if (res == Lexer::ConversionResult::SUCCESS) {
29             if (temp <= std::numeric_limits<int32_t>::max() && temp >= std::numeric_limits<int32_t>::min()) {
30                 num_ = static_cast<int32_t>(temp);
31             } else {
32                 num_ = temp;
33             }
34         } else {
35             flags_ |= NumberFlags::ERROR;
36         }
37     } else {
38         const double temp = Lexer::StrToNumeric(&std::strtod, str.Utf8().data(), res);
39         if (res == Lexer::ConversionResult::SUCCESS) {
40             if (str.Utf8().back() != 'f') {
41                 num_ = temp;
42             } else {
43                 num_ = static_cast<float>(temp);
44             }
45         } else {
46             flags_ |= NumberFlags::ERROR;
47         }
48     }
49 }
50 }  // namespace ark::es2panda::lexer
51