KSeExpr  4.0.4.0
Utils.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #include "Utils.h"
5 #include <KSeExpr/Utils.h>
6 
7 bool KSeExpr::Utils::parseRangeComment(const std::string &comment, double &from, double &to)
8 {
9  if (comment.find_first_of('#') != 0) {
10  return false;
11  }
12 
13  auto trimmed_comment = trim(comment.substr(1, std::string::npos));
14 
15  auto div = trimmed_comment.find_first_of(',');
16 
17  if (div == std::string::npos) {
18  return false;
19  }
20 
21  std::string first {trim(trimmed_comment.substr(0, div))};
22 
23  std::string second {trim(trimmed_comment.substr(div + 1, std::string::npos))};
24 
25  auto i = KSeExpr::Utils::atof(first);
26 
27  auto j = KSeExpr::Utils::atof(second);
28 
29  if (std::isfinite(i) && std::isfinite(j)) {
30  from = i, to = j;
31  return true;
32  }
33 
34  return false;
35 }
36 
37 bool KSeExpr::Utils::parseRangeComment(const std::string &comment, float &from, float &to)
38 {
39  if (comment.find_first_of('#') != 0) {
40  return false;
41  }
42 
43  auto trimmed_comment = trim(comment.substr(1, std::string::npos));
44 
45  auto div = trimmed_comment.find_first_of(',');
46 
47  if (div == std::string::npos) {
48  return false;
49  }
50 
51  std::string first {trim(trimmed_comment.substr(0, div))};
52 
53  std::string second {trim(trimmed_comment.substr(div + 1, std::string::npos))};
54 
55  auto i = static_cast<float>(KSeExpr::Utils::atof(first));
56 
57  auto j = static_cast<float>(KSeExpr::Utils::atof(second));
58 
59  if (std::isfinite(i) && std::isfinite(j)) {
60  from = i, to = j;
61  return true;
62  }
63 
64  return false;
65 }
66 
67 bool KSeExpr::Utils::parseRangeComment(const std::string &comment, int32_t &from, int32_t &to)
68 {
69  if (comment.find_first_of('#') != 0) {
70  return false;
71  }
72 
73  auto trimmed_comment = trim(comment.substr(1, std::string::npos));
74 
75  auto div = trimmed_comment.find_first_of(',');
76 
77  if (div == std::string::npos) {
78  return false;
79  }
80 
81  std::string first {trim(trimmed_comment.substr(0, div))};
82 
83  std::string second {trim(trimmed_comment.substr(div + 1, std::string::npos))};
84 
85  try {
86  auto i = KSeExpr::Utils::strtol(first);
87  auto j = KSeExpr::Utils::strtol(second);
88 
89  from = i, to = j;
90  return true;
91  }
92  catch(const std::exception&) {
93  return false;
94  }
95 
96  return false;
97 }
98 
99 bool KSeExpr::Utils::parseTypeNameComment(const std::string &comment, std::string &type, std::string &name)
100 {
101  if (comment.find_first_of('#') != 0) {
102  return false;
103  }
104 
105  auto trimmed_comment = trim(comment.substr(1, std::string::npos));
106 
107  auto div = trimmed_comment.find_first_of(' ');
108 
109  if (div == std::string::npos) {
110  return false;
111  }
112 
113  auto first = trimmed_comment.substr(0, div);
114 
115  auto second = trimmed_comment.substr(div + 1, std::string::npos);
116 
117  type.replace(0, type.size(), first);
118 
119  name.replace(0, name.size(), second);
120 
121  return true;
122 }
123 
124 bool KSeExpr::Utils::parseLabelComment(const std::string &comment, std::string &label)
125 {
126  if (comment.find_first_of('#') != 0) {
127  return false;
128  }
129 
130  auto trimmed_comment = trim(comment.substr(1, std::string::npos));
131 
132  auto first = trimmed_comment.substr(0, std::string::npos);
133 
134  label.replace(0, label.size(), first);
135 
136  return true;
137 }
bool parseRangeComment(const std::string &comment, double &from, double &to)
Definition: Utils.cpp:7
bool parseLabelComment(const std::string &comment, std::string &label)
Definition: Utils.cpp:124
bool parseTypeNameComment(const std::string &comment, std::string &type, std::string &name)
Definition: Utils.cpp:99
int32_t strtol(const char *num)
double_t atof(const char *num)
Definition: Utils.cpp:51
std::string trim(const std::string &s)
Definition: Utils.h:19