| 1 | package Wildling::Generator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Wildling::ParsePattern qw(parse_pattern); | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ( $class, $input_pattern, $dictionaries ) = @_; | ||
| 10 | my $tokens = parse_pattern( $input_pattern, $dictionaries ); | ||
| 11 | my $count = 1; | ||
| 12 | for my $token (@$tokens) { | ||
| 13 | $count *= $token->count(); | ||
| 14 | } | ||
| 15 | return bless { | ||
| 16 | source => $input_pattern, | ||
| 17 | tokens => $tokens, | ||
| 18 | count => $count, | ||
| 19 | }, $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub source { | ||
| 23 | my ($self) = @_; | ||
| 24 | return $self->{source}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub count { | ||
| 28 | my ($self) = @_; | ||
| 29 | return $self->{count}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub tokens { | ||
| 33 | my ($self) = @_; | ||
| 34 | return $self->{tokens}; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub get { | ||
| 38 | my ( $self, $index ) = @_; | ||
| 39 | return '' if $index > $self->{count} - 1 || $index < 0; | ||
| 40 | |||
| 41 | my @string_array; | ||
| 42 | my $index_with_offset = $index; | ||
| 43 | for my $token ( @{ $self->{tokens} } ) { | ||
| 44 | my $token_count = $token->count(); | ||
| 45 | push @string_array, $token->get( $index_with_offset % $token_count ); | ||
| 46 | $index_with_offset = int( $index_with_offset / $token_count ); | ||
| 47 | } | ||
| 48 | return join '', @string_array; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |