Skip to content

Commit afa6a36

Browse files
committed
Make first char's character class configurable
1 parent 273046d commit afa6a36

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/rex/random_identifier_generator.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ExhaustedSpaceError < StandardError; end
3131
:min_length => 3,
3232
# This should be pretty universal for identifier rules
3333
:char_set => Rex::Text::AlphaNumeric+"_",
34+
:first_char_set => Rex::Text::LowerAlpha
3435
}
3536

3637
# @param opts [Hash] Options, see {DefaultOpts} for default values
@@ -46,8 +47,8 @@ def initialize(opts={})
4647
raise ArgumentError, "Invalid length options"
4748
end
4849

49-
# This is really just the maximum number of shortest names. Since
50-
# this will still be a pretty big number most of the time, don't
50+
# This is really just the maximum number of shortest names. This
51+
# will still be a pretty big number most of the time, so don't
5152
# bother calculating the real one, which will potentially be
5253
# expensive, since we're talking about a 36-digit decimal number to
5354
# represent the total possibilities for the range of 10- to
@@ -108,9 +109,17 @@ def store(name, value)
108109
end
109110

110111
# Create a random string that satisfies most languages' requirements
111-
# for identifiers.
112+
# for identifiers. In particular, with a default configuration, the
113+
# first character will always be lowercase alpha (unless modified by a
114+
# block), and the whole thing will contain only a-zA-Z0-9_ characters.
112115
#
113-
# Note that the first character will always be lowercase alpha.
116+
# If called with a block, the block will be given the identifier before
117+
# uniqueness checks. The block's return value will be the new
118+
# identifier. Note that the block may be called multiple times if it
119+
# returns a non-unique value.
120+
#
121+
# @note Calling this method with a block that returns only values that
122+
# this generator already contains will result in an infinite loop.
114123
#
115124
# @example
116125
# rig = Rex::RandomIdentifierGenerator.new
@@ -139,7 +148,7 @@ def generate(len=nil)
139148
# fact that you'd have to call generate at least 26*62 times (in the
140149
# case of 2-character names) to hit it with the default :char_set.
141150
loop do
142-
ident = Rex::Text.rand_text_alpha_lower(1)
151+
ident = Rex::Text.rand_base(1, "", @opts[:first_char_set])
143152
ident << Rex::Text.rand_base(len-1, "", @opts[:char_set])
144153
if block_given?
145154
ident = yield ident

0 commit comments

Comments
 (0)