| 1 | package Sidef::Types::String::String; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use List::Util qw(); | ||
| 7 | use parent qw(Sidef::Object::Object); | ||
| 8 | |||
| 9 | use overload | ||
| 10 | q{bool} => \&get_value, | ||
| 11 | q{0+} => \&get_value, | ||
| 12 | q{""} => \&get_value, | ||
| 13 | q{@{}} => \&chars; | ||
| 14 | |||
| 15 | use Sidef::Types::Bool::Bool; | ||
| 16 | use Sidef::Types::Number::Number; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my (undef, $str) = @_; | ||
| 20 | if (@_ > 2) { | ||
| 21 | shift(@_); | ||
| 22 | $str = CORE::join('', @_); | ||
| 23 | } | ||
| 24 | bless \"$str"; | ||
| 25 | } | ||
| 26 | |||
| 27 | *call = \&new; | ||
| 28 | |||
| 29 | sub get_value { | ||
| 30 | ${$_[0]}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub inc { | ||
| 34 | my ($self) = @_; | ||
| 35 | my $copy = $$self; | ||
| 36 | $self->new(++$copy); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub div { | ||
| 40 | my ($self, $num) = @_; | ||
| 41 | (my $strlen = CORE::int(length($$self) / CORE::int($num))) < 1 and return $self->chars; | ||
| 42 | Sidef::Types::Array::Array->new([map { bless \$_ } unpack "(a$strlen)*", $$self]); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub lt { | ||
| 46 | my ($self, $string) = @_; | ||
| 47 | $$self lt "$string" | ||
| 48 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 49 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub gt { | ||
| 53 | my ($self, $string) = @_; | ||
| 54 | $$self gt "$string" | ||
| 55 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 56 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub le { | ||
| 60 | my ($self, $string) = @_; | ||
| 61 | $$self le "$string" | ||
| 62 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 63 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub ge { | ||
| 67 | my ($self, $string) = @_; | ||
| 68 | $$self ge "$string" | ||
| 69 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 70 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub diff { | ||
| 74 | my ($self, $obj) = @_; | ||
| 75 | |||
| 76 | if (ref($obj) eq 'Sidef::Types::Regex::Regex') { | ||
| 77 | |||
| 78 | my $str = $$self; | ||
| 79 | if (exists $obj->{global}) { | ||
| 80 | return $self->new($str =~ s/$obj->{regex}//gr); | ||
| 81 | } | ||
| 82 | |||
| 83 | if ($str =~ /$obj->{regex}/) { | ||
| 84 | return $self->new(CORE::substr($str, 0, $-[0]) . CORE::substr($str, $+[0])); | ||
| 85 | } | ||
| 86 | |||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ((my $ind = CORE::index($$self, "$obj")) != -1) { | ||
| 91 | return $self->new(CORE::substr($$self, 0, $ind) . CORE::substr($$self, $ind + CORE::length("$obj"))); | ||
| 92 | } | ||
| 93 | $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub match { | ||
| 97 | my ($self, $regex, $pos) = @_; | ||
| 98 | $regex->run($self, (defined($pos) ? $pos : ())); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub gmatch { | ||
| 102 | my ($self, $regex, $pos) = @_; | ||
| 103 | local $regex->{global} = 1; | ||
| 104 | $regex->run($self, (defined($pos) ? $pos : ())); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub to { | ||
| 108 | my ($from, $to, $step) = @_; | ||
| 109 | Sidef::Types::Range::RangeString->call($from, $to, $step // Sidef::Types::Number::Number::ONE); | ||
| 110 | } | ||
| 111 | |||
| 112 | *upto = \&to; | ||
| 113 | |||
| 114 | sub downto { | ||
| 115 | my ($from, $to, $step) = @_; | ||
| 116 | Sidef::Types::Range::RangeString->call($from, $to, defined($step) ? $step->neg : Sidef::Types::Number::Number::MONE); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub range { | ||
| 120 | my ($from, $to, $step) = @_; | ||
| 121 | Sidef::Types::Range::RangeString->call($from, $to, $step); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub cmp { | ||
| 125 | my ($self, $string) = @_; | ||
| 126 | my $cmp = $$self cmp "$string"; | ||
| 127 | $cmp == 0 ? (Sidef::Types::Number::Number::ZERO) | ||
| 128 | : $cmp > 0 ? (Sidef::Types::Number::Number::ONE) | ||
| 129 | : (Sidef::Types::Number::Number::MONE); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub xor { | ||
| 133 | my ($self, $str) = @_; | ||
| 134 | $self->new($$self ^ "$str"); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub or { | ||
| 138 | my ($self, $str) = @_; | ||
| 139 | $self->new($$self | "$str"); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub and { | ||
| 143 | my ($self, $str) = @_; | ||
| 144 | $self->new($$self & "$str"); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub not { | ||
| 148 | my ($self) = @_; | ||
| 149 | $self->new(~$$self); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub mul { | ||
| 153 | my ($self, $num) = @_; | ||
| 154 | $self->new($$self x $num); | ||
| 155 | } | ||
| 156 | |||
| 157 | *repeat = \&mul; | ||
| 158 | |||
| 159 | sub eq { | ||
| 160 | my ($self, $arg) = @_; | ||
| 161 | $$self eq $$arg | ||
| 162 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 163 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub ne { | ||
| 167 | my ($self, $arg) = @_; | ||
| 168 | $$self ne $$arg | ||
| 169 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 170 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub append { | ||
| 174 | my ($self, $string) = @_; | ||
| 175 | $self->new($$self . "$string"); | ||
| 176 | } | ||
| 177 | |||
| 178 | *add = \&append; | ||
| 179 | *concat = \&append; | ||
| 180 | |||
| 181 | sub prepend { | ||
| 182 | my ($self, $string) = @_; | ||
| 183 | $self->new("$string" . $$self); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub ucfirst { | ||
| 187 | my ($self) = @_; | ||
| 188 | $self->new(CORE::ucfirst $$self); | ||
| 189 | } | ||
| 190 | |||
| 191 | *tc = \&ucfirst; | ||
| 192 | *titlecase = \&ucfirst; | ||
| 193 | |||
| 194 | sub lc { | ||
| 195 | my ($self) = @_; | ||
| 196 | $self->new(CORE::lc $$self); | ||
| 197 | } | ||
| 198 | |||
| 199 | *downcase = \&lc; | ||
| 200 | *lower = \&lc; | ||
| 201 | *lowercase = \&lc; | ||
| 202 | |||
| 203 | sub uc { | ||
| 204 | my ($self) = @_; | ||
| 205 | $self->new(CORE::uc $$self); | ||
| 206 | } | ||
| 207 | |||
| 208 | *upcase = \&uc; | ||
| 209 | *upper = \&uc; | ||
| 210 | *uppercase = \&uc; | ||
| 211 | |||
| 212 | sub fc { | ||
| 213 | my ($self) = @_; | ||
| 214 | $self->new(CORE::fc $$self); | ||
| 215 | } | ||
| 216 | |||
| 217 | *foldcase = \&fc; | ||
| 218 | |||
| 219 | sub lcfirst { | ||
| 220 | my ($self) = @_; | ||
| 221 | $self->new(CORE::lcfirst $$self); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub first { | ||
| 225 | my ($self, $num) = @_; | ||
| 226 | bless \(my $t = CORE::substr($$self, 0, defined($num) ? CORE::int($num) : 1)); | ||
| 227 | } | ||
| 228 | |||
| 229 | *head = \&first; | ||
| 230 | |||
| 231 | sub last { | ||
| 232 | my ($self, $num) = @_; | ||
| 233 | bless \(my $t = CORE::substr($$self, defined($num) ? -(CORE::int($num) || return $self->new('')) : -1)); | ||
| 234 | } | ||
| 235 | |||
| 236 | *tail = \&last; | ||
| 237 | |||
| 238 | sub char { | ||
| 239 | my ($self, $pos) = @_; | ||
| 240 | bless \(my $t = CORE::substr($$self, CORE::int($pos), 1) // ''); | ||
| 241 | } | ||
| 242 | |||
| 243 | *char_at = \&char; | ||
| 244 | |||
| 245 | sub byte { | ||
| 246 | my ($self, $pos) = @_; | ||
| 247 | |||
| 248 | require bytes; | ||
| 249 | |||
| 250 | $pos = CORE::int($pos); | ||
| 251 | |||
| 252 | if ($pos >= bytes::length($$self)) { | ||
| 253 | return undef; | ||
| 254 | } | ||
| 255 | |||
| 256 | my $char = bytes::substr($$self, $pos, 1) // return undef; | ||
| 257 | Sidef::Types::Number::Number::_set_int(CORE::ord($char)); | ||
| 258 | } | ||
| 259 | |||
| 260 | *byte_at = \&byte; | ||
| 261 | |||
| 262 | sub wordcase { | ||
| 263 | my ($self) = @_; | ||
| 264 | |||
| 265 | my $str = $$self; | ||
| 266 | my $string = ''; | ||
| 267 | |||
| 268 | if ($str =~ /\G(\s+)/gc) { | ||
| 269 | $string = $1; | ||
| 270 | } | ||
| 271 | |||
| 272 | while ($str =~ /\G(\S++)(\s*+)/gc) { | ||
| 273 | $string .= CORE::ucfirst(CORE::lc($1)) . $2; | ||
| 274 | } | ||
| 275 | |||
| 276 | bless \$string; | ||
| 277 | } | ||
| 278 | |||
| 279 | *wc = \&wordcase; | ||
| 280 | |||
| 281 | sub capitalize { | ||
| 282 | my ($self) = @_; | ||
| 283 | $self->new(CORE::ucfirst(CORE::lc($$self))); | ||
| 284 | } | ||
| 285 | |||
| 286 | *tclc = \&capitalize; | ||
| 287 | |||
| 288 | sub swapcase { | ||
| 289 | my ($self) = @_; | ||
| 290 | (my $str = $$self) =~ s/(\p{Lu})|(\p{Ll})/defined($1) ? CORE::lc($1) : CORE::uc($2)/ge; | ||
| 291 | bless \$str; | ||
| 292 | } | ||
| 293 | |||
| 294 | *swap_case = \&swapcase; | ||
| 295 | |||
| 296 | sub common_prefix { | ||
| 297 | my ($self, $other) = @_; | ||
| 298 | my $s = $$self; | ||
| 299 | my $t = "$other"; | ||
| 300 | my $len = List::Util::min(CORE::length($s), CORE::length($t)); | ||
| 301 | my $i = 0; | ||
| 302 | ++$i while $i < $len && CORE::substr($s, $i, 1) eq CORE::substr($t, $i, 1); | ||
| 303 | $self->new(CORE::substr($s, 0, $i)); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub common_suffix { | ||
| 307 | my ($self, $other) = @_; | ||
| 308 | my $s = $$self; | ||
| 309 | my $t = "$other"; | ||
| 310 | my $ls = CORE::length($s); | ||
| 311 | my $lt = CORE::length($t); | ||
| 312 | my $len = List::Util::min($ls, $lt); | ||
| 313 | my $i = 0; | ||
| 314 | ++$i while $i < $len && CORE::substr($s, $ls - $i - 1, 1) eq CORE::substr($t, $lt - $i - 1, 1); | ||
| 315 | $i ? $self->new(CORE::substr($s, $ls - $i)) : $self->new(''); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub wrap { | ||
| 319 | my ($self, $width) = @_; | ||
| 320 | $width = defined($width) ? CORE::int($width) : 76; | ||
| 321 | $width = 1 if $width < 1; | ||
| 322 | |||
| 323 | my @lines; | ||
| 324 | my $line = ''; | ||
| 325 | |||
| 326 | foreach my $word (CORE::split(' ', $$self)) { | ||
| 327 | if ($line eq '') { | ||
| 328 | $line = $word; | ||
| 329 | } | ||
| 330 | elsif (CORE::length($line) + 1 + CORE::length($word) <= $width) { | ||
| 331 | $line .= ' ' . $word; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | CORE::push(@lines, bless \(my $l = $line)); | ||
| 335 | $line = $word; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | CORE::push(@lines, bless \(my $l = $line)) if $line ne ''; | ||
| 340 | |||
| 341 | Sidef::Types::Array::Array->new(\@lines); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub chop { | ||
| 345 | my ($self) = @_; | ||
| 346 | $self->new(CORE::substr($$self, 0, -1)); | ||
| 347 | } | ||
| 348 | |||
| 349 | sub pop { | ||
| 350 | my ($self) = @_; | ||
| 351 | $self->new(CORE::substr($$self, -1)); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub chomp { | ||
| 355 | my ($self) = @_; | ||
| 356 | |||
| 357 | if (substr($$self, -1) eq "\n") { | ||
| 358 | return $self->chop; | ||
| 359 | } | ||
| 360 | |||
| 361 | $self; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub crypt { | ||
| 365 | my ($self, $salt) = @_; | ||
| 366 | $self->new(crypt($$self, "$salt")); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub bin { | ||
| 370 | my ($self) = @_; | ||
| 371 | Sidef::Types::Number::Number->new($$self || '0', 2); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub oct { | ||
| 375 | my ($self) = @_; | ||
| 376 | Sidef::Types::Number::Number->new($$self || '0', 8); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub hex { | ||
| 380 | my ($self) = @_; | ||
| 381 | Sidef::Types::Number::Number->new($$self || '0', 16); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub hexlify { | ||
| 385 | my ($self) = @_; | ||
| 386 | my $r = CORE::unpack("H*", $$self); | ||
| 387 | bless \$r; | ||
| 388 | } | ||
| 389 | |||
| 390 | *ascii2hex = \&hexlify; | ||
| 391 | |||
| 392 | sub unhexlify { | ||
| 393 | my ($self) = @_; | ||
| 394 | my $r = CORE::pack("H*", $$self); | ||
| 395 | bless \$r; | ||
| 396 | } | ||
| 397 | |||
| 398 | *hex2ascii = \&unhexlify; | ||
| 399 | |||
| 400 | sub ascii2bin { | ||
| 401 | my ($self) = @_; | ||
| 402 | my $r = CORE::unpack("B*", $$self); | ||
| 403 | bless \$r; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub bin2ascii { | ||
| 407 | my ($self) = @_; | ||
| 408 | my $r = CORE::pack("B*", $$self); | ||
| 409 | bless \$r; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub ascii2bits { | ||
| 413 | my ($self) = @_; | ||
| 414 | Sidef::Types::Array::Array->new( | ||
| 415 | [map { $_ ? Sidef::Types::Number::Number::ONE : Sidef::Types::Number::Number::ZERO } CORE::split(//, scalar CORE::unpack("B*", $$self))]); | ||
| 416 | } | ||
| 417 | |||
| 418 | sub decode_base64 { | ||
| 419 | state $x = require MIME::Base64; | ||
| 420 | bless \(my $value = MIME::Base64::decode_base64(${$_[0]})); | ||
| 421 | } | ||
| 422 | |||
| 423 | *base64_decode = \&decode_base64; | ||
| 424 | |||
| 425 | sub encode_base64 { | ||
| 426 | state $x = require MIME::Base64; | ||
| 427 | bless \(my $value = MIME::Base64::encode_base64(${$_[0]})); | ||
| 428 | } | ||
| 429 | |||
| 430 | *base64_encode = \&encode_base64; | ||
| 431 | |||
| 432 | sub md5 { | ||
| 433 | state $x = require Digest::MD5; | ||
| 434 | bless \(my $value = Digest::MD5::md5_hex(${$_[0]})); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub sha1 { | ||
| 438 | state $x = require Digest::SHA; | ||
| 439 | bless \(my $value = Digest::SHA::sha1_hex(${$_[0]})); | ||
| 440 | } | ||
| 441 | |||
| 442 | sub sha256 { | ||
| 443 | state $x = require Digest::SHA; | ||
| 444 | bless \(my $value = Digest::SHA::sha256_hex(${$_[0]})); | ||
| 445 | } | ||
| 446 | |||
| 447 | sub sha512 { | ||
| 448 | state $x = require Digest::SHA; | ||
| 449 | bless \(my $value = Digest::SHA::sha512_hex(${$_[0]})); | ||
| 450 | } | ||
| 451 | |||
| 452 | sub parse_quotewords { | ||
| 453 | my ($self, $delim, $keep) = @_; | ||
| 454 | $delim //= ' '; | ||
| 455 | state $x = require Text::ParseWords; | ||
| 456 | my @words = map { bless \$_ } Text::ParseWords::parse_line("$delim", ($keep ? 1 : 0), $$self); | ||
| 457 | Sidef::Types::Array::Array->new(\@words); | ||
| 458 | } | ||
| 459 | |||
| 460 | sub extract_bracketed { | ||
| 461 | my ($self, $brackets) = @_; | ||
| 462 | state $x = require Text::Balanced; | ||
| 463 | my @results = Text::Balanced::extract_bracketed($$self, "$brackets"); | ||
| 464 | map { bless \$_ } @results; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub extract_delimited { | ||
| 468 | my ($self, $delim) = @_; | ||
| 469 | state $x = require Text::Balanced; | ||
| 470 | my @results = Text::Balanced::extract_delimited($$self, "$delim"); | ||
| 471 | map { bless \$_ } @results; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub extract_codeblock { | ||
| 475 | my ($self, $delim) = @_; | ||
| 476 | state $x = require Text::Balanced; | ||
| 477 | my @results = Text::Balanced::extract_codeblock($$self, "$delim"); | ||
| 478 | map { bless \$_ } @results; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub extract_quotelike { | ||
| 482 | my ($self) = @_; | ||
| 483 | state $x = require Text::Balanced; | ||
| 484 | my @results = Text::Balanced::extract_quotelike($$self); | ||
| 485 | map { bless \$_ } @results; | ||
| 486 | } | ||
| 487 | |||
| 488 | sub extract_tagged { | ||
| 489 | my ($self) = @_; | ||
| 490 | state $x = require Text::Balanced; | ||
| 491 | my @results = Text::Balanced::extract_tagged($$self); | ||
| 492 | map { bless \$_ } @results; | ||
| 493 | } | ||
| 494 | |||
| 495 | sub substr { | ||
| 496 | my ($self, $offs, $len) = @_; | ||
| 497 | bless \( | ||
| 498 | my $t = ( | ||
| 499 | defined($len) | ||
| 500 | ? CORE::substr($$self, CORE::int($offs), CORE::int($len)) | ||
| 501 | : CORE::substr($$self, CORE::int($offs)) | ||
| 502 | ) | ||
| 503 | ); | ||
| 504 | } | ||
| 505 | |||
| 506 | *substring = \&substr; | ||
| 507 | *slice = \&substr; | ||
| 508 | |||
| 509 | sub insert { | ||
| 510 | my ($self, $string, $pos, $len) = @_; | ||
| 511 | my $copy_str = "$$self"; | ||
| 512 | CORE::substr($copy_str, CORE::int($pos), (defined($len) ? CORE::int($len) : 0), "$string"); | ||
| 513 | bless \$copy_str; | ||
| 514 | } | ||
| 515 | |||
| 516 | sub join { | ||
| 517 | my ($self, @rest) = @_; | ||
| 518 | bless \(my $value = CORE::join($$self, @rest)); | ||
| 519 | } | ||
| 520 | |||
| 521 | sub clear { | ||
| 522 | state $x = bless \(my $str = ''); | ||
| 523 | } | ||
| 524 | |||
| 525 | sub is_empty { | ||
| 526 | my ($self) = @_; | ||
| 527 | ($$self eq '') | ||
| 528 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 529 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 530 | } | ||
| 531 | |||
| 532 | sub is_lowercase { | ||
| 533 | my ($self) = @_; | ||
| 534 | ($$self =~ /^[[:lower:]]+\z/) | ||
| 535 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 536 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 537 | } | ||
| 538 | |||
| 539 | *is_lc = \&is_lowercase; | ||
| 540 | |||
| 541 | sub is_uppercase { | ||
| 542 | my ($self) = @_; | ||
| 543 | ($$self =~ /^[[:upper:]]+\z/) | ||
| 544 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 545 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 546 | } | ||
| 547 | |||
| 548 | *is_uc = \&is_uppercase; | ||
| 549 | |||
| 550 | sub is_ascii { | ||
| 551 | my ($self) = @_; | ||
| 552 | ($$self =~ /^[[:ascii:]]+\z/) | ||
| 553 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 554 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub is_space { | ||
| 558 | my ($self) = @_; | ||
| 559 | ($$self =~ /^[[:space:]]+\z/) | ||
| 560 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 561 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub is_word { | ||
| 565 | my ($self) = @_; | ||
| 566 | ($$self =~ /^[[:word:]]+\z/) | ||
| 567 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 568 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 569 | } | ||
| 570 | |||
| 571 | sub is_punctuation { | ||
| 572 | my ($self) = @_; | ||
| 573 | ($$self =~ /^[[:punct:]]+\z/) | ||
| 574 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 575 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 576 | } | ||
| 577 | |||
| 578 | *is_punct = \&is_punctuation; | ||
| 579 | |||
| 580 | sub is_digit { | ||
| 581 | my ($self) = @_; | ||
| 582 | ($$self =~ /^[[:digit:]]+\z/) | ||
| 583 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 584 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 585 | } | ||
| 586 | |||
| 587 | sub is_alpha { | ||
| 588 | my ($self) = @_; | ||
| 589 | ($$self =~ /^[[:alpha:]]+\z/) | ||
| 590 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 591 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 592 | } | ||
| 593 | |||
| 594 | sub is_alphanum { | ||
| 595 | my ($self) = @_; | ||
| 596 | ($$self =~ /^[[:alnum:]]+\z/) | ||
| 597 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 598 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 599 | } | ||
| 600 | |||
| 601 | *is_alnum = \&is_alphanum; | ||
| 602 | |||
| 603 | sub is_blank { | ||
| 604 | my ($self) = @_; | ||
| 605 | ($$self =~ /^[[:blank:]]+\z/) | ||
| 606 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 607 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub is_graph { | ||
| 611 | my ($self) = @_; | ||
| 612 | ($$self =~ /^[[:graph:]]+\z/) | ||
| 613 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 614 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 615 | } | ||
| 616 | |||
| 617 | sub is_control { | ||
| 618 | my ($self) = @_; | ||
| 619 | ($$self =~ /^[[:cntrl:]]+\z/) | ||
| 620 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 621 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 622 | } | ||
| 623 | |||
| 624 | sub is_printable { | ||
| 625 | my ($self) = @_; | ||
| 626 | ($$self =~ /^[[:print:]]+\z/) | ||
| 627 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 628 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 629 | } | ||
| 630 | |||
| 631 | sub is_xdigit { | ||
| 632 | my ($self) = @_; | ||
| 633 | ($$self =~ /^[[:xdigit:]]+\z/) | ||
| 634 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 635 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 636 | } | ||
| 637 | |||
| 638 | sub index { | ||
| 639 | my ($self, $substr, $pos) = @_; | ||
| 640 | Sidef::Types::Number::Number::_set_int( | ||
| 641 | defined($pos) | ||
| 642 | ? CORE::index($$self, "$substr", CORE::int($pos)) | ||
| 643 | : CORE::index($$self, "$substr") | ||
| 644 | ); | ||
| 645 | } | ||
| 646 | |||
| 647 | sub rindex { | ||
| 648 | my ($self, $substr, $pos) = @_; | ||
| 649 | Sidef::Types::Number::Number::_set_int( | ||
| 650 | defined($pos) | ||
| 651 | ? CORE::rindex($$self, "$substr", CORE::int($pos)) | ||
| 652 | : CORE::rindex($$self, "$substr") | ||
| 653 | ); | ||
| 654 | } | ||
| 655 | |||
| 656 | sub ord { | ||
| 657 | my ($self) = @_; | ||
| 658 | $$self eq '' | ||
| 659 | ? undef | ||
| 660 | : Sidef::Types::Number::Number::_set_int(CORE::ord($$self)); | ||
| 661 | } | ||
| 662 | |||
| 663 | sub reverse { | ||
| 664 | my ($self) = @_; | ||
| 665 | $self->new(scalar CORE::reverse($$self)); | ||
| 666 | } | ||
| 667 | |||
| 668 | *flip = \&reverse; | ||
| 669 | |||
| 670 | sub printf { | ||
| 671 | my ($self, @arguments) = @_; | ||
| 672 | printf($$self, @arguments) | ||
| 673 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 674 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 675 | } | ||
| 676 | |||
| 677 | sub printlnf { | ||
| 678 | my ($self, @arguments) = @_; | ||
| 679 | printf($$self . "\n", @arguments) | ||
| 680 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 681 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 682 | } | ||
| 683 | |||
| 684 | *sayf = \&printlnf; | ||
| 685 | |||
| 686 | sub sprintf { | ||
| 687 | my ($self, @arguments) = @_; | ||
| 688 | $self->new(CORE::sprintf $$self, @arguments); | ||
| 689 | } | ||
| 690 | |||
| 691 | sub sprintlnf { | ||
| 692 | my ($self, @arguments) = @_; | ||
| 693 | $self->new(CORE::sprintf($$self . "\n", @arguments)); | ||
| 694 | } | ||
| 695 | |||
| 696 | sub _string_or_regex { | ||
| 697 | my ($obj) = @_; | ||
| 698 | |||
| 699 | if (ref($obj) eq 'Sidef::Types::Regex::Regex') { | ||
| 700 | return $obj->{regex}; | ||
| 701 | } | ||
| 702 | |||
| 703 | CORE::quotemeta("$obj"); | ||
| 704 | } | ||
| 705 | |||
| 706 | sub sub { | ||
| 707 | my ($self, $regex, $str) = @_; | ||
| 708 | |||
| 709 | ref($str) eq 'Sidef::Types::Block::Block' | ||
| 710 | && return $self->esub($regex, $str); | ||
| 711 | |||
| 712 | my $search = _string_or_regex($regex); | ||
| 713 | my $value = "$str"; | ||
| 714 | |||
| 715 | $self->new($$self =~ s{$search}{$value}r); | ||
| 716 | } | ||
| 717 | |||
| 718 | *replace = \⊂ | ||
| 719 | |||
| 720 | sub gsub { | ||
| 721 | my ($self, $regex, $str) = @_; | ||
| 722 | |||
| 723 | ref($str) eq 'Sidef::Types::Block::Block' | ||
| 724 | && return $self->gesub($regex, $str); | ||
| 725 | |||
| 726 | my $search = _string_or_regex($regex); | ||
| 727 | my $value = "$str"; | ||
| 728 | $self->new($$self =~ s{$search}{$value}gr); | ||
| 729 | } | ||
| 730 | |||
| 731 | *replace_all = \&gsub; | ||
| 732 | |||
| 733 | sub _get_captures { | ||
| 734 | my ($string) = @_; | ||
| 735 | map { bless \(my $t = CORE::substr($string, $-[$_], $+[$_] - $-[$_])) } 1 .. $#{-}; | ||
| 736 | } | ||
| 737 | |||
| 738 | sub esub { | ||
| 739 | my ($self, $regex, $block) = @_; | ||
| 740 | |||
| 741 | my $search = _string_or_regex($regex); | ||
| 742 | |||
| 743 | if (ref($block) eq 'Sidef::Types::Block::Block') { | ||
| 744 | return $self->new($$self =~ s{$search}{$block->run(_get_captures($$self))}er); | ||
| 745 | } | ||
| 746 | |||
| 747 | $self->new($$self =~ s{$search}{"$block"}eer); | ||
| 748 | } | ||
| 749 | |||
| 750 | sub gesub { | ||
| 751 | my ($self, $regex, $block) = @_; | ||
| 752 | |||
| 753 | my $search = _string_or_regex($regex); | ||
| 754 | |||
| 755 | if (ref($block) eq 'Sidef::Types::Block::Block') { | ||
| 756 | my $value = $$self; | ||
| 757 | return $self->new($value =~ s{$search}{$block->run(_get_captures($value))}ger); | ||
| 758 | } | ||
| 759 | |||
| 760 | my $value = "$block"; | ||
| 761 | $self->new($$self =~ s{$search}{$value}geer); | ||
| 762 | } | ||
| 763 | |||
| 764 | sub glob { | ||
| 765 | my ($self) = @_; | ||
| 766 | state $x = require Encode; | ||
| 767 | Sidef::Types::Array::Array->new([map { bless \(my $value = Encode::decode_utf8($_)) } CORE::glob($$self)]); | ||
| 768 | } | ||
| 769 | |||
| 770 | sub quotemeta { | ||
| 771 | my ($self) = @_; | ||
| 772 | $self->new(CORE::quotemeta($$self)); | ||
| 773 | } | ||
| 774 | |||
| 775 | *escape = \"emeta; | ||
| 776 | |||
| 777 | sub scan { | ||
| 778 | my ($self, $regex) = @_; | ||
| 779 | my $str = $$self; | ||
| 780 | Sidef::Types::Array::Array->new([map { bless \$_ } $str =~ /$regex->{regex}/g]); | ||
| 781 | } | ||
| 782 | |||
| 783 | sub collect { | ||
| 784 | my ($self, $regex) = @_; | ||
| 785 | my $str = $$self; | ||
| 786 | my @matches; | ||
| 787 | while ($str =~ /$regex->{regex}/g) { | ||
| 788 | push @matches, Sidef::Types::Array::Array->new([_get_captures($str)]); | ||
| 789 | } | ||
| 790 | Sidef::Types::Array::Array->new(\@matches); | ||
| 791 | } | ||
| 792 | |||
| 793 | *findall = \&collect; | ||
| 794 | *find_all = \&collect; | ||
| 795 | |||
| 796 | sub slices { | ||
| 797 | my ($self, $n) = @_; | ||
| 798 | $n = CORE::int($n); | ||
| 799 | $n > 0 or return Sidef::Types::Array::Array->new; | ||
| 800 | Sidef::Types::Array::Array->new([map { bless \$_ } unpack('(a' . $n . ')*', $$self)]); | ||
| 801 | } | ||
| 802 | |||
| 803 | sub each_slice { | ||
| 804 | my ($self, $n, $block) = @_; | ||
| 805 | |||
| 806 | $n = CORE::int($n); | ||
| 807 | $n > 0 or return $self; | ||
| 808 | |||
| 809 | my $len = length($$self); | ||
| 810 | for (my $i = 0 ; $i < $len ; $i += $n) { | ||
| 811 | $block->run(bless \(my $str = CORE::substr($$self, $i, $n))); | ||
| 812 | } | ||
| 813 | |||
| 814 | $self; | ||
| 815 | } | ||
| 816 | |||
| 817 | sub cons { | ||
| 818 | my ($self, $n) = @_; | ||
| 819 | $n = CORE::int($n); | ||
| 820 | $n > 0 or return Sidef::Types::Array::Array->new; | ||
| 821 | my @strings; | ||
| 822 | foreach my $i (0 .. CORE::length($$self) - $n) { | ||
| 823 | push @strings, bless \(my $str = CORE::substr($$self, $i, $n)); | ||
| 824 | } | ||
| 825 | Sidef::Types::Array::Array->new(\@strings); | ||
| 826 | } | ||
| 827 | |||
| 828 | sub each_cons { | ||
| 829 | my ($self, $n, $block) = @_; | ||
| 830 | |||
| 831 | $n = CORE::int($n); | ||
| 832 | $n > 0 or return $self; | ||
| 833 | |||
| 834 | foreach my $i (0 .. CORE::length($$self) - $n) { | ||
| 835 | $block->run(bless \(my $str = CORE::substr($$self, $i, $n))); | ||
| 836 | } | ||
| 837 | |||
| 838 | $self; | ||
| 839 | } | ||
| 840 | |||
| 841 | sub split { | ||
| 842 | my ($self, $sep, $size) = @_; | ||
| 843 | |||
| 844 | $size = defined($size) ? CORE::int($size) : 0; | ||
| 845 | |||
| 846 | if (!defined($sep)) { | ||
| 847 | return Sidef::Types::Array::Array->new([map { bless \$_ } split(' ', $$self, $size)]); | ||
| 848 | } | ||
| 849 | |||
| 850 | if (ref($sep) eq 'Sidef::Types::Number::Number') { | ||
| 851 | return $self->slices($sep); | ||
| 852 | } | ||
| 853 | |||
| 854 | $sep = _string_or_regex($sep); | ||
| 855 | Sidef::Types::Array::Array->new([map { bless \$_ } split(/$sep/, $$self, $size)]); | ||
| 856 | } | ||
| 857 | |||
| 858 | sub sort { | ||
| 859 | my ($self, $block) = @_; | ||
| 860 | |||
| 861 | if (defined $block) { | ||
| 862 | return $self->chars->sort($block)->join; | ||
| 863 | } | ||
| 864 | |||
| 865 | $self->new(CORE::join('', sort(CORE::split(//, $$self)))); | ||
| 866 | } | ||
| 867 | |||
| 868 | sub unique { | ||
| 869 | my ($self) = @_; | ||
| 870 | $self->new(CORE::join('', List::Util::uniq(CORE::split(//, $$self)))); | ||
| 871 | } | ||
| 872 | |||
| 873 | *uniq = \&unique; | ||
| 874 | *distinct = \&unique; | ||
| 875 | |||
| 876 | sub format { | ||
| 877 | my ($self) = @_; | ||
| 878 | CORE::chomp(my $text = 'format __MY_FORMAT__ = ' . "\n" . $$self); | ||
| 879 | eval($text . "\n."); | ||
| 880 | |||
| 881 | open my $str_h, '>', \my $acc; | ||
| 882 | my $old_h = select($str_h); | ||
| 883 | local $~ = '__MY_FORMAT__'; | ||
| 884 | write; | ||
| 885 | select($old_h); | ||
| 886 | close $str_h; | ||
| 887 | |||
| 888 | bless \$acc; | ||
| 889 | } | ||
| 890 | |||
| 891 | sub words { | ||
| 892 | my ($self) = @_; | ||
| 893 | Sidef::Types::Array::Array->new([map { bless \$_ } CORE::split(' ', $$self)]); | ||
| 894 | } | ||
| 895 | |||
| 896 | sub each_word { | ||
| 897 | my ($self, $block) = @_; | ||
| 898 | |||
| 899 | foreach my $word (CORE::split(' ', $$self)) { | ||
| 900 | $block->run(bless \$word); | ||
| 901 | } | ||
| 902 | |||
| 903 | $self; | ||
| 904 | } | ||
| 905 | |||
| 906 | sub numbers { | ||
| 907 | my ($self) = @_; | ||
| 908 | Sidef::Types::Array::Array->new( | ||
| 909 | [map { Sidef::Types::Number::Number->new($_) } | ||
| 910 | grep { Scalar::Util::looks_like_number($_) } CORE::split(' ', $$self) | ||
| 911 | ] | ||
| 912 | ); | ||
| 913 | } | ||
| 914 | |||
| 915 | *nums = \&numbers; | ||
| 916 | |||
| 917 | sub integers { | ||
| 918 | my ($self) = @_; | ||
| 919 | Sidef::Types::Array::Array->new( | ||
| 920 | [map { Sidef::Types::Number::Number::_set_int($_) } | ||
| 921 | grep { /^-?[0-9]+\z/ } CORE::split(' ', $$self) | ||
| 922 | ] | ||
| 923 | ); | ||
| 924 | } | ||
| 925 | |||
| 926 | *ints = \ℤ | ||
| 927 | |||
| 928 | sub digits { | ||
| 929 | my ($self) = @_; | ||
| 930 | Sidef::Types::Array::Array->new([map { Sidef::Types::Number::Number::_set_int($_) } grep { /^[0-9]\z/ } CORE::split(//, $$self)]); | ||
| 931 | } | ||
| 932 | |||
| 933 | sub each_number { | ||
| 934 | my ($self, $block) = @_; | ||
| 935 | |||
| 936 | foreach my $num (CORE::split(' ', $$self)) { | ||
| 937 | $block->run(Sidef::Types::Number::Number->new($num)); | ||
| 938 | } | ||
| 939 | |||
| 940 | $self; | ||
| 941 | } | ||
| 942 | |||
| 943 | *each_num = \&each_number; | ||
| 944 | |||
| 945 | sub bytes { | ||
| 946 | my ($self) = @_; | ||
| 947 | my $string = $$self; | ||
| 948 | require bytes; | ||
| 949 | Sidef::Types::Array::Array->new([map { Sidef::Types::Number::Number::_set_int(CORE::ord(bytes::substr($string, $_, 1))) } 0 .. bytes::length($string) - 1]); | ||
| 950 | } | ||
| 951 | |||
| 952 | sub each_byte { | ||
| 953 | my ($self, $block) = @_; | ||
| 954 | |||
| 955 | my $string = $$self; | ||
| 956 | |||
| 957 | require bytes; | ||
| 958 | foreach my $i (0 .. bytes::length($string) - 1) { | ||
| 959 | $block->run(Sidef::Types::Number::Number::_set_int(CORE::ord bytes::substr($string, $i, 1))); | ||
| 960 | } | ||
| 961 | |||
| 962 | $self; | ||
| 963 | } | ||
| 964 | |||
| 965 | sub chars { | ||
| 966 | my ($self) = @_; | ||
| 967 | Sidef::Types::Array::Array->new([map { bless \$_ } CORE::split(//, $$self)]); | ||
| 968 | } | ||
| 969 | |||
| 970 | sub code_points { | ||
| 971 | my ($self) = @_; | ||
| 972 | Sidef::Types::Array::Array->new([map { Sidef::Types::Number::Number::_set_int($_) } unpack('C*', $$self)]); | ||
| 973 | } | ||
| 974 | |||
| 975 | *codes = \&code_points; | ||
| 976 | |||
| 977 | sub iter { | ||
| 978 | my ($self) = @_; | ||
| 979 | |||
| 980 | my @chars = split(//, $$self); | ||
| 981 | |||
| 982 | my $i = -1; | ||
| 983 | my $end = $#chars; | ||
| 984 | |||
| 985 | Sidef::Types::Block::Block->new( | ||
| 986 | code => sub { | ||
| 987 | ++$i <= $end or return undef; | ||
| 988 | bless \(my $chr = $chars[$i]); | ||
| 989 | } | ||
| 990 | ); | ||
| 991 | } | ||
| 992 | |||
| 993 | sub each_char { | ||
| 994 | my ($self, $block) = @_; | ||
| 995 | |||
| 996 | foreach my $char (CORE::split(//, $$self)) { | ||
| 997 | $block->run(bless \$char); | ||
| 998 | } | ||
| 999 | |||
| 1000 | $self; | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | *each = \&each_char; | ||
| 1004 | |||
| 1005 | sub each_kv { | ||
| 1006 | my ($self, $block) = @_; | ||
| 1007 | |||
| 1008 | my @chars = CORE::split(//, $$self); | ||
| 1009 | |||
| 1010 | foreach my $i (0 .. $#chars) { | ||
| 1011 | my $char = $chars[$i]; | ||
| 1012 | $block->run(Sidef::Types::Number::Number::_set_int($i), (bless \$char)); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | $self; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub graphemes { | ||
| 1019 | my ($self) = @_; | ||
| 1020 | Sidef::Types::Array::Array->new([map { bless \$_ } $$self =~ /(\X)/g]); | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | *graphs = \&graphemes; | ||
| 1024 | |||
| 1025 | sub each_grapheme { | ||
| 1026 | my ($self, $block) = @_; | ||
| 1027 | |||
| 1028 | my $str = $$self; | ||
| 1029 | while ($str =~ /(\X)/g) { | ||
| 1030 | $block->run(bless \(my $str = $1)); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | $self; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | *each_graph = \&each_grapheme; | ||
| 1037 | |||
| 1038 | sub lines { | ||
| 1039 | my ($self) = @_; | ||
| 1040 | Sidef::Types::Array::Array->new([map { bless \$_ } CORE::split(/\R/, $$self)]); | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | sub each_line { | ||
| 1044 | my ($self, $block) = @_; | ||
| 1045 | |||
| 1046 | foreach my $line (CORE::split(/\R/, $$self)) { | ||
| 1047 | $block->run(bless \$line); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | $self; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | sub pipe { | ||
| 1054 | my ($self) = @_; | ||
| 1055 | Sidef::Types::Glob::Pipe->new($$self); | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | sub backtick { | ||
| 1059 | my ($self) = @_; | ||
| 1060 | Sidef::Types::Glob::Backtick->new($$self); | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | sub open_r { | ||
| 1064 | my ($self, $mode) = @_; | ||
| 1065 | $mode //= 'utf8'; | ||
| 1066 | my $str = $$self; | ||
| 1067 | open(my $fh, "<:$mode", \$str) or return undef; | ||
| 1068 | Sidef::Types::Glob::FileHandle->new($fh); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | sub center { | ||
| 1072 | my ($self, $size, $char) = @_; | ||
| 1073 | |||
| 1074 | my $string = $$self; | ||
| 1075 | |||
| 1076 | $size = defined($size) ? CORE::int($size) : 0; | ||
| 1077 | $char = defined($char) ? "$char" : ' '; | ||
| 1078 | |||
| 1079 | if (CORE::length($char) > 1) { | ||
| 1080 | $char = CORE::substr($char, 0, 1); | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | my $len = CORE::length($string); | ||
| 1084 | |||
| 1085 | $size <= $len | ||
| 1086 | && return $self; | ||
| 1087 | |||
| 1088 | my $padlen = $size - $len; | ||
| 1089 | my $lpad = $padlen >> 1; | ||
| 1090 | my $rpad = $padlen - $lpad; | ||
| 1091 | |||
| 1092 | $self->new(($char x $lpad) . $string . ($char x $rpad)); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | sub pad_left { | ||
| 1096 | my ($self, $size, $char) = @_; | ||
| 1097 | |||
| 1098 | my $string = $$self; | ||
| 1099 | |||
| 1100 | $size = defined($size) ? CORE::int($size) : 0; | ||
| 1101 | $char = defined($char) ? "$char" : ' '; | ||
| 1102 | |||
| 1103 | if (CORE::length($char) > 1) { | ||
| 1104 | $char = CORE::substr($char, 0, 1); | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | my $len = CORE::length($string); | ||
| 1108 | |||
| 1109 | $size <= $len | ||
| 1110 | && return $self; | ||
| 1111 | |||
| 1112 | $self->new($string . ($char x ($size - $len))); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | *ljust = \&pad_left; | ||
| 1116 | |||
| 1117 | sub pad_right { | ||
| 1118 | my ($self, $size, $char) = @_; | ||
| 1119 | |||
| 1120 | my $string = $$self; | ||
| 1121 | |||
| 1122 | $size = defined($size) ? CORE::int($size) : 0; | ||
| 1123 | $char = defined($char) ? "$char" : ' '; | ||
| 1124 | |||
| 1125 | if (CORE::length($char) > 1) { | ||
| 1126 | $char = CORE::substr($char, 0, 1); | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | my $len = CORE::length($string); | ||
| 1130 | |||
| 1131 | $size <= $len | ||
| 1132 | && return $self; | ||
| 1133 | |||
| 1134 | $self->new(($char x ($size - $len)) . $string); | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | *rjust = \&pad_right; | ||
| 1138 | |||
| 1139 | sub trim { | ||
| 1140 | my ($self, $arg) = @_; | ||
| 1141 | |||
| 1142 | if (defined($arg)) { | ||
| 1143 | |||
| 1144 | my $regex; | ||
| 1145 | |||
| 1146 | if (ref($arg) eq 'Sidef::Types::Regex::Regex') { | ||
| 1147 | $regex = qr/(?:$arg->{regex})/; | ||
| 1148 | } | ||
| 1149 | else { | ||
| 1150 | my @chars = split('', "$arg"); | ||
| 1151 | $regex = qr/(?:[@chars]+)/; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | return $self->new(($$self =~ s/^$regex//r) =~ s/$regex\z//r); | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | $self->new(unpack('A*', $$self) =~ s/^\s+//r); | ||
| 1158 | } | ||
| 1159 | |||
| 1160 | *strip = \&trim; | ||
| 1161 | |||
| 1162 | sub strip_beg { | ||
| 1163 | my ($self, $arg) = @_; | ||
| 1164 | |||
| 1165 | if (defined($arg)) { | ||
| 1166 | |||
| 1167 | my $regex; | ||
| 1168 | |||
| 1169 | if (ref($arg) eq 'Sidef::Types::Regex::Regex') { | ||
| 1170 | $regex = qr/^(?:$arg->{regex})/; | ||
| 1171 | } | ||
| 1172 | else { | ||
| 1173 | my @chars = split('', "$arg"); | ||
| 1174 | $regex = qr/^(?:[@chars]+)/; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | return $self->new($$self =~ s/$regex//r); | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | $self->new($$self =~ s/^\s+//r); | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | *lstrip = \&strip_beg; | ||
| 1184 | *ltrim = \&strip_beg; | ||
| 1185 | *trim_beg = \&strip_beg; | ||
| 1186 | *strip_left = \&strip_beg; | ||
| 1187 | *trim_left = \&strip_beg; | ||
| 1188 | |||
| 1189 | sub strip_end { | ||
| 1190 | my ($self, $arg) = @_; | ||
| 1191 | |||
| 1192 | if (defined($arg)) { | ||
| 1193 | |||
| 1194 | my $regex; | ||
| 1195 | |||
| 1196 | if (ref($arg) eq 'Sidef::Types::Regex::Regex') { | ||
| 1197 | $regex = qr/(?:$arg->{regex})\z/; | ||
| 1198 | } | ||
| 1199 | else { | ||
| 1200 | my @chars = split('', "$arg"); | ||
| 1201 | $regex = qr/(?:[@chars]+)\z/; | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | return $self->new($$self =~ s/$regex//r); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | $self->new(unpack('A*', $$self)); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | *rstrip = \&strip_end; | ||
| 1211 | *rtrim = \&strip_end; | ||
| 1212 | *trim_end = \&strip_end; | ||
| 1213 | *strip_right = \&strip_end; | ||
| 1214 | *trim_right = \&strip_end; | ||
| 1215 | |||
| 1216 | sub trans { | ||
| 1217 | my ($self, $orig, $repl) = @_; | ||
| 1218 | |||
| 1219 | my %map; | ||
| 1220 | if (!defined($repl) and defined($orig)) { # assume an array of pairs | ||
| 1221 | foreach my $pair (@{$orig}) { | ||
| 1222 | $map{"$pair->[0]"} = "$pair->[1]"; | ||
| 1223 | } | ||
| 1224 | } | ||
| 1225 | else { | ||
| 1226 | @map{@{$orig}} = @{$repl}; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | my $tries = ( | ||
| 1230 | CORE::join('|', map { CORE::quotemeta($_) } | ||
| 1231 | sort { length($b) <=> length($a) } CORE::keys(%map)) | ||
| 1232 | ); | ||
| 1233 | |||
| 1234 | $self->new($$self =~ s{($tries)}{$map{$1}}gr); | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | sub translit { | ||
| 1238 | my ($self, $orig, $repl, $modes) = @_; | ||
| 1239 | |||
| 1240 | UNIVERSAL::isa($orig, 'ARRAY') && return $self->trans($orig, $repl); | ||
| 1241 | $self->new( | ||
| 1242 | eval qq{"\Q${\$$self}\E"=~tr/} | ||
| 1243 | . "$orig" =~ s{([/\\])}{\\$1}gr . "/" | ||
| 1244 | . "$repl" =~ s{([/\\])}{\\$1}gr . "/r" | ||
| 1245 | . ( | ||
| 1246 | defined($modes) | ||
| 1247 | ? "$modes" | ||
| 1248 | : '' | ||
| 1249 | ) | ||
| 1250 | ); | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | *tr = \&translit; | ||
| 1254 | |||
| 1255 | sub unpack { | ||
| 1256 | my ($self, $arg) = @_; | ||
| 1257 | my @values = map { bless \$_ } CORE::unpack($$self, "$arg"); | ||
| 1258 | @values > 1 ? @values : $values[0]; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | sub pack { | ||
| 1262 | my ($self, @list) = @_; | ||
| 1263 | $self->new(CORE::pack($$self, @list)); | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | sub chars_length { | ||
| 1267 | Sidef::Types::Number::Number::_set_int(CORE::length(${$_[0]})); | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | *len = \&chars_length; | ||
| 1271 | *length = \&chars_length; | ||
| 1272 | *chars_len = \&chars_length; | ||
| 1273 | *size = \&chars_length; | ||
| 1274 | |||
| 1275 | sub graphs_length { | ||
| 1276 | Sidef::Types::Number::Number::_set_int(scalar(() = ${$_[0]} =~ /\X/g)); | ||
| 1277 | } | ||
| 1278 | |||
| 1279 | *graphs_len = \&graphs_length; | ||
| 1280 | |||
| 1281 | sub bytes_length { | ||
| 1282 | my ($self) = @_; | ||
| 1283 | |||
| 1284 | require bytes; | ||
| 1285 | Sidef::Types::Number::Number::_set_int(bytes::length($$self)); | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | *bytes_len = \&bytes_length; | ||
| 1289 | |||
| 1290 | sub levenshtein { | ||
| 1291 | my ($self, $arg) = @_; | ||
| 1292 | |||
| 1293 | my @s = split(//, $$self); | ||
| 1294 | my @t = split(//, "$arg"); | ||
| 1295 | |||
| 1296 | my $len1 = scalar(@s); | ||
| 1297 | my $len2 = scalar(@t); | ||
| 1298 | |||
| 1299 | my @d = ([0 .. $len2], map { [$_] } 1 .. $len1); | ||
| 1300 | foreach my $i (1 .. $len1) { | ||
| 1301 | foreach my $j (1 .. $len2) { | ||
| 1302 | $d[$i][$j] = | ||
| 1303 | $s[$i - 1] eq $t[$j - 1] | ||
| 1304 | ? $d[$i - 1][$j - 1] | ||
| 1305 | : List::Util::min($d[$i - 1][$j], $d[$i][$j - 1], $d[$i - 1][$j - 1]) + 1; | ||
| 1306 | } | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | Sidef::Types::Number::Number::_set_int($d[-1][-1]); | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | *lev = \&levenshtein; | ||
| 1313 | *leven = \&levenshtein; | ||
| 1314 | |||
| 1315 | sub jaro { | ||
| 1316 | my ($s, $t, $winkler) = @_; | ||
| 1317 | |||
| 1318 | $s = $$s; | ||
| 1319 | $t = "$t"; | ||
| 1320 | |||
| 1321 | my $s_len = length($s); | ||
| 1322 | my $t_len = length($t); | ||
| 1323 | |||
| 1324 | if ($s_len == 0 and $t_len == 0) { | ||
| 1325 | return 1; | ||
| 1326 | } | ||
| 1327 | |||
| 1328 | my $match_distance = int(List::Util::max($s_len, $t_len) / 2) - 1; | ||
| 1329 | |||
| 1330 | my @s_matches; | ||
| 1331 | my @t_matches; | ||
| 1332 | |||
| 1333 | my @s = split(//, $s); | ||
| 1334 | my @t = split(//, $t); | ||
| 1335 | |||
| 1336 | my $matches = 0; | ||
| 1337 | foreach my $i (0 .. $#s) { | ||
| 1338 | |||
| 1339 | my $start = List::Util::max(0, $i - $match_distance); | ||
| 1340 | my $end = List::Util::min($i + $match_distance + 1, $t_len); | ||
| 1341 | |||
| 1342 | foreach my $j ($start .. $end - 1) { | ||
| 1343 | $t_matches[$j] and next; | ||
| 1344 | $s[$i] eq $t[$j] or next; | ||
| 1345 | $s_matches[$i] = 1; | ||
| 1346 | $t_matches[$j] = 1; | ||
| 1347 | $matches++; | ||
| 1348 | last; | ||
| 1349 | } | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | return Sidef::Types::Number::Number::ZERO if $matches == 0; | ||
| 1353 | |||
| 1354 | my $k = 0; | ||
| 1355 | my $transpositions = 0; | ||
| 1356 | |||
| 1357 | foreach my $i (0 .. $#s) { | ||
| 1358 | $s_matches[$i] or next; | ||
| 1359 | until ($t_matches[$k]) { ++$k } | ||
| 1360 | $s[$i] eq $t[$k] or ++$transpositions; | ||
| 1361 | ++$k; | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | my $jaro = (($matches / $s_len) + ($matches / $t_len) + (($matches - $transpositions / 2) / $matches)) / 3; | ||
| 1365 | |||
| 1366 | $winkler || return Sidef::Types::Number::Number->new($jaro); # return the Jaro similarity instead of Jaro-Winkler | ||
| 1367 | |||
| 1368 | my $prefix = 0; | ||
| 1369 | foreach my $i (0 .. List::Util::min(3, $#t, $#s)) { | ||
| 1370 | $s[$i] eq $t[$i] ? ++$prefix : last; | ||
| 1371 | } | ||
| 1372 | |||
| 1373 | Sidef::Types::Number::Number->new($jaro + $prefix * 0.1 * (1 - $jaro)); | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | sub contains { | ||
| 1377 | my ($self, $arg, $start_pos) = @_; | ||
| 1378 | |||
| 1379 | $start_pos = defined($start_pos) ? CORE::int($start_pos) : 0; | ||
| 1380 | |||
| 1381 | if (ref($arg) eq 'Sidef::Types::Regex::Regex') { | ||
| 1382 | my $regex = $arg->{regex}; | ||
| 1383 | my $s = $$self; | ||
| 1384 | pos($s) = $start_pos; | ||
| 1385 | return ( | ||
| 1386 | (scalar $s =~ /$regex/g) | ||
| 1387 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1388 | : (Sidef::Types::Bool::Bool::FALSE) | ||
| 1389 | ); | ||
| 1390 | } | ||
| 1391 | |||
| 1392 | if ($start_pos < 0) { | ||
| 1393 | $start_pos = CORE::length($$self) + $start_pos; | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | CORE::index($$self, "$arg", $start_pos) != -1 | ||
| 1397 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1398 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | *has = \&contains; | ||
| 1402 | *contain = \&contains; | ||
| 1403 | *include = \&contains; | ||
| 1404 | *includes = \&contains; | ||
| 1405 | |||
| 1406 | sub rotate { | ||
| 1407 | my ($self, $n) = @_; | ||
| 1408 | $n = (CORE::int($n) % (CORE::length($$self) || return $self)) || return $self; | ||
| 1409 | $self->new(CORE::substr($$self, $n) . CORE::substr($$self, 0, $n)); | ||
| 1410 | } | ||
| 1411 | |||
| 1412 | sub count { | ||
| 1413 | my ($self, $arg) = @_; | ||
| 1414 | |||
| 1415 | my $s = $$self; | ||
| 1416 | my $counter = 0; | ||
| 1417 | |||
| 1418 | if (ref($arg) eq 'Sidef::Types::Regex::Regex') { | ||
| 1419 | my $regex = $arg->{regex}; | ||
| 1420 | ++$counter while $s =~ /$regex/g; | ||
| 1421 | return Sidef::Types::Number::Number::_set_int($counter); | ||
| 1422 | } | ||
| 1423 | elsif (ref($arg) eq 'Sidef::Types::Block::Block') { | ||
| 1424 | foreach my $char (split //, $s) { | ||
| 1425 | ++$counter if $arg->run(bless \$char); | ||
| 1426 | } | ||
| 1427 | return Sidef::Types::Number::Number::_set_int($counter); | ||
| 1428 | } | ||
| 1429 | |||
| 1430 | my $ss = "$arg"; | ||
| 1431 | ++$counter while $s =~ /\Q$ss\E/g; | ||
| 1432 | Sidef::Types::Number::Number::_set_int($counter); | ||
| 1433 | } | ||
| 1434 | |||
| 1435 | sub overlaps { | ||
| 1436 | my ($self, $arg) = @_; | ||
| 1437 | CORE::index($$self ^ "$arg", "\0") != -1 | ||
| 1438 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1439 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1440 | } | ||
| 1441 | |||
| 1442 | sub begins_with { | ||
| 1443 | my ($self, $string) = @_; | ||
| 1444 | |||
| 1445 | $string = "$string"; | ||
| 1446 | |||
| 1447 | CORE::length($$self) < (my $len = CORE::length($string)) | ||
| 1448 | && return (Sidef::Types::Bool::Bool::FALSE); | ||
| 1449 | |||
| 1450 | CORE::substr($$self, 0, $len) eq $string | ||
| 1451 | && return (Sidef::Types::Bool::Bool::TRUE); | ||
| 1452 | |||
| 1453 | Sidef::Types::Bool::Bool::FALSE; | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | *starts_with = \&begins_with; | ||
| 1457 | |||
| 1458 | sub ends_with { | ||
| 1459 | my ($self, $string) = @_; | ||
| 1460 | |||
| 1461 | $string = "$string"; | ||
| 1462 | |||
| 1463 | CORE::length($$self) < (my $len = CORE::length($string)) | ||
| 1464 | && return (Sidef::Types::Bool::Bool::FALSE); | ||
| 1465 | |||
| 1466 | CORE::substr($$self, -$len) eq $string | ||
| 1467 | && return (Sidef::Types::Bool::Bool::TRUE); | ||
| 1468 | |||
| 1469 | Sidef::Types::Bool::Bool::FALSE; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | sub delete_prefix { | ||
| 1473 | my ($self, $prefix) = @_; | ||
| 1474 | |||
| 1475 | $prefix = "$prefix"; | ||
| 1476 | |||
| 1477 | my $len = CORE::length($prefix); | ||
| 1478 | |||
| 1479 | if ($len > 0 && CORE::substr($$self, 0, $len) eq $prefix) { | ||
| 1480 | return $self->new(CORE::substr($$self, $len)); | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | $self; | ||
| 1484 | } | ||
| 1485 | |||
| 1486 | *remove_prefix = \&delete_prefix; | ||
| 1487 | |||
| 1488 | sub delete_suffix { | ||
| 1489 | my ($self, $suffix) = @_; | ||
| 1490 | |||
| 1491 | $suffix = "$suffix"; | ||
| 1492 | |||
| 1493 | my $len = CORE::length($suffix); | ||
| 1494 | |||
| 1495 | if ($len > 0 && CORE::substr($$self, -$len) eq $suffix) { | ||
| 1496 | return $self->new(CORE::substr($$self, 0, CORE::length($$self) - $len)); | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | $self; | ||
| 1500 | } | ||
| 1501 | |||
| 1502 | *remove_suffix = \&delete_suffix; | ||
| 1503 | |||
| 1504 | sub looks_like_number { | ||
| 1505 | my ($self) = @_; | ||
| 1506 | Scalar::Util::looks_like_number($$self) | ||
| 1507 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1508 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1509 | } | ||
| 1510 | |||
| 1511 | *is_numeric = \&looks_like_number; | ||
| 1512 | |||
| 1513 | sub is_palindrome { | ||
| 1514 | my ($self) = @_; | ||
| 1515 | $$self eq CORE::reverse(${$self}) | ||
| 1516 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1517 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1518 | } | ||
| 1519 | |||
| 1520 | sub warn { | ||
| 1521 | my ($self) = @_; | ||
| 1522 | warn $$self; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | sub die { | ||
| 1526 | my ($self) = @_; | ||
| 1527 | die $$self; | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | sub encode { | ||
| 1531 | my ($self, $enc) = @_; | ||
| 1532 | state $x = require Encode; | ||
| 1533 | $self->new(Encode::encode("$enc", $$self)); | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | sub decode { | ||
| 1537 | my ($self, $enc) = @_; | ||
| 1538 | state $x = require Encode; | ||
| 1539 | $self->new(Encode::decode("$enc", $$self)); | ||
| 1540 | } | ||
| 1541 | |||
| 1542 | sub encode_utf8 { | ||
| 1543 | my ($self) = @_; | ||
| 1544 | state $x = require Encode; | ||
| 1545 | $self->new(Encode::encode_utf8($$self)); | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | sub decode_utf8 { | ||
| 1549 | my ($self) = @_; | ||
| 1550 | state $x = require Encode; | ||
| 1551 | $self->new(Encode::decode_utf8($$self)); | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | sub deflate { | ||
| 1555 | my ($self) = @_; | ||
| 1556 | state $x = require IO::Compress::RawDeflate; | ||
| 1557 | my $input = $$self; | ||
| 1558 | IO::Compress::RawDeflate::rawdeflate(\$input => \my $output) | ||
| 1559 | or CORE::die("String.deflate failed: $IO::Compress::RawDeflate::RawDeflateError"); | ||
| 1560 | bless \$output; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | sub inflate { | ||
| 1564 | my ($self) = @_; | ||
| 1565 | state $x = require IO::Uncompress::RawInflate; | ||
| 1566 | my $input = $$self; | ||
| 1567 | IO::Uncompress::RawInflate::rawinflate(\$input => \my $output) | ||
| 1568 | or CORE::die("String.inflate failed: $IO::Uncompress::RawInflate::RawInflateError"); | ||
| 1569 | bless \$output; | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | sub gzip { | ||
| 1573 | my ($self) = @_; | ||
| 1574 | state $x = require IO::Compress::Gzip; | ||
| 1575 | my $input = $$self; | ||
| 1576 | IO::Compress::Gzip::gzip(\$input => \my $output) | ||
| 1577 | or CORE::die("String.gzip failed: $IO::Compress::Gzip::GzipError"); | ||
| 1578 | bless \$output; | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | sub gunzip { | ||
| 1582 | my ($self) = @_; | ||
| 1583 | state $x = require IO::Uncompress::Gunzip; | ||
| 1584 | my $input = $$self; | ||
| 1585 | IO::Uncompress::Gunzip::gunzip(\$input => \my $output) | ||
| 1586 | or CORE::die("String.gunzip failed: $IO::Uncompress::Gunzip::GunzipError"); | ||
| 1587 | bless \$output; | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | sub from_json { | ||
| 1591 | my ($self) = @_; | ||
| 1592 | state $x = require JSON::PP; | ||
| 1593 | state $y = require Encode; | ||
| 1594 | Sidef::Types::Perl::Perl->to_sidef(scalar JSON::PP::decode_json(Encode::encode_utf8($$self))); | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | sub _require { | ||
| 1598 | my ($self) = @_; | ||
| 1599 | |||
| 1600 | my $name = $$self; | ||
| 1601 | eval { require(($name . '.pm') =~ s{::}{/}gr) }; | ||
| 1602 | |||
| 1603 | if ($@) { | ||
| 1604 | CORE::die CORE::substr($@, 0, CORE::rindex($@, ' at ')), "\n"; | ||
| 1605 | } | ||
| 1606 | |||
| 1607 | $name; | ||
| 1608 | } | ||
| 1609 | |||
| 1610 | sub require { | ||
| 1611 | my ($self) = @_; | ||
| 1612 | Sidef::Module::OO->__NEW__($self->_require); | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | sub frequire { | ||
| 1616 | my ($self) = @_; | ||
| 1617 | Sidef::Module::Func->__NEW__($self->_require); | ||
| 1618 | } | ||
| 1619 | |||
| 1620 | sub use { | ||
| 1621 | my ($self) = @_; | ||
| 1622 | eval("use $$self"); | ||
| 1623 | $@ ? Sidef::Types::Bool::Bool::FALSE : Sidef::Types::Bool::Bool::TRUE; | ||
| 1624 | } | ||
| 1625 | |||
| 1626 | sub run { | ||
| 1627 | my ($method, $self, @args) = @_; | ||
| 1628 | $self->$$method(@args); | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | sub unescape { | ||
| 1632 | my ($self) = @_; | ||
| 1633 | $self->new($$self =~ s{\\(.)}{$1}grs); | ||
| 1634 | } | ||
| 1635 | |||
| 1636 | sub _bwt_sort { | ||
| 1637 | my ($s, $LOOKAHEAD_LEN) = @_; # O(n * LOOKAHEAD_LEN) space (fast) | ||
| 1638 | |||
| 1639 | $LOOKAHEAD_LEN //= 128; | ||
| 1640 | |||
| 1641 | my $len = CORE::length($s); | ||
| 1642 | my $double_s = $s . $s; # Pre-compute doubled string | ||
| 1643 | |||
| 1644 | # Schwartzian transform with optimized sorting | ||
| 1645 | return [ | ||
| 1646 | map { $_->[1] } | ||
| 1647 | sort { | ||
| 1648 | ($a->[0] cmp $b->[0]) | ||
| 1649 | || do { | ||
| 1650 | my ($cmp, $s_len) = (0, $LOOKAHEAD_LEN << 2); | ||
| 1651 | while (1) { | ||
| 1652 | ($cmp = CORE::substr($double_s, $a->[1], $s_len) cmp CORE::substr($double_s, $b->[1], $s_len)) && last; | ||
| 1653 | $s_len <<= 1; | ||
| 1654 | } | ||
| 1655 | $cmp; | ||
| 1656 | } | ||
| 1657 | } | ||
| 1658 | map { | ||
| 1659 | my $pos = $_; | ||
| 1660 | my $end = $pos + $LOOKAHEAD_LEN; | ||
| 1661 | |||
| 1662 | # Handle wraparound efficiently | ||
| 1663 | my $t = | ||
| 1664 | ($end <= $len) | ||
| 1665 | ? CORE::substr($s, $pos, $LOOKAHEAD_LEN) | ||
| 1666 | : CORE::substr($double_s, $pos, $LOOKAHEAD_LEN); | ||
| 1667 | |||
| 1668 | [$t, $pos] | ||
| 1669 | } 0 .. $len - 1 | ||
| 1670 | ]; | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | sub bwt_encode { | ||
| 1674 | my ($s) = @_; | ||
| 1675 | |||
| 1676 | $s = $$s; | ||
| 1677 | |||
| 1678 | my $bwt = _bwt_sort($s); | ||
| 1679 | |||
| 1680 | my $ret = ''; | ||
| 1681 | my $idx = 0; | ||
| 1682 | |||
| 1683 | my $i = 0; | ||
| 1684 | foreach my $pos (@$bwt) { | ||
| 1685 | $ret .= CORE::substr($s, $pos - 1, 1); | ||
| 1686 | $idx = $i if !$pos; | ||
| 1687 | ++$i; | ||
| 1688 | } | ||
| 1689 | |||
| 1690 | return ((bless \$ret), Sidef::Types::Number::Number::_set_int($idx)); | ||
| 1691 | } | ||
| 1692 | |||
| 1693 | sub bwt_decode { | ||
| 1694 | my ($bwt, $idx) = @_; | ||
| 1695 | |||
| 1696 | $bwt = $$bwt; | ||
| 1697 | $idx = CORE::int($idx); | ||
| 1698 | |||
| 1699 | my @L = CORE::unpack('C*', $bwt); | ||
| 1700 | my $n = scalar @L; | ||
| 1701 | |||
| 1702 | my @freq = (0) x 256; | ||
| 1703 | $freq[$_]++ for @L; | ||
| 1704 | |||
| 1705 | my @cumul = (0) x 257; | ||
| 1706 | $cumul[$_ + 1] = $cumul[$_] + $freq[$_] for 0 .. 255; | ||
| 1707 | |||
| 1708 | my @next; | ||
| 1709 | my @cnt = (0) x 256; | ||
| 1710 | for my $i (0 .. $n - 1) { | ||
| 1711 | $next[$cumul[$L[$i]] + $cnt[$L[$i]]++] = $i; | ||
| 1712 | } | ||
| 1713 | |||
| 1714 | my @dec; | ||
| 1715 | my $i = $idx; | ||
| 1716 | for (1 .. $n) { | ||
| 1717 | $i = $next[$i]; | ||
| 1718 | CORE::push(@dec, $L[$i]); | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | my $str = CORE::pack('C*', @dec); | ||
| 1722 | bless \$str; | ||
| 1723 | } | ||
| 1724 | |||
| 1725 | sub apply_escapes { | ||
| 1726 | my ($self, $parser) = @_; | ||
| 1727 | |||
| 1728 | $$self =~ /\\|#\{/ || return $self; # return faster | ||
| 1729 | |||
| 1730 | require Encode; | ||
| 1731 | |||
| 1732 | my $str = $$self; | ||
| 1733 | |||
| 1734 | state $esc = { | ||
| 1735 | a => "\a", | ||
| 1736 | b => "\b", | ||
| 1737 | e => "\e", | ||
| 1738 | f => "\f", | ||
| 1739 | n => "\n", | ||
| 1740 | r => "\r", | ||
| 1741 | t => "\t", | ||
| 1742 | s => ' ', | ||
| 1743 | v => chr(11), | ||
| 1744 | }; | ||
| 1745 | |||
| 1746 | my @inline_expressions; | ||
| 1747 | my @chars = split(//, $str); | ||
| 1748 | |||
| 1749 | my $spec = 'E'; | ||
| 1750 | for (my $i = 0 ; $i <= $#chars ; $i++) { | ||
| 1751 | |||
| 1752 | if ($chars[$i] eq '\\' and exists $chars[$i + 1]) { | ||
| 1753 | my $char = $chars[$i + 1]; | ||
| 1754 | |||
| 1755 | if (exists $esc->{$char}) { | ||
| 1756 | splice(@chars, $i--, 2, $esc->{$char}); | ||
| 1757 | next; | ||
| 1758 | } | ||
| 1759 | elsif ($char =~ /^[0-7]/) { | ||
| 1760 | |||
| 1761 | my $max = List::Util::min($i + 3, $#chars); | ||
| 1762 | my $str = CORE::join('', @chars[$i + 1 .. $max]); | ||
| 1763 | |||
| 1764 | if ($str =~ /^(0[0-7]{1,2}|[0-7]{1,2})/) { | ||
| 1765 | splice @chars, $i, 1 + $+[0], CORE::chr(CORE::oct($1)); | ||
| 1766 | } | ||
| 1767 | } | ||
| 1768 | elsif ( $char eq 'L' | ||
| 1769 | or $char eq 'U' | ||
| 1770 | or $char eq 'E' | ||
| 1771 | or $char eq 'Q') { | ||
| 1772 | $spec = $char; | ||
| 1773 | splice(@chars, $i--, 2); | ||
| 1774 | next; | ||
| 1775 | } | ||
| 1776 | elsif ($char eq 'l') { | ||
| 1777 | if (exists $chars[$i + 2]) { | ||
| 1778 | splice(@chars, $i, 3, CORE::lc($chars[$i + 2])); | ||
| 1779 | next; | ||
| 1780 | } | ||
| 1781 | else { | ||
| 1782 | splice(@chars, $i, 2); | ||
| 1783 | } | ||
| 1784 | } | ||
| 1785 | elsif ($char eq 'u') { | ||
| 1786 | if (exists $chars[$i + 2]) { | ||
| 1787 | splice(@chars, $i, 3, CORE::uc($chars[$i + 2])); | ||
| 1788 | next; | ||
| 1789 | } | ||
| 1790 | else { | ||
| 1791 | splice(@chars, $i, 2); | ||
| 1792 | } | ||
| 1793 | } | ||
| 1794 | elsif ($char eq 'N') { | ||
| 1795 | if (exists $chars[$i + 2] and $chars[$i + 2] eq '{') { | ||
| 1796 | |||
| 1797 | my $max = List::Util::min($i + 2048, $#chars); | ||
| 1798 | my $str = CORE::join('', @chars[$i + 2 .. $max]); | ||
| 1799 | |||
| 1800 | if ($str =~ /^\{(.*?)\}/) { | ||
| 1801 | require charnames; | ||
| 1802 | my $char = charnames::string_vianame($1); | ||
| 1803 | if (defined $char) { | ||
| 1804 | splice(@chars, $i--, 2 + $+[0], $char); | ||
| 1805 | next; | ||
| 1806 | } | ||
| 1807 | } | ||
| 1808 | else { | ||
| 1809 | CORE::warn("Missing right brace on \\N{, within string!\n"); | ||
| 1810 | } | ||
| 1811 | } | ||
| 1812 | else { | ||
| 1813 | CORE::warn("Missing braces on \\N{}, within string!\n"); | ||
| 1814 | } | ||
| 1815 | splice(@chars, $i, 1); | ||
| 1816 | } | ||
| 1817 | elsif ($char eq 'x') { | ||
| 1818 | if (exists $chars[$i + 2]) { | ||
| 1819 | |||
| 1820 | my $max = List::Util::min($i + 64, $#chars); | ||
| 1821 | my $str = CORE::join('', @chars[$i + 2 .. $max]); | ||
| 1822 | |||
| 1823 | if ($str =~ /^\{([[:xdigit:]]+)\}/) { | ||
| 1824 | splice(@chars, $i, 2 + $+[0], chr(CORE::hex($1))); | ||
| 1825 | next; | ||
| 1826 | } | ||
| 1827 | elsif ($str =~ /^([[:xdigit:]]{1,2})/) { | ||
| 1828 | splice(@chars, $i, 2 + $+[0], chr(CORE::hex($1))); | ||
| 1829 | next; | ||
| 1830 | } | ||
| 1831 | } | ||
| 1832 | splice(@chars, $i, 1); | ||
| 1833 | } | ||
| 1834 | elsif ($char eq 'o') { | ||
| 1835 | if (exists $chars[$i + 2] and $chars[$i + 2] eq '{') { | ||
| 1836 | |||
| 1837 | my $max = List::Util::min($i + 64, $#chars); | ||
| 1838 | my $str = CORE::join('', @chars[$i + 2 .. $max]); | ||
| 1839 | |||
| 1840 | if ($str =~ /^\{(.*?)\}/) { | ||
| 1841 | splice(@chars, $i--, 2 + $+[0], CORE::chr(CORE::oct($1))); | ||
| 1842 | next; | ||
| 1843 | } | ||
| 1844 | else { | ||
| 1845 | CORE::warn("Missing right brace on \\o{, within string!\n"); | ||
| 1846 | } | ||
| 1847 | } | ||
| 1848 | else { | ||
| 1849 | CORE::warn("Missing braces on \\o{}, within string!\n"); | ||
| 1850 | } | ||
| 1851 | splice(@chars, $i, 1); | ||
| 1852 | } | ||
| 1853 | elsif ($char eq 'c') { | ||
| 1854 | if (exists $chars[$i + 2]) { # bug for: "\c\\" | ||
| 1855 | splice(@chars, $i, 3, chr((CORE::ord(CORE::uc($chars[$i + 2])) + 64) % 128)); | ||
| 1856 | } | ||
| 1857 | else { | ||
| 1858 | CORE::warn "[WARNING] Missing control char name in \\c, within string\n"; | ||
| 1859 | splice(@chars, $i, 2); | ||
| 1860 | } | ||
| 1861 | } | ||
| 1862 | else { | ||
| 1863 | splice(@chars, $i, 1); | ||
| 1864 | } | ||
| 1865 | } | ||
| 1866 | elsif ( $chars[$i] eq '#' | ||
| 1867 | and exists $chars[$i + 1] | ||
| 1868 | and $chars[$i + 1] eq '{') { | ||
| 1869 | if (ref $parser eq 'Sidef::Parser') { | ||
| 1870 | my $code = 'do' . CORE::join('', @chars[$i + 1 .. $#chars]); | ||
| 1871 | my $block = $parser->parse_expr(code => \$code); | ||
| 1872 | push @inline_expressions, [$i, $block]; | ||
| 1873 | splice(@chars, $i--, 1 + pos($code) - 2); | ||
| 1874 | } | ||
| 1875 | else { | ||
| 1876 | # Can't eval #{} at runtime! | ||
| 1877 | } | ||
| 1878 | } | ||
| 1879 | |||
| 1880 | if ($spec ne 'E' and defined($chars[$i])) { | ||
| 1881 | if ($spec eq 'U') { | ||
| 1882 | $chars[$i] = CORE::uc($chars[$i]); | ||
| 1883 | } | ||
| 1884 | elsif ($spec eq 'L') { | ||
| 1885 | $chars[$i] = CORE::lc($chars[$i]); | ||
| 1886 | } | ||
| 1887 | elsif ($spec eq 'Q') { | ||
| 1888 | $chars[$i] = CORE::quotemeta($chars[$i]); | ||
| 1889 | } | ||
| 1890 | } | ||
| 1891 | } | ||
| 1892 | |||
| 1893 | if (@inline_expressions) { | ||
| 1894 | |||
| 1895 | foreach my $i (0 .. $#inline_expressions) { | ||
| 1896 | my $pair = $inline_expressions[$i]; | ||
| 1897 | splice @chars, $pair->[0] + $i, 0, $pair->[1]; | ||
| 1898 | } | ||
| 1899 | |||
| 1900 | state %cache; | ||
| 1901 | my $type = (ref($self) =~ s/::Types::/::DataTypes::/r); | ||
| 1902 | my $dt = ($cache{$type} //= bless({}, $type)); | ||
| 1903 | |||
| 1904 | my $expr = { | ||
| 1905 | $parser->{class} => [ | ||
| 1906 | { | ||
| 1907 | self => $dt, | ||
| 1908 | call => [{method => 'interpolate'}] | ||
| 1909 | } | ||
| 1910 | ] | ||
| 1911 | }; | ||
| 1912 | |||
| 1913 | my $append_arg = sub { | ||
| 1914 | push @{$expr->{$parser->{class}}[0]{call}[-1]{arg}}, $_[0]; | ||
| 1915 | }; | ||
| 1916 | |||
| 1917 | my $string = ''; | ||
| 1918 | foreach my $char (@chars) { | ||
| 1919 | if (ref($char)) { | ||
| 1920 | my $block = $char; | ||
| 1921 | |||
| 1922 | if ($string ne '') { | ||
| 1923 | $append_arg->($string); | ||
| 1924 | $string = ''; | ||
| 1925 | } | ||
| 1926 | $append_arg->($block); | ||
| 1927 | } | ||
| 1928 | else { | ||
| 1929 | $string .= $char; | ||
| 1930 | } | ||
| 1931 | } | ||
| 1932 | |||
| 1933 | if ($string ne '') { | ||
| 1934 | $append_arg->($string); | ||
| 1935 | } | ||
| 1936 | |||
| 1937 | return $expr; | ||
| 1938 | } | ||
| 1939 | |||
| 1940 | $self->new(CORE::join('', @chars)); | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | sub shift_left { | ||
| 1944 | my ($self, $i) = @_; | ||
| 1945 | my $len = CORE::length($$self); | ||
| 1946 | $i = CORE::int($i); | ||
| 1947 | $i = $len if $i > $len; | ||
| 1948 | $self->new(CORE::substr($$self, $i)); | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | *drop_left = \&shift_left; | ||
| 1952 | |||
| 1953 | sub shift_right { | ||
| 1954 | my ($self, $i) = @_; | ||
| 1955 | $i = CORE::int($i); | ||
| 1956 | $self->new(CORE::substr($$self, 0, -$i)); | ||
| 1957 | } | ||
| 1958 | |||
| 1959 | *drop_right = \&shift_right; | ||
| 1960 | |||
| 1961 | sub to_str { | ||
| 1962 | $_[0]; | ||
| 1963 | } | ||
| 1964 | |||
| 1965 | *to_s = \&to_str; | ||
| 1966 | |||
| 1967 | sub to_int { | ||
| 1968 | my ($self, $base) = @_; | ||
| 1969 | Sidef::Types::Number::Number->new($$self, (defined($base) ? $base : ()))->int; | ||
| 1970 | } | ||
| 1971 | |||
| 1972 | *to_i = \&to_int; | ||
| 1973 | |||
| 1974 | sub to_num { | ||
| 1975 | my ($self, $base) = @_; | ||
| 1976 | Sidef::Types::Number::Number->new($$self, (defined($base) ? $base : ())); | ||
| 1977 | } | ||
| 1978 | |||
| 1979 | *num = \&to_num; | ||
| 1980 | *to_n = \&to_num; | ||
| 1981 | |||
| 1982 | { | ||
| 1983 | my %esc = ( | ||
| 1984 | "\a" => "\\a", | ||
| 1985 | "\b" => "\\b", | ||
| 1986 | "\t" => "\\t", | ||
| 1987 | "\n" => "\\n", | ||
| 1988 | "\f" => "\\f", | ||
| 1989 | "\r" => "\\r", | ||
| 1990 | "\e" => "\\e", | ||
| 1991 | chr(11) => "\\v", | ||
| 1992 | ); | ||
| 1993 | |||
| 1994 | # Function by Gisle Aas, copied from `Data::Dump` (thanks). | ||
| 1995 | sub dump { | ||
| 1996 | my $str = "${$_[0]}"; | ||
| 1997 | |||
| 1998 | $str =~ s/([\\\"]|#\{)/\\$1/g; | ||
| 1999 | $str =~ /[^\040-\176]/ or return bless \(my $value = qq("$str")); | ||
| 2000 | |||
| 2001 | $str =~ s/([\a\b\t\n\f\r\e\13])/$esc{$1}/g; | ||
| 2002 | $str =~ s/([\0-\037])(?!\d)/CORE::sprintf('\\%o',CORE::ord($1))/eg; | ||
| 2003 | |||
| 2004 | $str =~ s/([\0-\037\177-\377])/CORE::sprintf('\\x%02X',CORE::ord($1))/eg; | ||
| 2005 | $str =~ s/([^\040-\176])/CORE::sprintf('\\x{%X}',CORE::ord($1))/eg; | ||
| 2006 | |||
| 2007 | bless \(my $value2 = qq("$str")); | ||
| 2008 | } | ||
| 2009 | } | ||
| 2010 | |||
| 2011 | *inspect = \&dump; | ||
| 2012 | |||
| 2013 | { | ||
| 2014 | no strict 'refs'; | ||
| 2015 | |||
| 2016 | *{__PACKAGE__ . '::' . '=~'} = \&match; | ||
| 2017 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 2018 | *{__PACKAGE__ . '::' . '+'} = \&append; | ||
| 2019 | *{__PACKAGE__ . '::' . '++'} = \&inc; | ||
| 2020 | *{__PACKAGE__ . '::' . '-'} = \&diff; | ||
| 2021 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 2022 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 2023 | *{__PACKAGE__ . '::' . '≠'} = \≠ | ||
| 2024 | *{__PACKAGE__ . '::' . '>'} = \> | ||
| 2025 | *{__PACKAGE__ . '::' . '<'} = \< | ||
| 2026 | *{__PACKAGE__ . '::' . '>='} = \≥ | ||
| 2027 | *{__PACKAGE__ . '::' . '≥'} = \≥ | ||
| 2028 | *{__PACKAGE__ . '::' . '<='} = \≤ | ||
| 2029 | *{__PACKAGE__ . '::' . '≤'} = \≤ | ||
| 2030 | *{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
| 2031 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 2032 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 2033 | *{__PACKAGE__ . '::' . '..'} = \&to; | ||
| 2034 | *{__PACKAGE__ . '::' . '^'} = \&xor; | ||
| 2035 | *{__PACKAGE__ . '::' . '|'} = \∨ | ||
| 2036 | *{__PACKAGE__ . '::' . '&'} = \∧ | ||
| 2037 | *{__PACKAGE__ . '::' . '<<'} = \&shift_left; | ||
| 2038 | *{__PACKAGE__ . '::' . '>>'} = \&shift_right; | ||
| 2039 | *{__PACKAGE__ . '::' . '%'} = \&sprintf; | ||
| 2040 | *{__PACKAGE__ . '::' . '~'} = \¬ | ||
| 2041 | } | ||
| 2042 | |||
| 2043 | 1 |