Permalink
Browse files

Perlito5 - javascript: fix 3-arguments substr()

  • Loading branch information...
1 parent ae33743 commit 78fd976e726088577687e56d13ff3405f5fee066 @fglock committed Mar 29, 2012
Showing with 53 additions and 25 deletions.
  1. +4 −4 TODO-perlito5
  2. +4 −0 html/perlito5.js
  3. +1 −1 lib5/Perlito5/Javascript/CORE.pm
  4. +4 −0 src5/lib/Perlito5/Javascript/CORE.pm
  5. +40 −20 t5/01-perlito/04-string.t
View
@@ -86,10 +86,10 @@ TODO list for Perlito5
-- bug: "my" variables - this doesn't work as expected: my $v = $v
possible fix: rename variables
--- move "substr" and others to CORE
--- lvalue "substr"
--- subroutine signatures
--- array interpolation in argument lists
+-- lvalue substr(); 4-arguments substr()
+-- pos($str)
+
+-- missing some types of subroutine signatures
-- bug: negative index in array doesn't work
View
@@ -612,6 +612,10 @@ CORE.substr = function(List__) {
var offset = List__[1];
var length = List__[2];
var replacement = List__[3];
+ if (length < 0) {
+ var s = p5str(expr);
+ length = s.length - offset + length;
+ }
return p5str(expr).substr(offset, length);
};
Oops, something went wrong.
@@ -132,6 +132,10 @@ CORE.substr = function(List__) {
var offset = List__[1];
var length = List__[2];
var replacement = List__[3];
+ if (length < 0) {
+ var s = p5str(expr);
+ length = s.length - offset + length;
+ }
return p5str(expr).substr(offset, length);
};
View
@@ -2,30 +2,50 @@ use v5;
use strict;
use feature 'say';
-package Main;
+say '1..10';
- say '1..5';
+my $x = "abcd";
+if (substr($x,1,1) ne "b") {
+ print 'not '
+};
+say 'ok 1 - substr ', substr($x,1,1);
- my $x = "abcd";
- if (substr($x,1,1) ne "b") {
- print 'not '
- };
- say 'ok 1 - substr ', substr($x,1,1);
+if (index($x,"c") ne 2) {
+ print 'not '
+};
+say 'ok 2 - index ', index($x,"c");
- if (index($x,"c") ne 2) {
- print 'not '
- };
- say 'ok 2 - index ', index($x,"c");
+if (substr($x,3,1) ne "d") {
+ print 'not '
+}
+say 'ok 3 - substr ', substr($x,3,1);
- if (substr($x,3,1) ne "d") {
- print 'not '
- }
- say 'ok 3 - substr ', substr($x,3,1);
+print 'not ' if !defined "abc";
+say 'ok 4 - defined str';
- print 'not ' if !defined "abc";
- say 'ok 4 - defined str';
+my $s = "o";
+$s .= "k 5 - concat";
+say $s;
- my $s = "o";
- $s .= "k 5 - concat";
- say $s;
+$s = "The black cat climbed the green tree";
+my $color = substr $s, 4, 5; # black
+my $middle = substr $s, 4, -11; # black cat climbed the
+my $end = substr $s, 14; # climbed the green tree
+my $tail = substr $s, -4; # tree
+my $z = substr $s, -4, 2; # tr
+
+print 'not ' if $color ne 'black';
+say 'ok 6';
+
+print 'not ' if $middle ne 'black cat climbed the';
+say "ok 7 # $middle";
+
+print 'not ' if $end ne 'climbed the green tree';
+say 'ok 8';
+
+print 'not ' if $tail ne 'tree';
+say 'ok 9';
+
+print 'not ' if $z ne 'tr';
+say 'ok 10';

0 comments on commit 78fd976

Please sign in to comment.