• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "debug/utils.h"
17 #include "mindspore/core/utils/log_adapter.h"
18 
19 namespace mindspore {
20 
CheckStoull(uint64_t * const output_digit,const std::string & input_str)21 bool CheckStoull(uint64_t *const output_digit, const std::string &input_str) {
22   try {
23     *output_digit = std::stoull(input_str);
24   } catch (const std::out_of_range &oor) {
25     MS_LOG(ERROR) << "Out of Range error: " << oor.what() << " when parse " << input_str;
26     return false;
27   } catch (const std::invalid_argument &ia) {
28     MS_LOG(ERROR) << "Invalid argument: " << ia.what() << " when parse " << input_str;
29     return false;
30   }
31   return true;
32 }
33 
CheckStoul(size_t * const output_digit,const std::string & input_str)34 bool CheckStoul(size_t *const output_digit, const std::string &input_str) {
35   try {
36     *output_digit = std::stoul(input_str);
37   } catch (const std::out_of_range &oor) {
38     MS_LOG(ERROR) << "Out of Range error: " << oor.what() << " when parse " << input_str;
39     return false;
40   } catch (const std::invalid_argument &ia) {
41     MS_LOG(ERROR) << "Invalid argument: " << ia.what() << " when parse " << input_str;
42     return false;
43   }
44   return true;
45 }
46 
CheckStoi(int64_t * const output_digit,const std::string & input_str)47 bool CheckStoi(int64_t *const output_digit, const std::string &input_str) {
48   try {
49     *output_digit = std::stoi(input_str);
50   } catch (const std::out_of_range &oor) {
51     MS_LOG(ERROR) << "Out of Range error: " << oor.what() << " when parse " << input_str;
52     return false;
53   } catch (const std::invalid_argument &ia) {
54     MS_LOG(ERROR) << "Invalid argument: " << ia.what() << " when parse " << input_str;
55     return false;
56   }
57   return true;
58 }
59 
CheckStringMatch(size_t start,size_t end,std::string * matched_str,const std::string & input_str)60 void CheckStringMatch(size_t start, size_t end, std::string *matched_str, const std::string &input_str) {
61   if (start != std::string::npos && end != std::string::npos && end > start && start + 1 < input_str.length()) {
62     *matched_str = input_str.substr(start + 1, end - (start + 1));
63   }
64 }
65 }  // namespace mindspore
66