Code cleanup

This commit is contained in:
yhirose 2021-01-28 19:21:59 -05:00
parent 15aa1380de
commit 23b284d0a3

View File

@ -325,6 +325,24 @@ inline std::string resolve_escape_sequence(const char *s, size_t n) {
return r; return r;
} }
/*-----------------------------------------------------------------------------
* token_to_number_ - This function should be removed eventually
*---------------------------------------------------------------------------*/
template <typename T> T token_to_number_(std::string_view sv) {
T n = 0;
#if __has_include(<charconv>)
if constexpr (!std::is_floating_point<T>::value) {
std::from_chars(sv.data(), sv.data() + sv.size(), n);
return n;
}
#endif
auto s = std::string(sv);
std::istringstream ss(s);
ss >> n;
return n;
}
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Trie * Trie
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
@ -479,23 +497,7 @@ struct SemanticValues : protected std::vector<std::any> {
} }
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
T n = 0; return token_to_number_<T>(token());
#if __has_include(<charconv>)
if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string());
ss >> n;
return n;
} else {
auto sv = token();
std::from_chars(sv.data(), sv.data() + sv.size(), n);
return n;
}
#else
std::istringstream ss(token_to_string());
ss >> n;
return n;
#endif
} }
// Transform the semantic value vector to another vector // Transform the semantic value vector to another vector
@ -3777,23 +3779,7 @@ template <typename Annotation> struct AstBase : public Annotation {
} }
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
T n = 0; return token_to_number_<T>(token);
#if __has_include(<charconv>)
if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string());
ss >> n;
return n;
} else {
assert(is_token);
std::from_chars(token.data(), token.data() + token.size(), n);
return n;
}
#else
std::istringstream ss(token_to_string());
ss >> n;
return n;
#endif
} }
}; };