From cac60663f553cf6a985050c068d906778e66214e Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Mon, 27 Jun 2022 16:49:11 -0400 Subject: [PATCH] common: add gen_rand_numeric() Signed-off-by: Casey Bodley --- src/common/random_string.cc | 16 ++++++++++++++++ src/common/random_string.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/common/random_string.cc b/src/common/random_string.cc index c728956182a4d..9ce8ded18a3ea 100644 --- a/src/common/random_string.cc +++ b/src/common/random_string.cc @@ -125,3 +125,19 @@ std::string gen_rand_alphanumeric_plain(CephContext *cct, size_t size) str.pop_back(); // pop the extra \0 return str; } + +void gen_rand_numeric(CephContext *cct, char *dest, size_t size) /* size should be the required string size + 1 */ +{ + static constexpr char table[] = "0123456789"; + choose_from(cct->random(), table, dest, size-1); + dest[size-1] = 0; +} + +std::string gen_rand_numeric(CephContext *cct, size_t size) +{ + std::string str; + str.resize(size + 1); + gen_rand_numeric(cct, str.data(), str.size()); + str.pop_back(); // pop the extra \0 + return str; +} diff --git a/src/common/random_string.h b/src/common/random_string.h index b5dd9825ebf47..2516425a6b99e 100644 --- a/src/common/random_string.h +++ b/src/common/random_string.h @@ -26,6 +26,7 @@ void gen_rand_alphanumeric_lower(CephContext *cct, char *dest, size_t size); void gen_rand_alphanumeric_upper(CephContext *cct, char *dest, size_t size); void gen_rand_alphanumeric_no_underscore(CephContext *cct, char *dest, size_t size); void gen_rand_alphanumeric_plain(CephContext *cct, char *dest, size_t size); +void gen_rand_numeric(CephContext *cct, char *dest, size_t size); // returns a std::string with 'size' random characters std::string gen_rand_alphanumeric(CephContext *cct, size_t size); @@ -33,3 +34,4 @@ std::string gen_rand_alphanumeric_lower(CephContext *cct, size_t size); std::string gen_rand_alphanumeric_upper(CephContext *cct, size_t size); std::string gen_rand_alphanumeric_no_underscore(CephContext *cct, size_t size); std::string gen_rand_alphanumeric_plain(CephContext *cct, size_t size); +std::string gen_rand_numeric(CephContext *cct, size_t size); -- 2.39.5