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