com php-langspec: Add support of negative string offsets: spec/10-expressions.md

[email protected] (Nikita Popov) Sun, 24 Apr 2016 14:07:50 +0000
Newsgroups php.standards.cvs
Message-ID <[email protected]>
Commit:    1bec75b6abf752c2a7888b6f0589a59c7eaabfd4
Author:    Francois Laupretre <[email protected]>         Mon, 7 Mar 2016 12:46:06 +0100
Committer: Nikita Popov <[email protected]>      Sun, 24 Apr 2016 16:07:50 +0200
Parents:   46a218a09ef6156e419194122a440e5400967fe2
Branches:  master

Link:       http://git.php.net/?p=php-langspec.git;a=commitdiff;h=1bec75b6abf752c2a7888b6f0589a59c7eaabfd4

Log:
Add support of negative string offsets

Additional fix: string are extended padding with spaces, not '\0'.
bugfix #71572: Assignment from empty string does not insert null byte anymore.

Bugs:
https://bugs.php.net/71572

Changed paths:
  M  spec/10-expressions.md


Diff:
diff --git a/spec/10-expressions.md b/spec/10-expressions.md
index 447e08e..134caec 100644
--- a/spec/10-expressions.md
+++ b/spec/10-expressions.md
@@ -1058,14 +1058,15 @@ The result is the added new element, or `NULL` if the element was not added.
 **postfix-expression designates a string**
 
 The *expression* is converted to `int` and the result is the character of the
-string at the position equal to that integer. If the integer is negative or refers
+string at the position corresponding to that integer. If the integer is negative,
+the position is counted backwards from the end of the string. If the position refers
 to a non-existing offset, the result is an empty string.
 
 If the operator is used as the left-hand side of a [*simple-assignment-expression*](#simple-assignment),
-the value being assigned is converted to string and the character in the specified offset will be
-replaced by the first character of the string. If the assigned string is empty, the `'\0'` character is used.
-If the string before the assignment had no such offset, the string is extended to include the offset
-with `'\0'` characters.
+
+- If the assigned string is empty, or in case of non-existing negative offset (absolute value larger than string length), a warning is raised and no assignment is performed.
+- If the offset is larger than the current string length, the string is extended to a length equal to the offset value, using space (0x20) padding characters.
+- The value being assigned is converted to string and the character in the specified offset is replaced by the first character of the string.
 
 The subscript operator can not be used on a string value in a byRef context or as the operand of the
 [postfix- or prefix-increment or decrement operators](#postfix-increment-and-decrement-operators) or on the left
@@ -1135,6 +1136,7 @@ $v["red"] = TRUE; // insert a new element with string key "red"
 function f() { return [1000, 2000, 3000]; }
 f()[2];      // designates element with value 3000
 "red"[1.9];    // designates "e"
+"red"[-2];    // designates "e"
 "red"[0][0][0];    // designates "r"
 // -----------------------------------------
 class MyVector implements ArrayAccess { /* ... */ }