github.com/creachadair/ffs@v0.17.3/block/example_test.go (about) 1 // Copyright 2019 Michael J. Fromberger. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package block_test 16 17 import ( 18 "fmt" 19 "log" 20 "strings" 21 22 "github.com/creachadair/ffs/block" 23 ) 24 25 func Example() { 26 // Note that these two strings are similar, but the second one has been 27 // edited. The edit changes the block splits around the point of the edit, 28 // but the sliding window allows them to resynchronize after the break. 29 const input1 = `abcdefg-hijklmnop-qrstuv-wxyz-abcdefg-hijklmnop-qrstuv-wxyz-abcdefghijklmnopqrstuv` 30 const input2 = `abcdefg-hijklmnop-qrstuv-wxyz-*-abcdefg-hijklmnop-qrstuv-wxyz-abcdefghijklmnopqrstuv` 31 32 opts := &block.SplitConfig{ 33 Min: 5, // no blocks shorter than this 34 Size: 10, // desired mean block size 35 Max: 20, // no blocks longer than this 36 Hasher: block.RabinKarpHasher(23, 997, 13), 37 } 38 39 for _, v := range []string{input1, input2} { 40 s := block.NewSplitter(strings.NewReader(v), opts) 41 var i int 42 if err := s.Split(func(data []byte) error { 43 i++ 44 fmt.Printf("%d. %s\n", i, string(data)) 45 return nil 46 }); err != nil { 47 log.Fatal(err) 48 } 49 fmt.Println() 50 } 51 52 // Output: 53 // 54 // 1. abcdefg-h 55 // 2. ijklmnop-qrstu 56 // 3. v-wxyz-abcdefg 57 // 4. -hijklmnop-qrstu 58 // 5. v-wxyz-abcdefghijklm 59 // 6. nopqrstuv 60 // 61 // 1. abcdefg-h 62 // 2. ijklmnop-qrstu 63 // 3. v-wxyz-*- 64 // 4. abcdefg-hi 65 // 5. jklmnop-qrstu 66 // 6. v-wxyz-abcdefghijklm 67 // 7. nopqrstuv 68 }