From: Rob Pike Date: Thu, 16 Apr 2009 03:28:25 +0000 (-0700) Subject: spec for range on strings X-Git-Tag: weekly.2009-11-06~1822 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7aee71bd4f743973d104e081586a892ebdaeed67;p=gostls13.git spec for range on strings R=rsc,iant DELTA=17 (11 added, 0 deleted, 6 changed) OCL=27529 CL=27535 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 2e5aa626c5..9d8d2b4e01 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -3444,7 +3444,8 @@ for true { S() } is the same as for { S() }

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 .

-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 int) and element, or of the map key and value respectively, must be assignment compatible to the iteration variables.

+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 int, will be the value of +the corresponding code point. If the iteration encounters an invalid +UTF-8 sequence, the second variable will be 0xFFFD, +the Unicode replacement character, and the next iteration will advance +a single byte in the string. +

+

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.