Antares Simulator
Power System Simulator
utils.hxx
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #ifndef __ANTARES_LIBS_UTILS_HXX__
22 #define __ANTARES_LIBS_UTILS_HXX__
23 
24 #include <cctype>
25 
26 #include <yuni/core/string.h>
27 
28 namespace Antares
29 {
30 template<class StringT>
31 void TransformNameIntoID(const AnyString& name, StringT& out)
32 {
33  if (not name.empty())
34  {
35  bool duppl = true;
36  out.reserve(name.size());
37 
38  for (uint i = 0; i != name.size(); ++i)
39  {
40  const char c = name[i];
41  if ((c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or (c >= '0' and c <= '9')
42  or c == '_' or c == '-' or c == '(' or c == ')' or c == ',' or c == '&' or c == ' ')
43  {
44  duppl = false;
45  out += c;
46  }
47  else
48  {
49  if (not duppl)
50  {
51  out += ' ';
52  duppl = true;
53  }
54  }
55  }
56  out.trim();
57  out.toLower();
58  }
59  else
60  {
61  // Generate an arbitrary ID
62  out << "id_" << ((size_t)(&out));
63  }
64 }
65 
66 template<>
67 void TransformNameIntoID(const AnyString& name, std::string& out);
68 
69 } // namespace Antares
70 
71 #endif // __ANTARES_LIBS_UTILS_HXX__