]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add gen_rand_numeric()
authorCasey Bodley <cbodley@redhat.com>
Mon, 27 Jun 2022 20:49:11 +0000 (16:49 -0400)
committerCasey Bodley <cbodley@redhat.com>
Wed, 10 Apr 2024 16:53:04 +0000 (12:53 -0400)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/common/random_string.cc
src/common/random_string.h

index c728956182a4ddf922d987828df0621751c385e7..9ce8ded18a3ea5a2f87a40fd5dc09e7a293f9e1e 100644 (file)
@@ -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;
+}
index b5dd9825ebf4714c63e7775c81546a2573473ee4..2516425a6b99ea42c5500a965d7e786baafa9f62 100644 (file)
@@ -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);