]> Cypherpunks repositories - gostls13.git/commitdiff
spec for range on strings
authorRob Pike <r@golang.org>
Thu, 16 Apr 2009 03:28:25 +0000 (20:28 -0700)
committerRob Pike <r@golang.org>
Thu, 16 Apr 2009 03:28:25 +0000 (20:28 -0700)
R=rsc,iant
DELTA=17  (11 added, 0 deleted, 6 changed)
OCL=27529
CL=27535

doc/go_spec.html

index 2e5aa626c5621b57feeb4a336da3d82237ac2ef1..9d8d2b4e01bef7787c678e5f43e76d81afca21a6 100644 (file)
@@ -3444,7 +3444,8 @@ for true { S() }        is the same as    for      { S() }
 
 <p>
 A "for" statement with a "range" clause
-iterates through all entries of an array, slice or map.
+iterates through all entries of an array, slice, string or map,
+or values received on a channel.
 For each entry it first assigns the current index or key to an iteration
 variable - or the current (index, element) or (key, value) pair to a pair
 of iteration variables - and then executes the block.
@@ -3455,20 +3456,30 @@ RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
 </pre>
 
 <p>
-The type of the right-hand expression in the "range" clause must be an array,
-slice or map, or a pointer to an array, slice or map;
+The type of the right-hand expression in the "range" clause must be an
+array, slice, string or map, or a pointer to an array, slice, string or map;
 or it may be a channel.
-If it is an array, slice or map,
+Except for channels,
 the identifier list must contain one or two identifiers denoting the
 iteration variables. On each iteration,
-the first variable is set to the array or slice index or
+the first variable is set to the string, array or slice index or
 map key, and the second variable, if present, is set to the corresponding
-array element or map value.
+string or array element or map value.
 The types of the array or slice index (always <code>int</code>)
 and element, or of the map key and value respectively,
 must be assignment compatible to the iteration variables.
 </p>
 <p>
+For strings, the "range" clause iterates over the Unicode code points
+in the string.  On successive iterations, the index variable will be the
+position of successive UTF-8-encoded code points in the string, and
+the second variable, of type <code>int</code>, will be the value of
+the corresponding code point.  If the iteration encounters an invalid
+UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
+the Unicode replacement character, and the next iteration will advance
+a single byte in the string.
+</p>
+<p>
 For channels, the identifier list must contain one identifier.
 The iteration recieves values sent on the channel until the channel is closed;
 it does not process the zero value sent before the channel is closed.