github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/bufio/example_test.gno (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 bufio_test
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"os"
    11  	"strconv"
    12  	"strings"
    13  )
    14  
    15  /* XXX fmt.Fprint cannot be satisfied.
    16  func ExampleWriter() {
    17  	w := bufio.NewWriter(os.Stdout)
    18  	fmt.Fprint(w, "Hello, ")
    19  	fmt.Fprint(w, "world!")
    20  	w.Flush() // Don't forget to flush!
    21  	// Output: Hello, world!
    22  }
    23  */
    24  
    25  // The simplest use of a Scanner, to read standard input as a set of lines.
    26  func ExampleScanner_lines() {
    27  	scanner := bufio.NewScanner(os.Stdin)
    28  	for scanner.Scan() {
    29  		fmt.Println(scanner.Text()) // Println will add back the final '\n'
    30  	}
    31  	if err := scanner.Err(); err != nil {
    32  		fmt.Fprintln(os.Stderr, "reading standard input:", err)
    33  	}
    34  }
    35  
    36  // Return the most recent call to Scan as a []byte.
    37  func ExampleScanner_Bytes() {
    38  	scanner := bufio.NewScanner(strings.NewReader("gopher"))
    39  	for scanner.Scan() {
    40  		fmt.Println(len(scanner.Bytes()) == 6)
    41  	}
    42  	if err := scanner.Err(); err != nil {
    43  		fmt.Fprintln(os.Stderr, "shouldn't see an error scanning a string")
    44  	}
    45  	// Output:
    46  	// true
    47  }
    48  
    49  // Use a Scanner to implement a simple word-count utility by scanning the
    50  // input as a sequence of space-delimited tokens.
    51  func ExampleScanner_words() {
    52  	// An artificial input source.
    53  	const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
    54  	scanner := bufio.NewScanner(strings.NewReader(input))
    55  	// Set the split function for the scanning operation.
    56  	scanner.Split(bufio.ScanWords)
    57  	// Count the words.
    58  	count := 0
    59  	for scanner.Scan() {
    60  		count++
    61  	}
    62  	if err := scanner.Err(); err != nil {
    63  		fmt.Fprintln(os.Stderr, "reading input:", err)
    64  	}
    65  	fmt.Printf("%d\n", count)
    66  	// Output: 15
    67  }
    68  
    69  // Use a Scanner with a custom split function (built by wrapping ScanWords) to validate
    70  // 32-bit decimal input.
    71  func ExampleScanner_custom() {
    72  	// An artificial input source.
    73  	const input = "1234 5678 1234567901234567890"
    74  	scanner := bufio.NewScanner(strings.NewReader(input))
    75  	// Create a custom split function by wrapping the existing ScanWords function.
    76  	split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
    77  		advance, token, err = bufio.ScanWords(data, atEOF)
    78  		if err == nil && token != nil {
    79  			_, err = strconv.ParseInt(string(token), 10, 32)
    80  		}
    81  		return
    82  	}
    83  	// Set the split function for the scanning operation.
    84  	scanner.Split(split)
    85  	// Validate the input
    86  	for scanner.Scan() {
    87  		fmt.Printf("%s\n", scanner.Text())
    88  	}
    89  
    90  	if err := scanner.Err(); err != nil {
    91  		fmt.Printf("Invalid input: %s", err)
    92  	}
    93  	// Output:
    94  	// 1234
    95  	// 5678
    96  	// Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range
    97  }
    98  
    99  // Use a Scanner with a custom split function to parse a comma-separated
   100  // list with an empty final value.
   101  func ExampleScanner_emptyFinalToken() {
   102  	// Comma-separated list; last entry is empty.
   103  	const input = "1,2,3,4,"
   104  	scanner := bufio.NewScanner(strings.NewReader(input))
   105  	// Define a split function that separates on commas.
   106  	onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
   107  		for i := 0; i < len(data); i++ {
   108  			if data[i] == ',' {
   109  				return i + 1, data[:i], nil
   110  			}
   111  		}
   112  		if !atEOF {
   113  			return 0, nil, nil
   114  		}
   115  		// There is one final token to be delivered, which may be the empty string.
   116  		// Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
   117  		// but does not trigger an error to be returned from Scan itself.
   118  		return 0, data, bufio.ErrFinalToken
   119  	}
   120  	scanner.Split(onComma)
   121  	// Scan.
   122  	for scanner.Scan() {
   123  		fmt.Printf("%q ", scanner.Text())
   124  	}
   125  	if err := scanner.Err(); err != nil {
   126  		fmt.Fprintln(os.Stderr, "reading input:", err)
   127  	}
   128  	// Output: "1" "2" "3" "4" ""
   129  }