From 1811fac7cbceedfdc0eb0d77e81ddec05f4f4020 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 17 Feb 2010 11:26:09 +1100 Subject: [PATCH] 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 --- doc/go_spec.html | 52 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index cd46744b9c..0f6475eac5 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -3227,33 +3227,60 @@ represent the value the conversion succeeds but the result value is implementation-dependent.

-

Conversions to a string type

+

Conversions to and from a string type

+
  1. -Converting an integer value yields a string containing the UTF-8 -representation of the integer. +Converting a signed or unsigned integer value to a string type yields a +string containing the UTF-8 representation of the integer. +Negative values are converted to "\uFFFD". + +
    +string('a')  // "a"
    +string(-1)  // "\ufffd" == "\xef\xbf\xbd "
    +string(0xf8)  // "\u00f8" == "ø" == "\xc3\xb8"
    +type MyString string
    +MyString(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
    +
    +
  2. + +
  3. +Converting a value of type []byte (or +the equivalent []uint8) to a string type yields a +string whose successive bytes are the elements of the slice. If +the slice value is nil, the result is the empty string.
    -string(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
    +string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'})  // "hellø"
     
  4. -Converting a slice of integers yields a string that is the -concatenation of the individual integers converted to strings. -If the slice value is nil, the result is the empty string. +Converting a value of type []int to a string type yields +a string that is the concatenation of the individual integers +converted to strings. If the slice value is nil, the +result is the empty string.
     string([]int{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
     
  5. -Converting a slice of bytes yields a string whose successive -bytes are those of the slice. If the slice value is nil, -the result is the empty string. +Converting a value of a string type to []byte (or []uint8) +yields a slice whose successive elements are the bytes of the string. +If the string is empty, the result is []byte(nil). + +
    +[]byte("hellø")  // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
    +
    +
  6. +
  7. +Converting a value of a string type to []int yields a +slice containing the individual Unicode code points of the string. +If the string is empty, the result is []int(nil).
    -string([]byte{'h', 'e', 'l', 'l', 'o'})  // "hello"
    +[]int(MyString("白鵬翔"))  // []int{0x767d, 0x9d6c, 0x7fd4}
     
@@ -3847,7 +3874,7 @@ statement. Function calls on the left hand side will be evaluated exactly once per iteration.

-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 index of the first byte of successive UTF-8-encoded code points in the string, and the second variable, of type int, will be the value of @@ -4777,5 +4804,6 @@ The following minimal alignment properties are guaranteed:

-- 2.50.0