User Guide
common_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 "common_utils.hpp"
9 
10 #include <array>
11 #include <cassert>
12 #include <cerrno>
13 #include <fstream>
14 #include <iostream>
15 #include <random>
16 #include <stdexcept>
17 #include <string>
18 #include <string_view>
19 
20 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
21 #define IS_WINDOWS
22 #endif
23 
24 namespace nmodl {
25 namespace utils {
26 
27 std::string generate_random_string(const int len, UseNumbersInString use_numbers) {
28  std::string s(len, 0);
29  constexpr std::size_t number_of_numbers{10};
30  constexpr std::string_view alphanum{
31  "0123456789"
32  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33  "abcdefghijklmnopqrstuvwxyz"};
34  std::random_device dev;
35  std::mt19937 rng(dev());
36  std::uniform_int_distribution<std::mt19937::result_type> dist(use_numbers ? 0
37  : number_of_numbers,
38  alphanum.size() - 1);
39  for (int i = 0; i < len; ++i) {
40  s[i] = alphanum[dist(rng)];
41  }
42  return s;
43 }
44 
45 } // namespace utils
46 } // namespace nmodl
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::utils::UseNumbersInString
UseNumbersInString
Enum to wrap bool variable to select if random string should have numbers or not.
Definition: common_utils.hpp:51
common_utils.hpp
Common utility functions for file/dir manipulation.
nmodl::utils::generate_random_string
std::string generate_random_string(const int len, UseNumbersInString use_numbers)
Generate random std::string of length len based on a uniform distribution.
Definition: common_utils.cpp:27