1
0
mirror of https://github.com/golang/go synced 2024-11-25 12:07:56 -07:00

specification of []int(string) and []byte(string).

also clarify other string conversions.

R=rsc, iant, gri, ken2
CC=golang-dev
https://golang.org/cl/207103
This commit is contained in:
Rob Pike 2010-02-17 11:26:09 +11:00
parent 04d9c8853b
commit 1811fac7cb

View File

@ -3227,33 +3227,60 @@ represent the value the conversion succeeds but the result value is
implementation-dependent. implementation-dependent.
</p> </p>
<h4>Conversions to a string type</h4> <h4>Conversions to and from a string type</h4>
<ol> <ol>
<li> <li>
Converting an integer value yields a string containing the UTF-8 Converting a signed or unsigned integer value to a string type yields a
representation of the integer. string containing the UTF-8 representation of the integer.
Negative values are converted to <code>"\uFFFD"</code>.
<pre> <pre>
string(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" string('a') // "a"
string(-1) // "\ufffd" == "\xef\xbf\xbd "
string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
type MyString string
MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
</pre> </pre>
</li> </li>
<li> <li>
Converting a slice of integers yields a string that is the Converting a value of type <code>[]byte</code> (or
concatenation of the individual integers converted to strings. the equivalent <code>[]uint8</code>) to a string type yields a
If the slice value is <code>nil</code>, the result is the empty string. string whose successive bytes are the elements of the slice. If
the slice value is <code>nil</code>, the result is the empty string.
<pre>
string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
</pre>
</li>
<li>
Converting a value of type <code>[]int</code> to a string type yields
a string that is the concatenation of the individual integers
converted to strings. If the slice value is <code>nil</code>, the
result is the empty string.
<pre> <pre>
string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
</pre> </pre>
</li> </li>
<li> <li>
Converting a slice of bytes yields a string whose successive Converting a value of a string type to <code>[]byte</code> (or <code>[]uint8</code>)
bytes are those of the slice. If the slice value is <code>nil</code>, yields a slice whose successive elements are the bytes of the string.
the result is the empty string. If the string is empty, the result is <code>[]byte(nil)</code>.
<pre> <pre>
string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello" []byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
</pre>
</li>
<li>
Converting a value of a string type to <code>[]int</code> yields a
slice containing the individual Unicode code points of the string.
If the string is empty, the result is <code>[]int(nil)</code>.
<pre>
[]int(MyString("白鵬翔")) // []int{0x767d, 0x9d6c, 0x7fd4}
</pre> </pre>
</li> </li>
</ol> </ol>
@ -3847,7 +3874,7 @@ statement</a>. Function calls on the left hand side will be evaluated
exactly once per iteration. exactly once per iteration.
</p> </p>
<p> <p>
For strings, the "range" clause iterates over the Unicode code points For a value of a string type, the "range" clause iterates over the Unicode code points
in the string. On successive iterations, the index variable will be the in the string. On successive iterations, the index variable will be the
index of the first byte of successive UTF-8-encoded code points in the string, and index of the first byte 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 second variable, of type <code>int</code>, will be the value of
@ -4777,5 +4804,6 @@ The following minimal alignment properties are guaranteed:
<ul> <ul>
<li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li> <li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li>
<li><span class="alert">Method expressions are not implemented.</span></li> <li><span class="alert">Method expressions are not implemented.</span></li>
<li><span class="alert">Conversions from strings to <code>[]int</code> and <code>[]byte</code> are not implemented..</span></li>
<li><span class="alert">Gccgo allows only one init() function per source file.</span></li> <li><span class="alert">Gccgo allows only one init() function per source file.</span></li>
</ul> </ul>