diff options
author | Roland Reichwein <mail@reichwein.it> | 2022-11-27 22:01:30 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2022-11-27 22:01:30 +0100 |
commit | 6a72251a10019bd42d10bb70ab26916826ac4527 (patch) | |
tree | 6a7ea82bdf8938ef511da98c25706dee0874fb3f | |
parent | d3ec5782691be62f0dffcbe7de8f968e94a49001 (diff) |
Generating new page: retry on collision
-rw-r--r-- | whiteboard.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp index 09674b5..b3367cf 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -36,23 +36,31 @@ namespace { return result & 0x7FFFFFFF; } + fs::path data_path; + std::string generate_id() { static std::random_device r; static std::default_random_engine e1(r()); static std::uniform_int_distribution<int> uniform_dist(0, 15); - std::string result; - for (int i = 0; i < 6; i++) { - char c{static_cast<char>('0' + uniform_dist(e1))}; - if (c > '9') - c = c - '9' + 'a'; - result.push_back(c); + // limit tries + for (int j = 0; j < 100000; j++) { + std::string result; + for (int i = 0; i < 6; i++) { + char c{static_cast<char>('0' + uniform_dist(e1))}; + if (c > '9') + c = c - '9' + 'a'; + result.push_back(c); + } + fs::path path{data_path / result}; + + if (!fs::exists(path)) + return result; } - return result; - } - fs::path data_path; + return "endofcodes"; + } void setFileById(const std::string& data, const std::string& id) { |