1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
package asciiset_test
import (
"fmt"
"github.com/elliotwutingfeng/asciiset"
)
// Basic asciiset operations
func Example_basics() {
// Making an ASCIISet
// No need to ensure that all characters in chars are unique
chars := "3gqZ1mAhVcA#Z7eKvwPN8J@D"
as, ok := asciiset.MakeASCIISet(chars)
if ok {
fmt.Println("as created")
}
// Character not in ASCIISet
if !as.Contains('n') {
fmt.Println("as does not contain 'n'")
}
// Adding character to ASCIISet
as.Add('b')
// Character is in ASCIISet
if as.Contains('b') {
fmt.Println("as contains 'b'")
}
// Attempting to add same character to ASCIISet again will not return an error
// and the ASCIISet contents will remain unchanged
as.Add('b')
// Adding non-ASCII byte characters will fail silently
britishPound := byte('£') // this is not an ASCII character
as.Add(britishPound)
if !as.Contains(britishPound) {
fmt.Printf("as does not contain %s\n", string(britishPound))
}
// Removing character from ASCIISet
as.Remove('3')
if !as.Contains('3') {
fmt.Println("as does not contain 3")
}
// Attempting to remove same character from ASCIISet again will not return an error
// and the ASCIISet contents will remain unchanged
as.Remove('3')
// Getting size of ASCIISet
fmt.Println(as.Size())
// Output: as created
// as does not contain 'n'
// as contains 'b'
// as does not contain £
// as does not contain 3
// 22
}
// Operations involving multiple sets
func Example_multiple_sets() {
as, _ := asciiset.MakeASCIISet("ABCD")
as2, _ := asciiset.MakeASCIISet("CDEF")
expectedUnion, _ := asciiset.MakeASCIISet("ABCDEF")
expectedIntersection, _ := asciiset.MakeASCIISet("CD")
expectedSubtract, _ := asciiset.MakeASCIISet("AB")
union := as.Union(as2)
if union.Equals(expectedUnion) {
fmt.Println(`Union of as and as2 is "ABCDEF"`)
}
intersection := as.Intersection(as2)
if intersection.Equals(expectedIntersection) {
fmt.Println(`Intersection of as and as2 is "CD"`)
}
subtract := as.Subtract(as2)
if subtract.Equals(expectedSubtract) {
fmt.Println(`Subtraction of as2 from as is "AB"`)
}
fmt.Printf("Content of as is \"")
as.Visit(func(n byte) bool {
fmt.Printf("%c", n)
return false
})
fmt.Println(`"`)
fmt.Printf(`Content of as2 up to character 'E' is "`)
as2.Visit(func(n byte) bool {
fmt.Printf("%c", n)
if n == 'E' {
return true
}
return false
})
fmt.Println(`"`)
// Output: Union of as and as2 is "ABCDEF"
// Intersection of as and as2 is "CD"
// Subtraction of as2 from as is "AB"
// Content of as is "ABCD"
// Content of as2 up to character 'E' is "CDE"
}
|