github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/unicode/utf8/example_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package utf8_test
     6  
     7  import (
     8  	"fmt"
     9  	"unicode/utf8"
    10  )
    11  
    12  func ExampleDecodeLastRune() {
    13  	b := []byte("Hello, 世界")
    14  
    15  	for len(b) > 0 {
    16  		r, size := utf8.DecodeLastRune(b)
    17  		fmt.Printf("%c %v\n", r, size)
    18  
    19  		b = b[:len(b)-size]
    20  	}
    21  	// Output:
    22  	// 界 3
    23  	// 世 3
    24  	//   1
    25  	// , 1
    26  	// o 1
    27  	// l 1
    28  	// l 1
    29  	// e 1
    30  	// H 1
    31  }
    32  
    33  func ExampleDecodeLastRuneInString() {
    34  	str := "Hello, 世界"
    35  
    36  	for len(str) > 0 {
    37  		r, size := utf8.DecodeLastRuneInString(str)
    38  		fmt.Printf("%c %v\n", r, size)
    39  
    40  		str = str[:len(str)-size]
    41  	}
    42  	// Output:
    43  	// 界 3
    44  	// 世 3
    45  	//   1
    46  	// , 1
    47  	// o 1
    48  	// l 1
    49  	// l 1
    50  	// e 1
    51  	// H 1
    52  
    53  }
    54  
    55  func ExampleDecodeRune() {
    56  	b := []byte("Hello, 世界")
    57  
    58  	for len(b) > 0 {
    59  		r, size := utf8.DecodeRune(b)
    60  		fmt.Printf("%c %v\n", r, size)
    61  
    62  		b = b[size:]
    63  	}
    64  	// Output:
    65  	// H 1
    66  	// e 1
    67  	// l 1
    68  	// l 1
    69  	// o 1
    70  	// , 1
    71  	//   1
    72  	// 世 3
    73  	// 界 3
    74  }
    75  
    76  func ExampleDecodeRuneInString() {
    77  	str := "Hello, 世界"
    78  
    79  	for len(str) > 0 {
    80  		r, size := utf8.DecodeRuneInString(str)
    81  		fmt.Printf("%c %v\n", r, size)
    82  
    83  		str = str[size:]
    84  	}
    85  	// Output:
    86  	// H 1
    87  	// e 1
    88  	// l 1
    89  	// l 1
    90  	// o 1
    91  	// , 1
    92  	//   1
    93  	// 世 3
    94  	// 界 3
    95  }
    96  
    97  func ExampleEncodeRune() {
    98  	r := '世'
    99  	buf := make([]byte, 3)
   100  
   101  	n := utf8.EncodeRune(buf, r)
   102  
   103  	fmt.Println(buf)
   104  	fmt.Println(n)
   105  	// Output:
   106  	// [228 184 150]
   107  	// 3
   108  }
   109  
   110  func ExampleFullRune() {
   111  	buf := []byte{228, 184, 150} // 世
   112  	fmt.Println(utf8.FullRune(buf))
   113  	fmt.Println(utf8.FullRune(buf[:2]))
   114  	// Output:
   115  	// true
   116  	// false
   117  }
   118  
   119  func ExampleFullRuneInString() {
   120  	str := "世"
   121  	fmt.Println(utf8.FullRuneInString(str))
   122  	fmt.Println(utf8.FullRuneInString(str[:2]))
   123  	// Output:
   124  	// true
   125  	// false
   126  }
   127  
   128  func ExampleRuneCount() {
   129  	buf := []byte("Hello, 世界")
   130  	fmt.Println("bytes =", len(buf))
   131  	fmt.Println("runes =", utf8.RuneCount(buf))
   132  	// Output:
   133  	// bytes = 13
   134  	// runes = 9
   135  }
   136  
   137  func ExampleRuneCountInString() {
   138  	str := "Hello, 世界"
   139  	fmt.Println("bytes =", len(str))
   140  	fmt.Println("runes =", utf8.RuneCountInString(str))
   141  	// Output:
   142  	// bytes = 13
   143  	// runes = 9
   144  }
   145  
   146  func ExampleRuneLen() {
   147  	fmt.Println(utf8.RuneLen('a'))
   148  	fmt.Println(utf8.RuneLen('界'))
   149  	// Output:
   150  	// 1
   151  	// 3
   152  }
   153  
   154  func ExampleRuneStart() {
   155  	buf := []byte("a界")
   156  	fmt.Println(utf8.RuneStart(buf[0]))
   157  	fmt.Println(utf8.RuneStart(buf[1]))
   158  	fmt.Println(utf8.RuneStart(buf[2]))
   159  	// Output:
   160  	// true
   161  	// true
   162  	// false
   163  }
   164  
   165  func ExampleValid() {
   166  	valid := []byte("Hello, 世界")
   167  	invalid := []byte{0xff, 0xfe, 0xfd}
   168  
   169  	fmt.Println(utf8.Valid(valid))
   170  	fmt.Println(utf8.Valid(invalid))
   171  	// Output:
   172  	// true
   173  	// false
   174  }
   175  
   176  func ExampleValidRune() {
   177  	valid := 'a'
   178  	invalid := rune(0xfffffff)
   179  
   180  	fmt.Println(utf8.ValidRune(valid))
   181  	fmt.Println(utf8.ValidRune(invalid))
   182  	// Output:
   183  	// true
   184  	// false
   185  }
   186  
   187  func ExampleValidString() {
   188  	valid := "Hello, 世界"
   189  	invalid := string([]byte{0xff, 0xfe, 0xfd})
   190  
   191  	fmt.Println(utf8.ValidString(valid))
   192  	fmt.Println(utf8.ValidString(invalid))
   193  	// Output:
   194  	// true
   195  	// false
   196  }