diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmStringCommand.cxx | 17 | ||||
-rw-r--r-- | Source/cmStringCommand.h | 5 |
2 files changed, 19 insertions, 3 deletions
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 2313853..3bd47a4 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -700,6 +700,9 @@ bool cmStringCommand return false; } + static bool seeded = false; + bool force_seed = false; + int seed = (int) time(NULL); int length = 5; const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm" "QWERTYUIOPASDFGHJKLZXCVBNM" @@ -723,6 +726,12 @@ bool cmStringCommand ++i; alphabet = args[i]; } + else if ( args[i] == "RANDOM_SEED" ) + { + ++i; + seed = atoi(args[i].c_str()); + force_seed = true; + } } } if ( !alphabet.size() ) @@ -744,7 +753,13 @@ bool cmStringCommand const std::string& variableName = args[args.size()-1]; std::vector<char> result; - srand((int)time(NULL)); + + if (!seeded || force_seed) + { + seeded = true; + srand(seed); + } + const char* alphaPtr = alphabet.c_str(); int cc; for ( cc = 0; cc < length; cc ++ ) diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h index 6e4bd86..2a916b4 100644 --- a/Source/cmStringCommand.h +++ b/Source/cmStringCommand.h @@ -89,7 +89,7 @@ public: " string(SUBSTRING <string> <begin> <length> <output variable>)\n" " string(STRIP <string> <output variable>)\n" " string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n" - " <output variable>)\n" + " [RANDOM_SEED <seed>] <output variable>)\n" "REGEX MATCH will match the regular expression once and store the " "match in the output variable.\n" "REGEX MATCHALL will match the regular expression as many times as " @@ -115,7 +115,8 @@ public: "RANDOM will return a random string of given length consisting of " "characters from the given alphabet. Default length is 5 " "characters and default alphabet is all numbers and upper and " - "lower case letters.\n" + "lower case letters. If an integer RANDOM_SEED is given, its " + "value will be used to seed the random number generator.\n" "The following characters have special meaning in regular expressions:\n" " ^ Matches at beginning of a line\n" " $ Matches at end of a line\n" |