Proper naming for RsRandom string functions

This commit is contained in:
Gioacchino Mazzurco 2020-06-12 18:41:42 +02:00
parent b49dfaead0
commit 5ff5a32df7
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
2 changed files with 30 additions and 7 deletions

View File

@ -121,13 +121,26 @@ double RsRandom::random_f64()
return random_u64() / (double)(~(uint64_t)0) ;
}
std::string RsRandom::random_alphaNumericString(uint32_t len)
/*static*/ std::string RsRandom::alphaNumeric(uint32_t length)
{
std::string s = "" ;
std::string s;
while(s.size() < length)
{
uint8_t rChar; random_bytes(&rChar, 1); rChar = rChar % 123;
/* if(isalnum(val)) isalnum result may vary depend on locale!! */
if( (rChar >= 48 && rChar <= 57) /* 0-9 */ ||
(rChar >= 65 && rChar <= 90) /* A-Z */ ||
(rChar >= 97 && rChar <= 122) /* a-z */ )
s += static_cast<char>(rChar);
}
for(uint32_t i=0;i<len;++i)
s += (char)( (random_u32()%94) + 33) ;
return s ;
return s;
}
/*static*/ std::string RsRandom::printable(uint32_t length)
{
std::string ret(length, 0);
random_bytes(reinterpret_cast<uint8_t*>(&ret[0]), length);
for(uint32_t i=0; i<length; ++i) ret[i] = (ret[i] % 94) + 33;
return ret;
}

View File

@ -46,9 +46,19 @@ public:
static bool seed(uint32_t s);
static std::string random_alphaNumericString(uint32_t length);
static void random_bytes(uint8_t* data, uint32_t length);
/** Return a random alphanumeric string of the given lenght */
static std::string alphaNumeric(uint32_t length);
/** Return a random printable string of the given lenght */
static std::string printable(uint32_t length);
/** This return a printable string not an alphanumeric one @deprecated */
RS_DEPRECATED_FOR("RsRandom::printable")
static inline std::string random_alphaNumericString(uint32_t length)
{ return printable(length); }
private:
static RsMutex rndMtx;