From 6a72251a10019bd42d10bb70ab26916826ac4527 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 27 Nov 2022 22:01:30 +0100 Subject: Generating new page: retry on collision --- whiteboard.cpp | 26 +++++++++++++++++--------- 1 file 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 uniform_dist(0, 15); - std::string result; - for (int i = 0; i < 6; i++) { - char c{static_cast('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('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) { -- cgit v1.2.3