2 ###############################################################################
4 ## Copyright (c) 2004 by Steffen Beyer. ##
5 ## All rights reserved. ##
7 ## This package is free software; you can redistribute it ##
8 ## and/or modify it under the same terms as Perl itself. ##
10 ###############################################################################
12 package Bit::Vector::String;
15 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
21 @ISA = qw(Exporter Bit::Vector);
32 use Carp::Clan '^Bit::Vector\b';
34 my $Factor = log(10) / log(2);
38 croak('Usage: $vector->to_Oct();') unless (@_ == 1);
43 eval { $string = reverse(join('', $self->Chunk_List_Read(3))); };
47 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
48 $string =~ s!\s+at\s.*$!!;
56 croak('Usage: $vector->from_Oct($string);') unless (@_ == 2);
57 my($self,$string) = @_;
60 if ($string =~ /^[0-7]+$/)
62 eval { $self->Chunk_List_Store(3, split(//, reverse($string))); };
66 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
67 $string =~ s!\s+at\s.*$!!;
73 croak("unknown string type");
80 croak('Usage: Bit::Vector->new_Oct($bits,$string);') unless (@_ == 3);
81 my($class,$bits,$string) = @_;
85 if ($string =~ /^[0-7]+$/)
87 unless (defined $bits) { $bits = 3 * length($string); }
88 eval { $self = Bit::Vector->new($bits); $self->Chunk_List_Store(3, split(//, reverse($string))); };
92 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
93 $string =~ s!\s+at\s.*$!!;
99 croak("unknown string type");
106 croak('Usage: $vector->String_Export($type);') unless (@_ == 1 or @_ == 2);
107 my($self,$type) = @_;
111 if (not defined $type or not $type)
113 eval { $string = '0x' . $self->to_Hex(); };
115 elsif ($type eq '1' or $type =~ /^b(?:in)?$/i)
117 eval { $string = '0b' . $self->to_Bin(); };
119 elsif ($type eq '2' or $type =~ /^o(?:ct)?$/i)
121 eval { $string = '0o' . reverse(join('', $self->Chunk_List_Read(3))); };
123 elsif ($type eq '3' or $type =~ /^d(?:ec)?$/i)
125 eval { $string = $self->to_Dec(); };
127 elsif ($type eq '4' or $type =~ /^(?:h(?:ex)?|x)$/i)
129 eval { $string = '0x' . $self->to_Hex(); };
131 elsif ($type eq '5' or $type =~ /^e(?:num)?$/i)
133 eval { $string = '{' . $self->to_Enum() . '}'; };
135 elsif ($type eq '6' or $type =~ /^p(?:ack)?$/i)
137 eval { $string = ':' . $self->Size() . ':' . $self->Block_Read(); };
141 croak("unknown string type '$type'");
146 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
147 $string =~ s!\s+at\s.*$!!;
155 croak('Usage: $vector->String_Import($string);') unless (@_ == 2);
156 my($self,$string) = @_;
160 if ($string =~ /^(?:0[bB])?([01]+)$/)
163 eval { $self->from_Bin($1); };
165 elsif ($string =~ /^(?:0[oO])?([0-7]+)$/)
168 eval { $self->Chunk_List_Store(3, split(//, reverse($1))); };
170 elsif ($string =~ /^(?:[+-])?[0-9]+$/)
173 eval { $self->from_Dec($string); };
175 elsif ($string =~ /^(?:0[hHxX])?([0-9A-Fa-f]+)$/)
178 eval { $self->from_Hex($1); };
180 elsif ($string =~ /^[(<{\[]?([0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*)[)>}\]]?$/)
183 eval { $self->from_Enum($1); };
185 elsif ($string =~ s!^:\d+:!!)
188 eval { $self->Block_Store($string); };
192 croak("unknown string type");
197 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
198 $string =~ s!\s+at\s.*$!!;
206 croak('Usage: Bit::Vector->new_String($bits,$string);') unless (@_ == 3);
207 my($class,$bits,$string) = @_;
212 if ($string =~ /^(?:0[bB])?([01]+)$/)
215 unless (defined $bits) { $bits = length($1); }
216 eval { $self = Bit::Vector->new_Bin($bits,$1); };
218 elsif ($string =~ /^(?:0[oO])?([0-7]+)$/)
221 unless (defined $bits) { $bits = 3 * length($1); }
222 eval { $self = Bit::Vector->new($bits); $self->Chunk_List_Store(3, split(//, reverse($1))); };
224 elsif ($string =~ /^(?:[+-])?([0-9]+)$/)
227 unless (defined $bits) { $bits = int( length($1) * $Factor + 1 ); }
228 eval { $self = Bit::Vector->new_Dec($bits,$string); };
230 elsif ($string =~ /^(?:0[hHxX])?([0-9A-Fa-f]+)$/)
233 unless (defined $bits) { $bits = 4 * length($1); }
234 eval { $self = Bit::Vector->new_Hex($bits,$1); };
236 elsif ($string =~ /^[(<{\[]?([0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*)[)>}\]]?$/)
240 unless (defined $bits)
243 while ($string =~ /([0-9]+)/g) { $bits = $1 if ($1 > $bits); }
246 eval { $self = Bit::Vector->new_Enum($bits,$string); };
248 elsif ($string =~ s!^:(\d+):!!)
251 $bits = $1 unless (defined $bits);
252 eval { $self = Bit::Vector->new($bits); $self->Block_Store($string); };
256 croak("unknown string type");
261 $string =~ s!^[A-Za-z0-9_]+(?:::[A-Za-z0-9_]+)*\(\):\s+!!;
262 $string =~ s!\s+at\s.*$!!;
265 if (wantarray) { return($self,$type); }