github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/strings/example_test.gno (about)

     1  // Copyright 2012 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 strings_test
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"unicode"
    11  )
    12  
    13  func ExampleFields() {
    14  	fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
    15  	// Output: Fields are: ["foo" "bar" "baz"]
    16  }
    17  
    18  func ExampleFieldsFunc() {
    19  	f := func(c rune) bool {
    20  		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
    21  	}
    22  	fmt.Printf("Fields are: %q", strings.FieldsFunc("  foo1;bar2,baz3...", f))
    23  	// Output: Fields are: ["foo1" "bar2" "baz3"]
    24  }
    25  
    26  func ExampleCompare() {
    27  	fmt.Println(strings.Compare("a", "b"))
    28  	fmt.Println(strings.Compare("a", "a"))
    29  	fmt.Println(strings.Compare("b", "a"))
    30  	// Output:
    31  	// -1
    32  	// 0
    33  	// 1
    34  }
    35  
    36  func ExampleContains() {
    37  	fmt.Println(strings.Contains("seafood", "foo"))
    38  	fmt.Println(strings.Contains("seafood", "bar"))
    39  	fmt.Println(strings.Contains("seafood", ""))
    40  	fmt.Println(strings.Contains("", ""))
    41  	// Output:
    42  	// true
    43  	// false
    44  	// true
    45  	// true
    46  }
    47  
    48  func ExampleContainsAny() {
    49  	fmt.Println(strings.ContainsAny("team", "i"))
    50  	fmt.Println(strings.ContainsAny("fail", "ui"))
    51  	fmt.Println(strings.ContainsAny("ure", "ui"))
    52  	fmt.Println(strings.ContainsAny("failure", "ui"))
    53  	fmt.Println(strings.ContainsAny("foo", ""))
    54  	fmt.Println(strings.ContainsAny("", ""))
    55  	// Output:
    56  	// false
    57  	// true
    58  	// true
    59  	// true
    60  	// false
    61  	// false
    62  }
    63  
    64  func ExampleContainsRune() {
    65  	// Finds whether a string contains a particular Unicode code point.
    66  	// The code point for the lowercase letter "a", for example, is 97.
    67  	fmt.Println(strings.ContainsRune("aardvark", 97))
    68  	fmt.Println(strings.ContainsRune("timeout", 97))
    69  	// Output:
    70  	// true
    71  	// false
    72  }
    73  
    74  func ExampleCount() {
    75  	fmt.Println(strings.Count("cheese", "e"))
    76  	fmt.Println(strings.Count("five", "")) // before & after each rune
    77  	// Output:
    78  	// 3
    79  	// 5
    80  }
    81  
    82  func ExampleEqualFold() {
    83  	fmt.Println(strings.EqualFold("Go", "go"))
    84  	// Output: true
    85  }
    86  
    87  func ExampleHasPrefix() {
    88  	fmt.Println(strings.HasPrefix("Gopher", "Go"))
    89  	fmt.Println(strings.HasPrefix("Gopher", "C"))
    90  	fmt.Println(strings.HasPrefix("Gopher", ""))
    91  	// Output:
    92  	// true
    93  	// false
    94  	// true
    95  }
    96  
    97  func ExampleHasSuffix() {
    98  	fmt.Println(strings.HasSuffix("Amigo", "go"))
    99  	fmt.Println(strings.HasSuffix("Amigo", "O"))
   100  	fmt.Println(strings.HasSuffix("Amigo", "Ami"))
   101  	fmt.Println(strings.HasSuffix("Amigo", ""))
   102  	// Output:
   103  	// true
   104  	// false
   105  	// false
   106  	// true
   107  }
   108  
   109  func ExampleIndex() {
   110  	fmt.Println(strings.Index("chicken", "ken"))
   111  	fmt.Println(strings.Index("chicken", "dmr"))
   112  	// Output:
   113  	// 4
   114  	// -1
   115  }
   116  
   117  func ExampleIndexFunc() {
   118  	f := func(c rune) bool {
   119  		return unicode.Is(unicode.Han, c)
   120  	}
   121  	fmt.Println(strings.IndexFunc("Hello, 世界", f))
   122  	fmt.Println(strings.IndexFunc("Hello, world", f))
   123  	// Output:
   124  	// 7
   125  	// -1
   126  }
   127  
   128  func ExampleIndexAny() {
   129  	fmt.Println(strings.IndexAny("chicken", "aeiouy"))
   130  	fmt.Println(strings.IndexAny("crwth", "aeiouy"))
   131  	// Output:
   132  	// 2
   133  	// -1
   134  }
   135  
   136  func ExampleIndexByte() {
   137  	fmt.Println(strings.IndexByte("golang", 'g'))
   138  	fmt.Println(strings.IndexByte("gophers", 'h'))
   139  	fmt.Println(strings.IndexByte("golang", 'x'))
   140  	// Output:
   141  	// 0
   142  	// 3
   143  	// -1
   144  }
   145  
   146  func ExampleIndexRune() {
   147  	fmt.Println(strings.IndexRune("chicken", 'k'))
   148  	fmt.Println(strings.IndexRune("chicken", 'd'))
   149  	// Output:
   150  	// 4
   151  	// -1
   152  }
   153  
   154  func ExampleLastIndex() {
   155  	fmt.Println(strings.Index("go gopher", "go"))
   156  	fmt.Println(strings.LastIndex("go gopher", "go"))
   157  	fmt.Println(strings.LastIndex("go gopher", "rodent"))
   158  	// Output:
   159  	// 0
   160  	// 3
   161  	// -1
   162  }
   163  
   164  func ExampleLastIndexAny() {
   165  	fmt.Println(strings.LastIndexAny("go gopher", "go"))
   166  	fmt.Println(strings.LastIndexAny("go gopher", "rodent"))
   167  	fmt.Println(strings.LastIndexAny("go gopher", "fail"))
   168  	// Output:
   169  	// 4
   170  	// 8
   171  	// -1
   172  }
   173  
   174  func ExampleLastIndexByte() {
   175  	fmt.Println(strings.LastIndexByte("Hello, world", 'l'))
   176  	fmt.Println(strings.LastIndexByte("Hello, world", 'o'))
   177  	fmt.Println(strings.LastIndexByte("Hello, world", 'x'))
   178  	// Output:
   179  	// 10
   180  	// 8
   181  	// -1
   182  }
   183  
   184  func ExampleLastIndexFunc() {
   185  	fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber))
   186  	fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber))
   187  	fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber))
   188  	// Output:
   189  	// 5
   190  	// 2
   191  	// -1
   192  }
   193  
   194  func ExampleJoin() {
   195  	s := []string{"foo", "bar", "baz"}
   196  	fmt.Println(strings.Join(s, ", "))
   197  	// Output: foo, bar, baz
   198  }
   199  
   200  func ExampleRepeat() {
   201  	fmt.Println("ba" + strings.Repeat("na", 2))
   202  	// Output: banana
   203  }
   204  
   205  func ExampleReplace() {
   206  	fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
   207  	fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
   208  	// Output:
   209  	// oinky oinky oink
   210  	// moo moo moo
   211  }
   212  
   213  func ExampleReplaceAll() {
   214  	fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
   215  	// Output:
   216  	// moo moo moo
   217  }
   218  
   219  func ExampleSplit() {
   220  	fmt.Printf("%q\n", strings.Split("a,b,c", ","))
   221  	fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
   222  	fmt.Printf("%q\n", strings.Split(" xyz ", ""))
   223  	fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
   224  	// Output:
   225  	// ["a" "b" "c"]
   226  	// ["" "man " "plan " "canal panama"]
   227  	// [" " "x" "y" "z" " "]
   228  	// [""]
   229  }
   230  
   231  func ExampleSplitN() {
   232  	fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
   233  	z := strings.SplitN("a,b,c", ",", 0)
   234  	fmt.Printf("%q (nil = %v)\n", z, z == nil)
   235  	// Output:
   236  	// ["a" "b,c"]
   237  	// [] (nil = true)
   238  }
   239  
   240  func ExampleSplitAfter() {
   241  	fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
   242  	// Output: ["a," "b," "c"]
   243  }
   244  
   245  func ExampleSplitAfterN() {
   246  	fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
   247  	// Output: ["a," "b,c"]
   248  }
   249  
   250  func ExampleTitle() {
   251  	// Compare this example to the ToTitle example.
   252  	fmt.Println(strings.Title("her royal highness"))
   253  	fmt.Println(strings.Title("loud noises"))
   254  	fmt.Println(strings.Title("хлеб"))
   255  	// Output:
   256  	// Her Royal Highness
   257  	// Loud Noises
   258  	// Хлеб
   259  }
   260  
   261  func ExampleToTitle() {
   262  	// Compare this example to the Title example.
   263  	fmt.Println(strings.ToTitle("her royal highness"))
   264  	fmt.Println(strings.ToTitle("loud noises"))
   265  	fmt.Println(strings.ToTitle("хлеб"))
   266  	// Output:
   267  	// HER ROYAL HIGHNESS
   268  	// LOUD NOISES
   269  	// ХЛЕБ
   270  }
   271  
   272  func ExampleToTitleSpecial() {
   273  	fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
   274  	// Output:
   275  	// DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR
   276  }
   277  
   278  func ExampleMap() {
   279  	rot13 := func(r rune) rune {
   280  		switch {
   281  		case r >= 'A' && r <= 'Z':
   282  			return 'A' + (r-'A'+13)%26
   283  		case r >= 'a' && r <= 'z':
   284  			return 'a' + (r-'a'+13)%26
   285  		}
   286  		return r
   287  	}
   288  	fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
   289  	// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
   290  }
   291  
   292  /* XXX replacer removed
   293  func ExampleNewReplacer() {
   294  	r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
   295  	fmt.Println(r.Replace("This is <b>HTML</b>!"))
   296  	// Output: This is &lt;b&gt;HTML&lt;/b&gt;!
   297  }
   298  */
   299  
   300  func ExampleToUpper() {
   301  	fmt.Println(strings.ToUpper("Gopher"))
   302  	// Output: GOPHER
   303  }
   304  
   305  func ExampleToUpperSpecial() {
   306  	fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
   307  	// Output: ÖRNEK İŞ
   308  }
   309  
   310  func ExampleToLower() {
   311  	fmt.Println(strings.ToLower("Gopher"))
   312  	// Output: gopher
   313  }
   314  
   315  func ExampleToLowerSpecial() {
   316  	fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
   317  	// Output: önnek iş
   318  }
   319  
   320  func ExampleTrim() {
   321  	fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
   322  	// Output: Hello, Gophers
   323  }
   324  
   325  func ExampleTrimSpace() {
   326  	fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
   327  	// Output: Hello, Gophers
   328  }
   329  
   330  func ExampleTrimPrefix() {
   331  	s := "¡¡¡Hello, Gophers!!!"
   332  	s = strings.TrimPrefix(s, "¡¡¡Hello, ")
   333  	s = strings.TrimPrefix(s, "¡¡¡Howdy, ")
   334  	fmt.Print(s)
   335  	// Output: Gophers!!!
   336  }
   337  
   338  func ExampleTrimSuffix() {
   339  	s := "¡¡¡Hello, Gophers!!!"
   340  	s = strings.TrimSuffix(s, ", Gophers!!!")
   341  	s = strings.TrimSuffix(s, ", Marmots!!!")
   342  	fmt.Print(s)
   343  	// Output: ¡¡¡Hello
   344  }
   345  
   346  func ExampleTrimFunc() {
   347  	fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
   348  		return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   349  	}))
   350  	// Output: Hello, Gophers
   351  }
   352  
   353  func ExampleTrimLeft() {
   354  	fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
   355  	// Output: Hello, Gophers!!!
   356  }
   357  
   358  func ExampleTrimLeftFunc() {
   359  	fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
   360  		return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   361  	}))
   362  	// Output: Hello, Gophers!!!
   363  }
   364  
   365  func ExampleTrimRight() {
   366  	fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
   367  	// Output: ¡¡¡Hello, Gophers
   368  }
   369  
   370  func ExampleTrimRightFunc() {
   371  	fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
   372  		return !unicode.IsLetter(r) && !unicode.IsNumber(r)
   373  	}))
   374  	// Output: ¡¡¡Hello, Gophers
   375  }
   376  
   377  /* XXX Builder not yet supported
   378  func ExampleBuilder() {
   379  	var b strings.Builder
   380  	for i := 3; i >= 1; i-- {
   381  		fmt.Fprintf(&b, "%d...", i)
   382  	}
   383  	b.WriteString("ignition")
   384  	fmt.Println(b.String())
   385  
   386  	// Output: 3...2...1...ignition
   387  }
   388  */