User Guide
string_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "utils/string_utils.hpp"
9 
10 #include "utils/fmt.h"
11 
12 #include <limits>
13 #include <string>
14 
15 namespace nmodl {
16 namespace stringutils {
17 
18 std::string to_string(double value, const std::string& format_spec) {
19  // double containing integer value
20  if (std::ceil(value) == value &&
21  value < static_cast<double>(std::numeric_limits<long long>::max()) &&
22  value > static_cast<double>(std::numeric_limits<long long>::min())) {
23  return std::to_string(static_cast<long long>(value));
24  }
25 
26  // actual float value
27  return fmt::format(format_spec, value);
28 }
29 
30 std::string join_arguments(const std::string& lhs, const std::string& rhs) {
31  if (lhs.empty()) {
32  return rhs;
33  } else if (rhs.empty()) {
34  return lhs;
35  } else {
36  return fmt::format("{}", fmt::join({lhs, rhs}, ", "));
37  }
38 }
39 
40 
41 } // namespace stringutils
42 } // namespace nmodl
nmodl::stringutils::join_arguments
std::string join_arguments(const std::string &lhs, const std::string &rhs)
Joint two (list of) arguments.
Definition: string_utils.cpp:30
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
string_utils.hpp
Implement string manipulation functions.
fmt.h
nmodl::stringutils::to_string
std::string to_string(double value, const std::string &format_spec)
Convert double value to string without trailing zeros.
Definition: string_utils.cpp:18