Browse Source

* fix #40500: do a for-in on a string variable using low(strvar) to high(strvar) so that zero based strings are handled correctly
+ added test

Sven/Sarah Barth 1 year ago
parent
commit
68668c649e
2 changed files with 21 additions and 2 deletions
  1. 2 2
      compiler/nflw.pas
  2. 19 0
      tests/webtbs/tw40500.pp

+ 2 - 2
compiler/nflw.pas

@@ -586,8 +586,8 @@ implementation
         addstatement(loopbodystatement,hloopbody);
         addstatement(loopbodystatement,hloopbody);
 
 
         forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
         forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
-          genintconstnode(1),
-          cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
+          cinlinenode.createintern(in_low_x,false,ctemprefnode.create(stringvar)),
+          cinlinenode.create(in_high_x,false,ctemprefnode.create(stringvar)),
           loopbody,
           loopbody,
           false);
           false);
 
 

+ 19 - 0
tests/webtbs/tw40500.pp

@@ -0,0 +1,19 @@
+program tw40500;
+
+{$mode objfpc} {$h+} {$coperators+} {$zerobasedstrings+}
+uses
+	SysUtils;
+
+var
+	s: string;
+	c: char;
+
+begin
+	s := '';
+	for c in string('share this to instantly die') do
+		if (c >= #32) and (c <= #127) then s += c else s += '#' + IntToStr(ord(c));
+	writeln(s);
+        if s <> 'share this to instantly die' then
+                Halt(1);
+end.
+