├── .gitignore ├── .travis.yml ├── APACHE-LICENSE-2.0 ├── AUTHORS ├── CONTRIBUTORS ├── LICENSE ├── Makefile ├── README.md ├── diffmatchpatch ├── benchutil_test.go ├── diff.go ├── diff_test.go ├── diffmatchpatch.go ├── index.go ├── index_test.go ├── match.go ├── match_test.go ├── mathutil.go ├── operation_string.go ├── patch.go ├── patch_test.go ├── stringutil.go └── stringutil_test.go ├── go.mod ├── go.sum ├── scripts └── lint.sh └── testdata ├── diff10klinestest.txt ├── fixture.go ├── speedtest1.txt └── speedtest2.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | arch: 3 | - amd64 4 | - ppc64le 5 | os: 6 | - linux 7 | - osx 8 | 9 | go: 10 | - 1.13 11 | - 1.14 12 | - 1.15 13 | 14 | sudo: false 15 | 16 | env: 17 | global: 18 | # Coveralls.io 19 | - secure: OGYOsFNXNarEZ5yA4/M6ZdVguD0jL8vXgXrbLzjcpkKcq8ObHSCtNINoUlnNf6l6Z92kPnuV+LSm7jKTojBlov4IwgiY1ACbvg921SdjxYkg1AiwHTRTLR1g/esX8RdaBpJ0TOcXOFFsYMRVvl5sxxtb0tXSuUrT+Ch4SUCY7X8= 20 | 21 | install: 22 | - make install-dependencies 23 | - make install-tools 24 | - make install 25 | 26 | script: 27 | - make lint 28 | - make test-with-coverage 29 | - gover 30 | - if [ "$TRAVIS_SECURE_ENV_VARS" = "true" ]; then goveralls -coverprofile=gover.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN; fi 31 | -------------------------------------------------------------------------------- /APACHE-LICENSE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the official list of go-diff authors for copyright purposes. 2 | # This file is distinct from the CONTRIBUTORS files. 3 | # See the latter for an explanation. 4 | 5 | # Names should be added to this file as 6 | # Name or Organization 7 | # The email address is not required for organizations. 8 | 9 | # Please keep the list sorted. 10 | 11 | Danny Yoo 12 | James Kolb 13 | Jonathan Amsterdam 14 | Markus Zimmermann 15 | Matt Kovars 16 | Örjan Persson 17 | Osman Masood 18 | Robert Carlsen 19 | Rory Flynn 20 | Sergi Mansilla 21 | Shatrugna Sadhu 22 | Shawn Smith 23 | Stas Maksimov 24 | Tor Arvid Lund 25 | Zac Bergquist 26 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This is the official list of people who can contribute 2 | # (and typically have contributed) code to the go-diff 3 | # repository. 4 | # 5 | # The AUTHORS file lists the copyright holders; this file 6 | # lists people. For example, ACME Inc. employees would be listed here 7 | # but not in AUTHORS, because ACME Inc. would hold the copyright. 8 | # 9 | # When adding J Random Contributor's name to this file, 10 | # either J's name or J's organization's name should be 11 | # added to the AUTHORS file. 12 | # 13 | # Names should be added to this file like so: 14 | # Name 15 | # 16 | # Please keep the list sorted. 17 | 18 | Danny Yoo 19 | James Kolb 20 | Jonathan Amsterdam 21 | Markus Zimmermann 22 | Matt Kovars 23 | Örjan Persson 24 | Osman Masood 25 | Robert Carlsen 26 | Rory Flynn 27 | Sergi Mansilla 28 | Shatrugna Sadhu 29 | Shawn Smith 30 | Stas Maksimov 31 | Tor Arvid Lund 32 | Zac Bergquist 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2016 The go-diff Authors. All rights reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included 11 | in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean clean-coverage install install-dependencies install-tools lint test test-verbose test-with-coverage 2 | 3 | export ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) 4 | export PKG := github.com/sergi/go-diff 5 | export ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) 6 | 7 | $(eval $(ARGS):;@:) # turn arguments into do-nothing targets 8 | export ARGS 9 | 10 | ifdef ARGS 11 | PKG_TEST := $(ARGS) 12 | else 13 | PKG_TEST := $(PKG)/... 14 | endif 15 | 16 | all: install-tools install-dependencies install lint test 17 | 18 | clean: 19 | go clean -i $(PKG)/... 20 | go clean -i -race $(PKG)/... 21 | clean-coverage: 22 | find $(ROOT_DIR) | grep .coverprofile | xargs rm 23 | install: 24 | go install -v $(PKG)/... 25 | install-dependencies: 26 | go get -t -v $(PKG)/... 27 | go build -v $(PKG)/... 28 | install-tools: 29 | # Install linting tools 30 | go get -u -v golang.org/x/lint/... 31 | go get -u -v github.com/kisielk/errcheck/... 32 | 33 | # Install code coverage tools 34 | go get -u -v github.com/onsi/ginkgo/ginkgo/... 35 | go get -u -v github.com/modocache/gover/... 36 | go get -u -v github.com/mattn/goveralls/... 37 | lint: 38 | $(ROOT_DIR)/scripts/lint.sh 39 | test: 40 | go test -race -test.timeout 120s $(PKG_TEST) 41 | test-verbose: 42 | go test -race -test.timeout 120s -v $(PKG_TEST) 43 | test-with-coverage: 44 | ginkgo -r -cover -race -skipPackage="testdata" 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-diff [![GoDoc](https://godoc.org/github.com/sergi/go-diff?status.png)](https://godoc.org/github.com/sergi/go-diff/diffmatchpatch) [![Build Status](https://travis-ci.org/sergi/go-diff.svg?branch=master)](https://travis-ci.org/sergi/go-diff) [![Coverage Status](https://coveralls.io/repos/sergi/go-diff/badge.png?branch=master)](https://coveralls.io/r/sergi/go-diff?branch=master) 2 | 3 | go-diff offers algorithms to perform operations required for synchronizing plain text: 4 | 5 | - Compare two texts and return their differences. 6 | - Perform fuzzy matching of text. 7 | - Apply patches onto text. 8 | 9 | ## Installation 10 | 11 | ```bash 12 | go get -u github.com/sergi/go-diff/... 13 | ``` 14 | 15 | ## Usage 16 | 17 | The following example compares two texts and writes out the differences to standard output. 18 | 19 | ```go 20 | package main 21 | 22 | import ( 23 | "fmt" 24 | 25 | "github.com/sergi/go-diff/diffmatchpatch" 26 | ) 27 | 28 | const ( 29 | text1 = "Lorem ipsum dolor." 30 | text2 = "Lorem dolor sit amet." 31 | ) 32 | 33 | func main() { 34 | dmp := diffmatchpatch.New() 35 | 36 | diffs := dmp.DiffMain(text1, text2, false) 37 | 38 | fmt.Println(dmp.DiffPrettyText(diffs)) 39 | } 40 | ``` 41 | 42 | ## Found a bug or are you missing a feature in go-diff? 43 | 44 | Please make sure to have the latest version of go-diff. If the problem still persists go through the [open issues](https://github.com/sergi/go-diff/issues) in the tracker first. If you cannot find your request just open up a [new issue](https://github.com/sergi/go-diff/issues/new). 45 | 46 | ## How to contribute? 47 | 48 | You want to contribute to go-diff? GREAT! If you are here because of a bug you want to fix or a feature you want to add, you can just read on. Otherwise we have a list of [open issues in the tracker](https://github.com/sergi/go-diff/issues). Just choose something you think you can work on and discuss your plans in the issue by commenting on it. 49 | 50 | Please make sure that every behavioral change is accompanied by test cases. Additionally, every contribution must pass the `lint` and `test` Makefile targets which can be run using the following commands in the repository root directory. 51 | 52 | ```bash 53 | make lint 54 | make test 55 | ``` 56 | 57 | After your contribution passes these commands, [create a PR](https://help.github.com/articles/creating-a-pull-request/) and we will review your contribution. 58 | 59 | ## Origins 60 | 61 | go-diff is a Go language port of Neil Fraser's google-diff-match-patch code. His original code is available at [http://code.google.com/p/google-diff-match-patch/](http://code.google.com/p/google-diff-match-patch/). 62 | 63 | ## Copyright and License 64 | 65 | The original Google Diff, Match and Patch Library is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). The full terms of that license are included here in the [APACHE-LICENSE-2.0](/APACHE-LICENSE-2.0) file. 66 | 67 | Diff, Match and Patch Library 68 | 69 | > Written by Neil Fraser 70 | > Copyright (c) 2006 Google Inc. 71 | > 72 | 73 | This Go version of Diff, Match and Patch Library is licensed under the [MIT License](http://www.opensource.org/licenses/MIT) (a.k.a. the Expat License) which is included here in the [LICENSE](/LICENSE) file. 74 | 75 | Go version of Diff, Match and Patch Library 76 | 77 | > Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 78 | > 79 | 80 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 81 | 82 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 83 | 84 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 85 | -------------------------------------------------------------------------------- /diffmatchpatch/benchutil_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "io/ioutil" 13 | ) 14 | 15 | const testdataPath = "../testdata/" 16 | 17 | func speedtestTexts() (s1 string, s2 string) { 18 | d1, err := ioutil.ReadFile(testdataPath + "speedtest1.txt") 19 | if err != nil { 20 | panic(err) 21 | } 22 | d2, err := ioutil.ReadFile(testdataPath + "speedtest2.txt") 23 | if err != nil { 24 | panic(err) 25 | } 26 | 27 | return string(d1), string(d2) 28 | } 29 | -------------------------------------------------------------------------------- /diffmatchpatch/diff.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "bytes" 13 | "errors" 14 | "fmt" 15 | "html" 16 | "math" 17 | "net/url" 18 | "regexp" 19 | "strconv" 20 | "strings" 21 | "time" 22 | "unicode/utf8" 23 | ) 24 | 25 | // Operation defines the operation of a diff item. 26 | type Operation int8 27 | 28 | //go:generate stringer -type=Operation -trimprefix=Diff 29 | 30 | const ( 31 | // DiffDelete item represents a delete diff. 32 | DiffDelete Operation = -1 33 | // DiffInsert item represents an insert diff. 34 | DiffInsert Operation = 1 35 | // DiffEqual item represents an equal diff. 36 | DiffEqual Operation = 0 37 | ) 38 | 39 | // Diff represents one diff operation 40 | type Diff struct { 41 | Type Operation 42 | Text string 43 | } 44 | 45 | // splice removes amount elements from slice at index index, replacing them with elements. 46 | func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff { 47 | if len(elements) == amount { 48 | // Easy case: overwrite the relevant items. 49 | copy(slice[index:], elements) 50 | return slice 51 | } 52 | if len(elements) < amount { 53 | // Fewer new items than old. 54 | // Copy in the new items. 55 | copy(slice[index:], elements) 56 | // Shift the remaining items left. 57 | copy(slice[index+len(elements):], slice[index+amount:]) 58 | // Calculate the new end of the slice. 59 | end := len(slice) - amount + len(elements) 60 | // Zero stranded elements at end so that they can be garbage collected. 61 | tail := slice[end:] 62 | for i := range tail { 63 | tail[i] = Diff{} 64 | } 65 | return slice[:end] 66 | } 67 | // More new items than old. 68 | // Make room in slice for new elements. 69 | // There's probably an even more efficient way to do this, 70 | // but this is simple and clear. 71 | need := len(slice) - amount + len(elements) 72 | for len(slice) < need { 73 | slice = append(slice, Diff{}) 74 | } 75 | // Shift slice elements right to make room for new elements. 76 | copy(slice[index+len(elements):], slice[index+amount:]) 77 | // Copy in new elements. 78 | copy(slice[index:], elements) 79 | return slice 80 | } 81 | 82 | // DiffMain finds the differences between two texts. 83 | // If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. 84 | func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff { 85 | return dmp.DiffMainRunes([]rune(text1), []rune(text2), checklines) 86 | } 87 | 88 | // DiffMainRunes finds the differences between two rune sequences. 89 | // If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. 90 | func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff { 91 | var deadline time.Time 92 | if dmp.DiffTimeout > 0 { 93 | deadline = time.Now().Add(dmp.DiffTimeout) 94 | } 95 | return dmp.diffMainRunes(text1, text2, checklines, deadline) 96 | } 97 | 98 | func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, deadline time.Time) []Diff { 99 | if runesEqual(text1, text2) { 100 | var diffs []Diff 101 | if len(text1) > 0 { 102 | diffs = append(diffs, Diff{DiffEqual, string(text1)}) 103 | } 104 | return diffs 105 | } 106 | // Trim off common prefix (speedup). 107 | commonlength := commonPrefixLength(text1, text2) 108 | commonprefix := text1[:commonlength] 109 | text1 = text1[commonlength:] 110 | text2 = text2[commonlength:] 111 | 112 | // Trim off common suffix (speedup). 113 | commonlength = commonSuffixLength(text1, text2) 114 | commonsuffix := text1[len(text1)-commonlength:] 115 | text1 = text1[:len(text1)-commonlength] 116 | text2 = text2[:len(text2)-commonlength] 117 | 118 | // Compute the diff on the middle block. 119 | diffs := dmp.diffCompute(text1, text2, checklines, deadline) 120 | 121 | // Restore the prefix and suffix. 122 | if len(commonprefix) != 0 { 123 | diffs = append([]Diff{{DiffEqual, string(commonprefix)}}, diffs...) 124 | } 125 | if len(commonsuffix) != 0 { 126 | diffs = append(diffs, Diff{DiffEqual, string(commonsuffix)}) 127 | } 128 | 129 | return dmp.DiffCleanupMerge(diffs) 130 | } 131 | 132 | // diffCompute finds the differences between two rune slices. Assumes that the texts do not have any common prefix or suffix. 133 | func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, deadline time.Time) []Diff { 134 | diffs := []Diff{} 135 | if len(text1) == 0 { 136 | // Just add some text (speedup). 137 | return append(diffs, Diff{DiffInsert, string(text2)}) 138 | } else if len(text2) == 0 { 139 | // Just delete some text (speedup). 140 | return append(diffs, Diff{DiffDelete, string(text1)}) 141 | } 142 | 143 | var longtext, shorttext []rune 144 | if len(text1) > len(text2) { 145 | longtext = text1 146 | shorttext = text2 147 | } else { 148 | longtext = text2 149 | shorttext = text1 150 | } 151 | 152 | if i := runesIndex(longtext, shorttext); i != -1 { 153 | op := DiffInsert 154 | // Swap insertions for deletions if diff is reversed. 155 | if len(text1) > len(text2) { 156 | op = DiffDelete 157 | } 158 | // Shorter text is inside the longer text (speedup). 159 | return []Diff{ 160 | Diff{op, string(longtext[:i])}, 161 | Diff{DiffEqual, string(shorttext)}, 162 | Diff{op, string(longtext[i+len(shorttext):])}, 163 | } 164 | } else if len(shorttext) == 1 { 165 | // Single character string. 166 | // After the previous speedup, the character can't be an equality. 167 | return []Diff{ 168 | {DiffDelete, string(text1)}, 169 | {DiffInsert, string(text2)}, 170 | } 171 | // Check to see if the problem can be split in two. 172 | } else if hm := dmp.diffHalfMatch(text1, text2); hm != nil { 173 | // A half-match was found, sort out the return data. 174 | text1A := hm[0] 175 | text1B := hm[1] 176 | text2A := hm[2] 177 | text2B := hm[3] 178 | midCommon := hm[4] 179 | // Send both pairs off for separate processing. 180 | diffsA := dmp.diffMainRunes(text1A, text2A, checklines, deadline) 181 | diffsB := dmp.diffMainRunes(text1B, text2B, checklines, deadline) 182 | // Merge the results. 183 | diffs := diffsA 184 | diffs = append(diffs, Diff{DiffEqual, string(midCommon)}) 185 | diffs = append(diffs, diffsB...) 186 | return diffs 187 | } else if checklines && len(text1) > 100 && len(text2) > 100 { 188 | return dmp.diffLineMode(text1, text2, deadline) 189 | } 190 | return dmp.diffBisect(text1, text2, deadline) 191 | } 192 | 193 | // diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs. 194 | func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff { 195 | // Scan the text on a line-by-line basis first. 196 | text1, text2, linearray := dmp.DiffLinesToRunes(string(text1), string(text2)) 197 | 198 | diffs := dmp.diffMainRunes(text1, text2, false, deadline) 199 | 200 | // Convert the diff back to original text. 201 | diffs = dmp.DiffCharsToLines(diffs, linearray) 202 | // Eliminate freak matches (e.g. blank lines) 203 | diffs = dmp.DiffCleanupSemantic(diffs) 204 | 205 | // Rediff any replacement blocks, this time character-by-character. 206 | // Add a dummy entry at the end. 207 | diffs = append(diffs, Diff{DiffEqual, ""}) 208 | 209 | pointer := 0 210 | countDelete := 0 211 | countInsert := 0 212 | 213 | // NOTE: Rune slices are slower than using strings in this case. 214 | textDelete := "" 215 | textInsert := "" 216 | 217 | for pointer < len(diffs) { 218 | switch diffs[pointer].Type { 219 | case DiffInsert: 220 | countInsert++ 221 | textInsert += diffs[pointer].Text 222 | case DiffDelete: 223 | countDelete++ 224 | textDelete += diffs[pointer].Text 225 | case DiffEqual: 226 | // Upon reaching an equality, check for prior redundancies. 227 | if countDelete >= 1 && countInsert >= 1 { 228 | // Delete the offending records and add the merged ones. 229 | diffs = splice(diffs, pointer-countDelete-countInsert, 230 | countDelete+countInsert) 231 | 232 | pointer = pointer - countDelete - countInsert 233 | a := dmp.diffMainRunes([]rune(textDelete), []rune(textInsert), false, deadline) 234 | for j := len(a) - 1; j >= 0; j-- { 235 | diffs = splice(diffs, pointer, 0, a[j]) 236 | } 237 | pointer = pointer + len(a) 238 | } 239 | 240 | countInsert = 0 241 | countDelete = 0 242 | textDelete = "" 243 | textInsert = "" 244 | } 245 | pointer++ 246 | } 247 | 248 | return diffs[:len(diffs)-1] // Remove the dummy entry at the end. 249 | } 250 | 251 | // DiffBisect finds the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff. 252 | // If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character. 253 | // See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations. 254 | func (dmp *DiffMatchPatch) DiffBisect(text1, text2 string, deadline time.Time) []Diff { 255 | // Unused in this code, but retained for interface compatibility. 256 | return dmp.diffBisect([]rune(text1), []rune(text2), deadline) 257 | } 258 | 259 | // diffBisect finds the 'middle snake' of a diff, splits the problem in two and returns the recursively constructed diff. 260 | // See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations. 261 | func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time) []Diff { 262 | // Cache the text lengths to prevent multiple calls. 263 | runes1Len, runes2Len := len(runes1), len(runes2) 264 | 265 | maxD := (runes1Len + runes2Len + 1) / 2 266 | vOffset := maxD 267 | vLength := 2 * maxD 268 | 269 | v1 := make([]int, vLength) 270 | v2 := make([]int, vLength) 271 | for i := range v1 { 272 | v1[i] = -1 273 | v2[i] = -1 274 | } 275 | v1[vOffset+1] = 0 276 | v2[vOffset+1] = 0 277 | 278 | delta := runes1Len - runes2Len 279 | // If the total number of characters is odd, then the front path will collide with the reverse path. 280 | front := (delta%2 != 0) 281 | // Offsets for start and end of k loop. Prevents mapping of space beyond the grid. 282 | k1start := 0 283 | k1end := 0 284 | k2start := 0 285 | k2end := 0 286 | for d := 0; d < maxD; d++ { 287 | // Bail out if deadline is reached. 288 | if !deadline.IsZero() && d%16 == 0 && time.Now().After(deadline) { 289 | break 290 | } 291 | 292 | // Walk the front path one step. 293 | for k1 := -d + k1start; k1 <= d-k1end; k1 += 2 { 294 | k1Offset := vOffset + k1 295 | var x1 int 296 | 297 | if k1 == -d || (k1 != d && v1[k1Offset-1] < v1[k1Offset+1]) { 298 | x1 = v1[k1Offset+1] 299 | } else { 300 | x1 = v1[k1Offset-1] + 1 301 | } 302 | 303 | y1 := x1 - k1 304 | for x1 < runes1Len && y1 < runes2Len { 305 | if runes1[x1] != runes2[y1] { 306 | break 307 | } 308 | x1++ 309 | y1++ 310 | } 311 | v1[k1Offset] = x1 312 | if x1 > runes1Len { 313 | // Ran off the right of the graph. 314 | k1end += 2 315 | } else if y1 > runes2Len { 316 | // Ran off the bottom of the graph. 317 | k1start += 2 318 | } else if front { 319 | k2Offset := vOffset + delta - k1 320 | if k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] != -1 { 321 | // Mirror x2 onto top-left coordinate system. 322 | x2 := runes1Len - v2[k2Offset] 323 | if x1 >= x2 { 324 | // Overlap detected. 325 | return dmp.diffBisectSplit(runes1, runes2, x1, y1, deadline) 326 | } 327 | } 328 | } 329 | } 330 | // Walk the reverse path one step. 331 | for k2 := -d + k2start; k2 <= d-k2end; k2 += 2 { 332 | k2Offset := vOffset + k2 333 | var x2 int 334 | if k2 == -d || (k2 != d && v2[k2Offset-1] < v2[k2Offset+1]) { 335 | x2 = v2[k2Offset+1] 336 | } else { 337 | x2 = v2[k2Offset-1] + 1 338 | } 339 | var y2 = x2 - k2 340 | for x2 < runes1Len && y2 < runes2Len { 341 | if runes1[runes1Len-x2-1] != runes2[runes2Len-y2-1] { 342 | break 343 | } 344 | x2++ 345 | y2++ 346 | } 347 | v2[k2Offset] = x2 348 | if x2 > runes1Len { 349 | // Ran off the left of the graph. 350 | k2end += 2 351 | } else if y2 > runes2Len { 352 | // Ran off the top of the graph. 353 | k2start += 2 354 | } else if !front { 355 | k1Offset := vOffset + delta - k2 356 | if k1Offset >= 0 && k1Offset < vLength && v1[k1Offset] != -1 { 357 | x1 := v1[k1Offset] 358 | y1 := vOffset + x1 - k1Offset 359 | // Mirror x2 onto top-left coordinate system. 360 | x2 = runes1Len - x2 361 | if x1 >= x2 { 362 | // Overlap detected. 363 | return dmp.diffBisectSplit(runes1, runes2, x1, y1, deadline) 364 | } 365 | } 366 | } 367 | } 368 | } 369 | // Diff took too long and hit the deadline or number of diffs equals number of characters, no commonality at all. 370 | return []Diff{ 371 | {DiffDelete, string(runes1)}, 372 | {DiffInsert, string(runes2)}, 373 | } 374 | } 375 | 376 | func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int, 377 | deadline time.Time) []Diff { 378 | runes1a := runes1[:x] 379 | runes2a := runes2[:y] 380 | runes1b := runes1[x:] 381 | runes2b := runes2[y:] 382 | 383 | // Compute both diffs serially. 384 | diffs := dmp.diffMainRunes(runes1a, runes2a, false, deadline) 385 | diffsb := dmp.diffMainRunes(runes1b, runes2b, false, deadline) 386 | 387 | return append(diffs, diffsb...) 388 | } 389 | 390 | // DiffLinesToChars splits two texts into a list of strings, and educes the texts to a string of hashes where each Unicode character represents one line. 391 | // It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes. 392 | func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string, []string) { 393 | chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2) 394 | return chars1, chars2, lineArray 395 | } 396 | 397 | // DiffLinesToRunes splits two texts into a list of runes. 398 | func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) { 399 | chars1, chars2, lineArray := dmp.diffLinesToStrings(text1, text2) 400 | return []rune(chars1), []rune(chars2), lineArray 401 | } 402 | 403 | // DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text. 404 | func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff { 405 | hydrated := make([]Diff, 0, len(diffs)) 406 | for _, aDiff := range diffs { 407 | runes := []rune(aDiff.Text) 408 | text := make([]string, len(runes)) 409 | 410 | for i, r := range runes { 411 | text[i] = lineArray[runeToInt(r)] 412 | } 413 | 414 | aDiff.Text = strings.Join(text, "") 415 | hydrated = append(hydrated, aDiff) 416 | } 417 | return hydrated 418 | } 419 | 420 | // DiffCommonPrefix determines the common prefix length of two strings. 421 | func (dmp *DiffMatchPatch) DiffCommonPrefix(text1, text2 string) int { 422 | // Unused in this code, but retained for interface compatibility. 423 | return commonPrefixLength([]rune(text1), []rune(text2)) 424 | } 425 | 426 | // DiffCommonSuffix determines the common suffix length of two strings. 427 | func (dmp *DiffMatchPatch) DiffCommonSuffix(text1, text2 string) int { 428 | // Unused in this code, but retained for interface compatibility. 429 | return commonSuffixLength([]rune(text1), []rune(text2)) 430 | } 431 | 432 | // commonPrefixLength returns the length of the common prefix of two rune slices. 433 | func commonPrefixLength(text1, text2 []rune) int { 434 | // Linear search. See comment in commonSuffixLength. 435 | n := 0 436 | for ; n < len(text1) && n < len(text2); n++ { 437 | if text1[n] != text2[n] { 438 | return n 439 | } 440 | } 441 | return n 442 | } 443 | 444 | // commonSuffixLength returns the length of the common suffix of two rune slices. 445 | func commonSuffixLength(text1, text2 []rune) int { 446 | // Use linear search rather than the binary search discussed at https://neil.fraser.name/news/2007/10/09/. 447 | // See discussion at https://github.com/sergi/go-diff/issues/54. 448 | i1 := len(text1) 449 | i2 := len(text2) 450 | for n := 0; ; n++ { 451 | i1-- 452 | i2-- 453 | if i1 < 0 || i2 < 0 || text1[i1] != text2[i2] { 454 | return n 455 | } 456 | } 457 | } 458 | 459 | // DiffCommonOverlap determines if the suffix of one string is the prefix of another. 460 | func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int { 461 | // Cache the text lengths to prevent multiple calls. 462 | text1Length := len(text1) 463 | text2Length := len(text2) 464 | // Eliminate the null case. 465 | if text1Length == 0 || text2Length == 0 { 466 | return 0 467 | } 468 | // Truncate the longer string. 469 | if text1Length > text2Length { 470 | text1 = text1[text1Length-text2Length:] 471 | } else if text1Length < text2Length { 472 | text2 = text2[0:text1Length] 473 | } 474 | textLength := int(math.Min(float64(text1Length), float64(text2Length))) 475 | // Quick check for the worst case. 476 | if text1 == text2 { 477 | return textLength 478 | } 479 | 480 | // Start by looking for a single character match and increase length until no match is found. Performance analysis: http://neil.fraser.name/news/2010/11/04/ 481 | best := 0 482 | length := 1 483 | for { 484 | pattern := text1[textLength-length:] 485 | found := strings.Index(text2, pattern) 486 | if found == -1 { 487 | break 488 | } 489 | length += found 490 | if found == 0 || text1[textLength-length:] == text2[0:length] { 491 | best = length 492 | length++ 493 | } 494 | } 495 | 496 | return best 497 | } 498 | 499 | // DiffHalfMatch checks whether the two texts share a substring which is at least half the length of the longer text. This speedup can produce non-minimal diffs. 500 | func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string { 501 | // Unused in this code, but retained for interface compatibility. 502 | runeSlices := dmp.diffHalfMatch([]rune(text1), []rune(text2)) 503 | if runeSlices == nil { 504 | return nil 505 | } 506 | 507 | result := make([]string, len(runeSlices)) 508 | for i, r := range runeSlices { 509 | result[i] = string(r) 510 | } 511 | return result 512 | } 513 | 514 | func (dmp *DiffMatchPatch) diffHalfMatch(text1, text2 []rune) [][]rune { 515 | if dmp.DiffTimeout <= 0 { 516 | // Don't risk returning a non-optimal diff if we have unlimited time. 517 | return nil 518 | } 519 | 520 | var longtext, shorttext []rune 521 | if len(text1) > len(text2) { 522 | longtext = text1 523 | shorttext = text2 524 | } else { 525 | longtext = text2 526 | shorttext = text1 527 | } 528 | 529 | if len(longtext) < 4 || len(shorttext)*2 < len(longtext) { 530 | return nil // Pointless. 531 | } 532 | 533 | // First check if the second quarter is the seed for a half-match. 534 | hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+3)/4)) 535 | 536 | // Check again based on the third quarter. 537 | hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+1)/2)) 538 | 539 | hm := [][]rune{} 540 | if hm1 == nil && hm2 == nil { 541 | return nil 542 | } else if hm2 == nil { 543 | hm = hm1 544 | } else if hm1 == nil { 545 | hm = hm2 546 | } else { 547 | // Both matched. Select the longest. 548 | if len(hm1[4]) > len(hm2[4]) { 549 | hm = hm1 550 | } else { 551 | hm = hm2 552 | } 553 | } 554 | 555 | // A half-match was found, sort out the return data. 556 | if len(text1) > len(text2) { 557 | return hm 558 | } 559 | 560 | return [][]rune{hm[2], hm[3], hm[0], hm[1], hm[4]} 561 | } 562 | 563 | // diffHalfMatchI checks if a substring of shorttext exist within longtext such that the substring is at least half the length of longtext? 564 | // Returns a slice containing the prefix of longtext, the suffix of longtext, the prefix of shorttext, the suffix of shorttext and the common middle, or null if there was no match. 565 | func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune { 566 | var bestCommonA []rune 567 | var bestCommonB []rune 568 | var bestCommonLen int 569 | var bestLongtextA []rune 570 | var bestLongtextB []rune 571 | var bestShorttextA []rune 572 | var bestShorttextB []rune 573 | 574 | // Start with a 1/4 length substring at position i as a seed. 575 | seed := l[i : i+len(l)/4] 576 | 577 | for j := runesIndexOf(s, seed, 0); j != -1; j = runesIndexOf(s, seed, j+1) { 578 | prefixLength := commonPrefixLength(l[i:], s[j:]) 579 | suffixLength := commonSuffixLength(l[:i], s[:j]) 580 | 581 | if bestCommonLen < suffixLength+prefixLength { 582 | bestCommonA = s[j-suffixLength : j] 583 | bestCommonB = s[j : j+prefixLength] 584 | bestCommonLen = len(bestCommonA) + len(bestCommonB) 585 | bestLongtextA = l[:i-suffixLength] 586 | bestLongtextB = l[i+prefixLength:] 587 | bestShorttextA = s[:j-suffixLength] 588 | bestShorttextB = s[j+prefixLength:] 589 | } 590 | } 591 | 592 | if bestCommonLen*2 < len(l) { 593 | return nil 594 | } 595 | 596 | return [][]rune{ 597 | bestLongtextA, 598 | bestLongtextB, 599 | bestShorttextA, 600 | bestShorttextB, 601 | append(bestCommonA, bestCommonB...), 602 | } 603 | } 604 | 605 | // DiffCleanupSemantic reduces the number of edits by eliminating semantically trivial equalities. 606 | func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff { 607 | changes := false 608 | // Stack of indices where equalities are found. 609 | equalities := make([]int, 0, len(diffs)) 610 | 611 | var lastequality string 612 | // Always equal to diffs[equalities[equalitiesLength - 1]][1] 613 | var pointer int // Index of current position. 614 | // Number of characters that changed prior to the equality. 615 | var lengthInsertions1, lengthDeletions1 int 616 | // Number of characters that changed after the equality. 617 | var lengthInsertions2, lengthDeletions2 int 618 | 619 | for pointer < len(diffs) { 620 | if diffs[pointer].Type == DiffEqual { 621 | // Equality found. 622 | equalities = append(equalities, pointer) 623 | lengthInsertions1 = lengthInsertions2 624 | lengthDeletions1 = lengthDeletions2 625 | lengthInsertions2 = 0 626 | lengthDeletions2 = 0 627 | lastequality = diffs[pointer].Text 628 | } else { 629 | // An insertion or deletion. 630 | 631 | if diffs[pointer].Type == DiffInsert { 632 | lengthInsertions2 += utf8.RuneCountInString(diffs[pointer].Text) 633 | } else { 634 | lengthDeletions2 += utf8.RuneCountInString(diffs[pointer].Text) 635 | } 636 | // Eliminate an equality that is smaller or equal to the edits on both sides of it. 637 | difference1 := int(math.Max(float64(lengthInsertions1), float64(lengthDeletions1))) 638 | difference2 := int(math.Max(float64(lengthInsertions2), float64(lengthDeletions2))) 639 | if utf8.RuneCountInString(lastequality) > 0 && 640 | (utf8.RuneCountInString(lastequality) <= difference1) && 641 | (utf8.RuneCountInString(lastequality) <= difference2) { 642 | // Duplicate record. 643 | insPoint := equalities[len(equalities)-1] 644 | diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality}) 645 | 646 | // Change second copy to insert. 647 | diffs[insPoint+1].Type = DiffInsert 648 | // Throw away the equality we just deleted. 649 | equalities = equalities[:len(equalities)-1] 650 | 651 | if len(equalities) > 0 { 652 | equalities = equalities[:len(equalities)-1] 653 | } 654 | pointer = -1 655 | if len(equalities) > 0 { 656 | pointer = equalities[len(equalities)-1] 657 | } 658 | 659 | lengthInsertions1 = 0 // Reset the counters. 660 | lengthDeletions1 = 0 661 | lengthInsertions2 = 0 662 | lengthDeletions2 = 0 663 | lastequality = "" 664 | changes = true 665 | } 666 | } 667 | pointer++ 668 | } 669 | 670 | // Normalize the diff. 671 | if changes { 672 | diffs = dmp.DiffCleanupMerge(diffs) 673 | } 674 | diffs = dmp.DiffCleanupSemanticLossless(diffs) 675 | // Find any overlaps between deletions and insertions. 676 | // e.g: abcxxxxxxdef 677 | // -> abcxxxdef 678 | // e.g: xxxabcdefxxx 679 | // -> defxxxabc 680 | // Only extract an overlap if it is as big as the edit ahead or behind it. 681 | pointer = 1 682 | for pointer < len(diffs) { 683 | if diffs[pointer-1].Type == DiffDelete && 684 | diffs[pointer].Type == DiffInsert { 685 | deletion := diffs[pointer-1].Text 686 | insertion := diffs[pointer].Text 687 | overlapLength1 := dmp.DiffCommonOverlap(deletion, insertion) 688 | overlapLength2 := dmp.DiffCommonOverlap(insertion, deletion) 689 | if overlapLength1 >= overlapLength2 { 690 | if float64(overlapLength1) >= float64(utf8.RuneCountInString(deletion))/2 || 691 | float64(overlapLength1) >= float64(utf8.RuneCountInString(insertion))/2 { 692 | 693 | // Overlap found. Insert an equality and trim the surrounding edits. 694 | diffs = splice(diffs, pointer, 0, Diff{DiffEqual, insertion[:overlapLength1]}) 695 | diffs[pointer-1].Text = 696 | deletion[0 : len(deletion)-overlapLength1] 697 | diffs[pointer+1].Text = insertion[overlapLength1:] 698 | pointer++ 699 | } 700 | } else { 701 | if float64(overlapLength2) >= float64(utf8.RuneCountInString(deletion))/2 || 702 | float64(overlapLength2) >= float64(utf8.RuneCountInString(insertion))/2 { 703 | // Reverse overlap found. Insert an equality and swap and trim the surrounding edits. 704 | overlap := Diff{DiffEqual, deletion[:overlapLength2]} 705 | diffs = splice(diffs, pointer, 0, overlap) 706 | diffs[pointer-1].Type = DiffInsert 707 | diffs[pointer-1].Text = insertion[0 : len(insertion)-overlapLength2] 708 | diffs[pointer+1].Type = DiffDelete 709 | diffs[pointer+1].Text = deletion[overlapLength2:] 710 | pointer++ 711 | } 712 | } 713 | pointer++ 714 | } 715 | pointer++ 716 | } 717 | 718 | return diffs 719 | } 720 | 721 | // Define some regex patterns for matching boundaries. 722 | var ( 723 | nonAlphaNumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]`) 724 | whitespaceRegex = regexp.MustCompile(`\s`) 725 | linebreakRegex = regexp.MustCompile(`[\r\n]`) 726 | blanklineEndRegex = regexp.MustCompile(`\n\r?\n$`) 727 | blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`) 728 | ) 729 | 730 | // diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries. 731 | // Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables. 732 | func diffCleanupSemanticScore(one, two string) int { 733 | if len(one) == 0 || len(two) == 0 { 734 | // Edges are the best. 735 | return 6 736 | } 737 | 738 | // Each port of this function behaves slightly differently due to subtle differences in each language's definition of things like 'whitespace'. Since this function's purpose is largely cosmetic, the choice has been made to use each language's native features rather than force total conformity. 739 | rune1, _ := utf8.DecodeLastRuneInString(one) 740 | rune2, _ := utf8.DecodeRuneInString(two) 741 | char1 := string(rune1) 742 | char2 := string(rune2) 743 | 744 | nonAlphaNumeric1 := nonAlphaNumericRegex.MatchString(char1) 745 | nonAlphaNumeric2 := nonAlphaNumericRegex.MatchString(char2) 746 | whitespace1 := nonAlphaNumeric1 && whitespaceRegex.MatchString(char1) 747 | whitespace2 := nonAlphaNumeric2 && whitespaceRegex.MatchString(char2) 748 | lineBreak1 := whitespace1 && linebreakRegex.MatchString(char1) 749 | lineBreak2 := whitespace2 && linebreakRegex.MatchString(char2) 750 | blankLine1 := lineBreak1 && blanklineEndRegex.MatchString(one) 751 | blankLine2 := lineBreak2 && blanklineEndRegex.MatchString(two) 752 | 753 | if blankLine1 || blankLine2 { 754 | // Five points for blank lines. 755 | return 5 756 | } else if lineBreak1 || lineBreak2 { 757 | // Four points for line breaks. 758 | return 4 759 | } else if nonAlphaNumeric1 && !whitespace1 && whitespace2 { 760 | // Three points for end of sentences. 761 | return 3 762 | } else if whitespace1 || whitespace2 { 763 | // Two points for whitespace. 764 | return 2 765 | } else if nonAlphaNumeric1 || nonAlphaNumeric2 { 766 | // One point for non-alphanumeric. 767 | return 1 768 | } 769 | return 0 770 | } 771 | 772 | // DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary. 773 | // E.g: The cat came. -> The cat came. 774 | func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff { 775 | pointer := 1 776 | 777 | // Intentionally ignore the first and last element (don't need checking). 778 | for pointer < len(diffs)-1 { 779 | if diffs[pointer-1].Type == DiffEqual && 780 | diffs[pointer+1].Type == DiffEqual { 781 | 782 | // This is a single edit surrounded by equalities. 783 | equality1 := diffs[pointer-1].Text 784 | edit := diffs[pointer].Text 785 | equality2 := diffs[pointer+1].Text 786 | 787 | // First, shift the edit as far left as possible. 788 | commonOffset := dmp.DiffCommonSuffix(equality1, edit) 789 | if commonOffset > 0 { 790 | commonString := edit[len(edit)-commonOffset:] 791 | equality1 = equality1[0 : len(equality1)-commonOffset] 792 | edit = commonString + edit[:len(edit)-commonOffset] 793 | equality2 = commonString + equality2 794 | } 795 | 796 | // Second, step character by character right, looking for the best fit. 797 | bestEquality1 := equality1 798 | bestEdit := edit 799 | bestEquality2 := equality2 800 | bestScore := diffCleanupSemanticScore(equality1, edit) + 801 | diffCleanupSemanticScore(edit, equality2) 802 | 803 | for len(edit) != 0 && len(equality2) != 0 { 804 | _, sz := utf8.DecodeRuneInString(edit) 805 | if len(equality2) < sz || edit[:sz] != equality2[:sz] { 806 | break 807 | } 808 | equality1 += edit[:sz] 809 | edit = edit[sz:] + equality2[:sz] 810 | equality2 = equality2[sz:] 811 | score := diffCleanupSemanticScore(equality1, edit) + 812 | diffCleanupSemanticScore(edit, equality2) 813 | // The >= encourages trailing rather than leading whitespace on edits. 814 | if score >= bestScore { 815 | bestScore = score 816 | bestEquality1 = equality1 817 | bestEdit = edit 818 | bestEquality2 = equality2 819 | } 820 | } 821 | 822 | if diffs[pointer-1].Text != bestEquality1 { 823 | // We have an improvement, save it back to the diff. 824 | if len(bestEquality1) != 0 { 825 | diffs[pointer-1].Text = bestEquality1 826 | } else { 827 | diffs = splice(diffs, pointer-1, 1) 828 | pointer-- 829 | } 830 | 831 | diffs[pointer].Text = bestEdit 832 | if len(bestEquality2) != 0 { 833 | diffs[pointer+1].Text = bestEquality2 834 | } else { 835 | diffs = append(diffs[:pointer+1], diffs[pointer+2:]...) 836 | pointer-- 837 | } 838 | } 839 | } 840 | pointer++ 841 | } 842 | 843 | return diffs 844 | } 845 | 846 | // DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities. 847 | func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff { 848 | changes := false 849 | // Stack of indices where equalities are found. 850 | type equality struct { 851 | data int 852 | next *equality 853 | } 854 | var equalities *equality 855 | // Always equal to equalities[equalitiesLength-1][1] 856 | lastequality := "" 857 | pointer := 0 // Index of current position. 858 | // Is there an insertion operation before the last equality. 859 | preIns := false 860 | // Is there a deletion operation before the last equality. 861 | preDel := false 862 | // Is there an insertion operation after the last equality. 863 | postIns := false 864 | // Is there a deletion operation after the last equality. 865 | postDel := false 866 | for pointer < len(diffs) { 867 | if diffs[pointer].Type == DiffEqual { // Equality found. 868 | if len(diffs[pointer].Text) < dmp.DiffEditCost && 869 | (postIns || postDel) { 870 | // Candidate found. 871 | equalities = &equality{ 872 | data: pointer, 873 | next: equalities, 874 | } 875 | preIns = postIns 876 | preDel = postDel 877 | lastequality = diffs[pointer].Text 878 | } else { 879 | // Not a candidate, and can never become one. 880 | equalities = nil 881 | lastequality = "" 882 | } 883 | postIns = false 884 | postDel = false 885 | } else { // An insertion or deletion. 886 | if diffs[pointer].Type == DiffDelete { 887 | postDel = true 888 | } else { 889 | postIns = true 890 | } 891 | 892 | // Five types to be split: 893 | // ABXYCD 894 | // AXCD 895 | // ABXC 896 | // AXCD 897 | // ABXC 898 | var sumPres int 899 | if preIns { 900 | sumPres++ 901 | } 902 | if preDel { 903 | sumPres++ 904 | } 905 | if postIns { 906 | sumPres++ 907 | } 908 | if postDel { 909 | sumPres++ 910 | } 911 | if len(lastequality) > 0 && 912 | ((preIns && preDel && postIns && postDel) || 913 | ((len(lastequality) < dmp.DiffEditCost/2) && sumPres == 3)) { 914 | 915 | insPoint := equalities.data 916 | 917 | // Duplicate record. 918 | diffs = splice(diffs, insPoint, 0, Diff{DiffDelete, lastequality}) 919 | 920 | // Change second copy to insert. 921 | diffs[insPoint+1].Type = DiffInsert 922 | // Throw away the equality we just deleted. 923 | equalities = equalities.next 924 | lastequality = "" 925 | 926 | if preIns && preDel { 927 | // No changes made which could affect previous entry, keep going. 928 | postIns = true 929 | postDel = true 930 | equalities = nil 931 | } else { 932 | if equalities != nil { 933 | equalities = equalities.next 934 | } 935 | if equalities != nil { 936 | pointer = equalities.data 937 | } else { 938 | pointer = -1 939 | } 940 | postIns = false 941 | postDel = false 942 | } 943 | changes = true 944 | } 945 | } 946 | pointer++ 947 | } 948 | 949 | if changes { 950 | diffs = dmp.DiffCleanupMerge(diffs) 951 | } 952 | 953 | return diffs 954 | } 955 | 956 | // DiffCleanupMerge reorders and merges like edit sections. Merge equalities. 957 | // Any edit section can move as long as it doesn't cross an equality. 958 | func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff { 959 | // Add a dummy entry at the end. 960 | diffs = append(diffs, Diff{DiffEqual, ""}) 961 | pointer := 0 962 | countDelete := 0 963 | countInsert := 0 964 | commonlength := 0 965 | textDelete := []rune(nil) 966 | textInsert := []rune(nil) 967 | 968 | for pointer < len(diffs) { 969 | switch diffs[pointer].Type { 970 | case DiffInsert: 971 | countInsert++ 972 | textInsert = append(textInsert, []rune(diffs[pointer].Text)...) 973 | pointer++ 974 | break 975 | case DiffDelete: 976 | countDelete++ 977 | textDelete = append(textDelete, []rune(diffs[pointer].Text)...) 978 | pointer++ 979 | break 980 | case DiffEqual: 981 | // Upon reaching an equality, check for prior redundancies. 982 | if countDelete+countInsert > 1 { 983 | if countDelete != 0 && countInsert != 0 { 984 | // Factor out any common prefixies. 985 | commonlength = commonPrefixLength(textInsert, textDelete) 986 | if commonlength != 0 { 987 | x := pointer - countDelete - countInsert 988 | if x > 0 && diffs[x-1].Type == DiffEqual { 989 | diffs[x-1].Text += string(textInsert[:commonlength]) 990 | } else { 991 | diffs = append([]Diff{{DiffEqual, string(textInsert[:commonlength])}}, diffs...) 992 | pointer++ 993 | } 994 | textInsert = textInsert[commonlength:] 995 | textDelete = textDelete[commonlength:] 996 | } 997 | // Factor out any common suffixies. 998 | commonlength = commonSuffixLength(textInsert, textDelete) 999 | if commonlength != 0 { 1000 | insertIndex := len(textInsert) - commonlength 1001 | deleteIndex := len(textDelete) - commonlength 1002 | diffs[pointer].Text = string(textInsert[insertIndex:]) + diffs[pointer].Text 1003 | textInsert = textInsert[:insertIndex] 1004 | textDelete = textDelete[:deleteIndex] 1005 | } 1006 | } 1007 | // Delete the offending records and add the merged ones. 1008 | if countDelete == 0 { 1009 | diffs = splice(diffs, pointer-countInsert, 1010 | countDelete+countInsert, 1011 | Diff{DiffInsert, string(textInsert)}) 1012 | } else if countInsert == 0 { 1013 | diffs = splice(diffs, pointer-countDelete, 1014 | countDelete+countInsert, 1015 | Diff{DiffDelete, string(textDelete)}) 1016 | } else { 1017 | diffs = splice(diffs, pointer-countDelete-countInsert, 1018 | countDelete+countInsert, 1019 | Diff{DiffDelete, string(textDelete)}, 1020 | Diff{DiffInsert, string(textInsert)}) 1021 | } 1022 | 1023 | pointer = pointer - countDelete - countInsert + 1 1024 | if countDelete != 0 { 1025 | pointer++ 1026 | } 1027 | if countInsert != 0 { 1028 | pointer++ 1029 | } 1030 | } else if pointer != 0 && diffs[pointer-1].Type == DiffEqual { 1031 | // Merge this equality with the previous one. 1032 | diffs[pointer-1].Text += diffs[pointer].Text 1033 | diffs = append(diffs[:pointer], diffs[pointer+1:]...) 1034 | } else { 1035 | pointer++ 1036 | } 1037 | countInsert = 0 1038 | countDelete = 0 1039 | textDelete = nil 1040 | textInsert = nil 1041 | break 1042 | } 1043 | } 1044 | 1045 | if len(diffs[len(diffs)-1].Text) == 0 { 1046 | diffs = diffs[0 : len(diffs)-1] // Remove the dummy entry at the end. 1047 | } 1048 | 1049 | // Second pass: look for single edits surrounded on both sides by equalities which can be shifted sideways to eliminate an equality. E.g: ABAC -> ABAC 1050 | changes := false 1051 | pointer = 1 1052 | // Intentionally ignore the first and last element (don't need checking). 1053 | for pointer < (len(diffs) - 1) { 1054 | if diffs[pointer-1].Type == DiffEqual && 1055 | diffs[pointer+1].Type == DiffEqual { 1056 | // This is a single edit surrounded by equalities. 1057 | if strings.HasSuffix(diffs[pointer].Text, diffs[pointer-1].Text) { 1058 | // Shift the edit over the previous equality. 1059 | diffs[pointer].Text = diffs[pointer-1].Text + 1060 | diffs[pointer].Text[:len(diffs[pointer].Text)-len(diffs[pointer-1].Text)] 1061 | diffs[pointer+1].Text = diffs[pointer-1].Text + diffs[pointer+1].Text 1062 | diffs = splice(diffs, pointer-1, 1) 1063 | changes = true 1064 | } else if strings.HasPrefix(diffs[pointer].Text, diffs[pointer+1].Text) { 1065 | // Shift the edit over the next equality. 1066 | diffs[pointer-1].Text += diffs[pointer+1].Text 1067 | diffs[pointer].Text = 1068 | diffs[pointer].Text[len(diffs[pointer+1].Text):] + diffs[pointer+1].Text 1069 | diffs = splice(diffs, pointer+1, 1) 1070 | changes = true 1071 | } 1072 | } 1073 | pointer++ 1074 | } 1075 | 1076 | // If shifts were made, the diff needs reordering and another shift sweep. 1077 | if changes { 1078 | diffs = dmp.DiffCleanupMerge(diffs) 1079 | } 1080 | 1081 | return diffs 1082 | } 1083 | 1084 | // DiffXIndex returns the equivalent location in s2. 1085 | func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int { 1086 | chars1 := 0 1087 | chars2 := 0 1088 | lastChars1 := 0 1089 | lastChars2 := 0 1090 | lastDiff := Diff{} 1091 | for i := 0; i < len(diffs); i++ { 1092 | aDiff := diffs[i] 1093 | if aDiff.Type != DiffInsert { 1094 | // Equality or deletion. 1095 | chars1 += len(aDiff.Text) 1096 | } 1097 | if aDiff.Type != DiffDelete { 1098 | // Equality or insertion. 1099 | chars2 += len(aDiff.Text) 1100 | } 1101 | if chars1 > loc { 1102 | // Overshot the location. 1103 | lastDiff = aDiff 1104 | break 1105 | } 1106 | lastChars1 = chars1 1107 | lastChars2 = chars2 1108 | } 1109 | if lastDiff.Type == DiffDelete { 1110 | // The location was deleted. 1111 | return lastChars2 1112 | } 1113 | // Add the remaining character length. 1114 | return lastChars2 + (loc - lastChars1) 1115 | } 1116 | 1117 | // DiffPrettyHtml converts a []Diff into a pretty HTML report. 1118 | // It is intended as an example from which to write one's own display functions. 1119 | func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string { 1120 | var buff bytes.Buffer 1121 | for _, diff := range diffs { 1122 | text := strings.Replace(html.EscapeString(diff.Text), "\n", "¶
", -1) 1123 | switch diff.Type { 1124 | case DiffInsert: 1125 | _, _ = buff.WriteString("") 1126 | _, _ = buff.WriteString(text) 1127 | _, _ = buff.WriteString("") 1128 | case DiffDelete: 1129 | _, _ = buff.WriteString("") 1130 | _, _ = buff.WriteString(text) 1131 | _, _ = buff.WriteString("") 1132 | case DiffEqual: 1133 | _, _ = buff.WriteString("") 1134 | _, _ = buff.WriteString(text) 1135 | _, _ = buff.WriteString("") 1136 | } 1137 | } 1138 | return buff.String() 1139 | } 1140 | 1141 | // DiffPrettyText converts a []Diff into a colored text report. 1142 | func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string { 1143 | var buff bytes.Buffer 1144 | for _, diff := range diffs { 1145 | text := diff.Text 1146 | 1147 | switch diff.Type { 1148 | case DiffInsert: 1149 | lines := strings.Split(text, "\n") 1150 | for i, line := range lines { 1151 | _, _ = buff.WriteString("\x1b[32m") 1152 | _, _ = buff.WriteString(line) 1153 | if i < len(lines)-1 { 1154 | _, _ = buff.WriteString("\x1b[0m\n") 1155 | } else { 1156 | _, _ = buff.WriteString("\x1b[0m") 1157 | } 1158 | } 1159 | 1160 | case DiffDelete: 1161 | lines := strings.Split(text, "\n") 1162 | for i, line := range lines { 1163 | _, _ = buff.WriteString("\x1b[31m") 1164 | _, _ = buff.WriteString(line) 1165 | if i < len(lines)-1 { 1166 | _, _ = buff.WriteString("\x1b[0m\n") 1167 | } else { 1168 | _, _ = buff.WriteString("\x1b[0m") 1169 | } 1170 | } 1171 | case DiffEqual: 1172 | _, _ = buff.WriteString(text) 1173 | } 1174 | } 1175 | 1176 | return buff.String() 1177 | } 1178 | 1179 | // DiffText1 computes and returns the source text (all equalities and deletions). 1180 | func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string { 1181 | //StringBuilder text = new StringBuilder() 1182 | var text bytes.Buffer 1183 | 1184 | for _, aDiff := range diffs { 1185 | if aDiff.Type != DiffInsert { 1186 | _, _ = text.WriteString(aDiff.Text) 1187 | } 1188 | } 1189 | return text.String() 1190 | } 1191 | 1192 | // DiffText2 computes and returns the destination text (all equalities and insertions). 1193 | func (dmp *DiffMatchPatch) DiffText2(diffs []Diff) string { 1194 | var text bytes.Buffer 1195 | 1196 | for _, aDiff := range diffs { 1197 | if aDiff.Type != DiffDelete { 1198 | _, _ = text.WriteString(aDiff.Text) 1199 | } 1200 | } 1201 | return text.String() 1202 | } 1203 | 1204 | // DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters. 1205 | func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int { 1206 | levenshtein := 0 1207 | insertions := 0 1208 | deletions := 0 1209 | 1210 | for _, aDiff := range diffs { 1211 | switch aDiff.Type { 1212 | case DiffInsert: 1213 | insertions += utf8.RuneCountInString(aDiff.Text) 1214 | case DiffDelete: 1215 | deletions += utf8.RuneCountInString(aDiff.Text) 1216 | case DiffEqual: 1217 | // A deletion and an insertion is one substitution. 1218 | levenshtein += max(insertions, deletions) 1219 | insertions = 0 1220 | deletions = 0 1221 | } 1222 | } 1223 | 1224 | levenshtein += max(insertions, deletions) 1225 | return levenshtein 1226 | } 1227 | 1228 | // DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2. 1229 | // E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation. 1230 | func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string { 1231 | var text bytes.Buffer 1232 | for _, aDiff := range diffs { 1233 | switch aDiff.Type { 1234 | case DiffInsert: 1235 | _, _ = text.WriteString("+") 1236 | _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1)) 1237 | _, _ = text.WriteString("\t") 1238 | break 1239 | case DiffDelete: 1240 | _, _ = text.WriteString("-") 1241 | _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text))) 1242 | _, _ = text.WriteString("\t") 1243 | break 1244 | case DiffEqual: 1245 | _, _ = text.WriteString("=") 1246 | _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text))) 1247 | _, _ = text.WriteString("\t") 1248 | break 1249 | } 1250 | } 1251 | delta := text.String() 1252 | if len(delta) != 0 { 1253 | // Strip off trailing tab character. 1254 | delta = delta[0 : utf8.RuneCountInString(delta)-1] 1255 | delta = unescaper.Replace(delta) 1256 | } 1257 | return delta 1258 | } 1259 | 1260 | // DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff. 1261 | func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) { 1262 | i := 0 1263 | runes := []rune(text1) 1264 | 1265 | for _, token := range strings.Split(delta, "\t") { 1266 | if len(token) == 0 { 1267 | // Blank tokens are ok (from a trailing \t). 1268 | continue 1269 | } 1270 | 1271 | // Each token begins with a one character parameter which specifies the operation of this token (delete, insert, equality). 1272 | param := token[1:] 1273 | 1274 | switch op := token[0]; op { 1275 | case '+': 1276 | // Decode would Diff all "+" to " " 1277 | param = strings.Replace(param, "+", "%2b", -1) 1278 | param, err = url.QueryUnescape(param) 1279 | if err != nil { 1280 | return nil, err 1281 | } 1282 | if !utf8.ValidString(param) { 1283 | return nil, fmt.Errorf("invalid UTF-8 token: %q", param) 1284 | } 1285 | 1286 | diffs = append(diffs, Diff{DiffInsert, param}) 1287 | case '=', '-': 1288 | n, err := strconv.ParseInt(param, 10, 0) 1289 | if err != nil { 1290 | return nil, err 1291 | } else if n < 0 { 1292 | return nil, errors.New("Negative number in DiffFromDelta: " + param) 1293 | } 1294 | 1295 | i += int(n) 1296 | // Break out if we are out of bounds, go1.6 can't handle this very well 1297 | if i > len(runes) { 1298 | break 1299 | } 1300 | // Remember that string slicing is by byte - we want by rune here. 1301 | text := string(runes[i-int(n) : i]) 1302 | 1303 | if op == '=' { 1304 | diffs = append(diffs, Diff{DiffEqual, text}) 1305 | } else { 1306 | diffs = append(diffs, Diff{DiffDelete, text}) 1307 | } 1308 | default: 1309 | // Anything else is an error. 1310 | return nil, errors.New("Invalid diff operation in DiffFromDelta: " + string(token[0])) 1311 | } 1312 | } 1313 | 1314 | if i != len(runes) { 1315 | return nil, fmt.Errorf("Delta length (%v) is different from source text length (%v)", i, len(text1)) 1316 | } 1317 | 1318 | return diffs, nil 1319 | } 1320 | 1321 | // diffLinesToStrings splits two texts into a list of strings. Each string represents one line. 1322 | func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) { 1323 | lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n' 1324 | 1325 | lineHash := make(map[string]int) 1326 | //Each string has the index of lineArray which it points to 1327 | strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray, lineHash) 1328 | strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray, lineHash) 1329 | 1330 | return intArrayToString(strIndexArray1), intArrayToString(strIndexArray2), lineArray 1331 | } 1332 | 1333 | // diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []index. 1334 | func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []index { 1335 | lineStart := 0 1336 | lineEnd := -1 1337 | strs := []index{} 1338 | 1339 | for lineEnd < len(text)-1 { 1340 | lineEnd = indexOf(text, "\n", lineStart) 1341 | 1342 | if lineEnd == -1 { 1343 | lineEnd = len(text) - 1 1344 | } 1345 | 1346 | line := text[lineStart : lineEnd+1] 1347 | lineStart = lineEnd + 1 1348 | lineValue, ok := lineHash[line] 1349 | 1350 | if ok { 1351 | strs = append(strs, index(lineValue)) 1352 | } else { 1353 | *lineArray = append(*lineArray, line) 1354 | lineHash[line] = len(*lineArray) - 1 1355 | strs = append(strs, index(len(*lineArray)-1)) 1356 | } 1357 | } 1358 | 1359 | return strs 1360 | } 1361 | -------------------------------------------------------------------------------- /diffmatchpatch/diff_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "bytes" 13 | "fmt" 14 | "io/ioutil" 15 | "os" 16 | "strconv" 17 | "strings" 18 | "testing" 19 | "time" 20 | "unicode/utf8" 21 | 22 | "github.com/stretchr/testify/assert" 23 | ) 24 | 25 | func pretty(diffs []Diff) string { 26 | var w bytes.Buffer 27 | 28 | for i, diff := range diffs { 29 | _, _ = w.WriteString(fmt.Sprintf("%v. ", i)) 30 | 31 | switch diff.Type { 32 | case DiffInsert: 33 | _, _ = w.WriteString("DiffIns") 34 | case DiffDelete: 35 | _, _ = w.WriteString("DiffDel") 36 | case DiffEqual: 37 | _, _ = w.WriteString("DiffEql") 38 | default: 39 | _, _ = w.WriteString("Unknown") 40 | } 41 | 42 | _, _ = w.WriteString(fmt.Sprintf(": %v\n", diff.Text)) 43 | } 44 | 45 | return w.String() 46 | } 47 | 48 | func diffRebuildTexts(diffs []Diff) []string { 49 | texts := []string{"", ""} 50 | 51 | for _, d := range diffs { 52 | if d.Type != DiffInsert { 53 | texts[0] += d.Text 54 | } 55 | if d.Type != DiffDelete { 56 | texts[1] += d.Text 57 | } 58 | } 59 | 60 | return texts 61 | } 62 | 63 | func TestDiffCommonPrefix(t *testing.T) { 64 | type TestCase struct { 65 | Name string 66 | 67 | Text1 string 68 | Text2 string 69 | 70 | Expected int 71 | } 72 | 73 | dmp := New() 74 | 75 | for i, tc := range []TestCase{ 76 | {"Null", "abc", "xyz", 0}, 77 | {"Non-null", "1234abcdef", "1234xyz", 4}, 78 | {"Whole", "1234", "1234xyz", 4}, 79 | } { 80 | actual := dmp.DiffCommonPrefix(tc.Text1, tc.Text2) 81 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 82 | } 83 | } 84 | 85 | func BenchmarkDiffCommonPrefix(b *testing.B) { 86 | s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" 87 | 88 | dmp := New() 89 | 90 | for i := 0; i < b.N; i++ { 91 | dmp.DiffCommonPrefix(s, s) 92 | } 93 | } 94 | 95 | func TestCommonPrefixLength(t *testing.T) { 96 | type TestCase struct { 97 | Text1 string 98 | Text2 string 99 | 100 | Expected int 101 | } 102 | 103 | for i, tc := range []TestCase{ 104 | {"abc", "xyz", 0}, 105 | {"1234abcdef", "1234xyz", 4}, 106 | {"1234", "1234xyz", 4}, 107 | } { 108 | actual := commonPrefixLength([]rune(tc.Text1), []rune(tc.Text2)) 109 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 110 | } 111 | } 112 | 113 | func TestDiffCommonSuffix(t *testing.T) { 114 | type TestCase struct { 115 | Name string 116 | 117 | Text1 string 118 | Text2 string 119 | 120 | Expected int 121 | } 122 | 123 | dmp := New() 124 | 125 | for i, tc := range []TestCase{ 126 | {"Null", "abc", "xyz", 0}, 127 | {"Non-null", "abcdef1234", "xyz1234", 4}, 128 | {"Whole", "1234", "xyz1234", 4}, 129 | } { 130 | actual := dmp.DiffCommonSuffix(tc.Text1, tc.Text2) 131 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 132 | } 133 | } 134 | 135 | var SinkInt int // exported sink var to avoid compiler optimizations in benchmarks 136 | 137 | func BenchmarkDiffCommonSuffix(b *testing.B) { 138 | s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" 139 | 140 | dmp := New() 141 | 142 | b.ResetTimer() 143 | 144 | for i := 0; i < b.N; i++ { 145 | SinkInt = dmp.DiffCommonSuffix(s, s) 146 | } 147 | } 148 | 149 | func BenchmarkCommonLength(b *testing.B) { 150 | data := []struct { 151 | name string 152 | x, y []rune 153 | }{ 154 | {name: "empty", x: nil, y: []rune{}}, 155 | {name: "short", x: []rune("AABCC"), y: []rune("AA-CC")}, 156 | {name: "long", 157 | x: []rune(strings.Repeat("A", 1000) + "B" + strings.Repeat("C", 1000)), 158 | y: []rune(strings.Repeat("A", 1000) + "-" + strings.Repeat("C", 1000)), 159 | }, 160 | } 161 | b.Run("prefix", func(b *testing.B) { 162 | for _, d := range data { 163 | b.Run(d.name, func(b *testing.B) { 164 | for i := 0; i < b.N; i++ { 165 | SinkInt = commonPrefixLength(d.x, d.y) 166 | } 167 | }) 168 | } 169 | }) 170 | b.Run("suffix", func(b *testing.B) { 171 | for _, d := range data { 172 | b.Run(d.name, func(b *testing.B) { 173 | for i := 0; i < b.N; i++ { 174 | SinkInt = commonSuffixLength(d.x, d.y) 175 | } 176 | }) 177 | } 178 | }) 179 | } 180 | 181 | func TestCommonSuffixLength(t *testing.T) { 182 | type TestCase struct { 183 | Text1 string 184 | Text2 string 185 | 186 | Expected int 187 | } 188 | 189 | for i, tc := range []TestCase{ 190 | {"abc", "xyz", 0}, 191 | {"abcdef1234", "xyz1234", 4}, 192 | {"1234", "xyz1234", 4}, 193 | {"123", "a3", 1}, 194 | } { 195 | actual := commonSuffixLength([]rune(tc.Text1), []rune(tc.Text2)) 196 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 197 | } 198 | } 199 | 200 | func TestDiffCommonOverlap(t *testing.T) { 201 | type TestCase struct { 202 | Name string 203 | 204 | Text1 string 205 | Text2 string 206 | 207 | Expected int 208 | } 209 | 210 | dmp := New() 211 | 212 | for i, tc := range []TestCase{ 213 | {"Null", "", "abcd", 0}, 214 | {"Whole", "abc", "abcd", 3}, 215 | {"Null", "123456", "abcd", 0}, 216 | {"Null", "123456xxx", "xxxabcd", 3}, 217 | // Some overly clever languages (C#) may treat ligatures as equal to their component letters, e.g. U+FB01 == 'fi' 218 | {"Unicode", "fi", "\ufb01i", 0}, 219 | } { 220 | actual := dmp.DiffCommonOverlap(tc.Text1, tc.Text2) 221 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 222 | } 223 | } 224 | 225 | func TestDiffHalfMatch(t *testing.T) { 226 | type TestCase struct { 227 | Text1 string 228 | Text2 string 229 | 230 | Expected []string 231 | } 232 | 233 | dmp := New() 234 | dmp.DiffTimeout = 1 235 | 236 | for i, tc := range []TestCase{ 237 | // No match 238 | {"1234567890", "abcdef", nil}, 239 | {"12345", "23", nil}, 240 | 241 | // Single Match 242 | {"1234567890", "a345678z", []string{"12", "90", "a", "z", "345678"}}, 243 | {"a345678z", "1234567890", []string{"a", "z", "12", "90", "345678"}}, 244 | {"abc56789z", "1234567890", []string{"abc", "z", "1234", "0", "56789"}}, 245 | {"a23456xyz", "1234567890", []string{"a", "xyz", "1", "7890", "23456"}}, 246 | 247 | // Multiple Matches 248 | {"121231234123451234123121", "a1234123451234z", []string{"12123", "123121", "a", "z", "1234123451234"}}, 249 | {"x-=-=-=-=-=-=-=-=-=-=-=-=", "xx-=-=-=-=-=-=-=", []string{"", "-=-=-=-=-=", "x", "", "x-=-=-=-=-=-=-="}}, 250 | {"-=-=-=-=-=-=-=-=-=-=-=-=y", "-=-=-=-=-=-=-=yy", []string{"-=-=-=-=-=", "", "", "y", "-=-=-=-=-=-=-=y"}}, 251 | 252 | // Non-optimal halfmatch, ptimal diff would be -q+x=H-i+e=lloHe+Hu=llo-Hew+y not -qHillo+x=HelloHe-w+Hulloy 253 | {"qHilloHelloHew", "xHelloHeHulloy", []string{"qHillo", "w", "x", "Hulloy", "HelloHe"}}, 254 | } { 255 | actual := dmp.DiffHalfMatch(tc.Text1, tc.Text2) 256 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 257 | } 258 | 259 | dmp.DiffTimeout = 0 260 | 261 | for i, tc := range []TestCase{ 262 | // Optimal no halfmatch 263 | {"qHilloHelloHew", "xHelloHeHulloy", nil}, 264 | } { 265 | actual := dmp.DiffHalfMatch(tc.Text1, tc.Text2) 266 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 267 | } 268 | } 269 | 270 | func BenchmarkDiffHalfMatch(b *testing.B) { 271 | s1, s2 := speedtestTexts() 272 | 273 | dmp := New() 274 | 275 | b.ResetTimer() 276 | 277 | for i := 0; i < b.N; i++ { 278 | dmp.DiffHalfMatch(s1, s2) 279 | } 280 | } 281 | 282 | func TestDiffBisectSplit(t *testing.T) { 283 | type TestCase struct { 284 | Text1 string 285 | Text2 string 286 | } 287 | 288 | dmp := New() 289 | 290 | for _, tc := range []TestCase{ 291 | {"STUV\x05WX\x05YZ\x05[", "WĺĻļ\x05YZ\x05ĽľĿŀZ"}, 292 | } { 293 | diffs := dmp.diffBisectSplit([]rune(tc.Text1), 294 | []rune(tc.Text2), 7, 6, time.Now().Add(time.Hour)) 295 | 296 | for _, d := range diffs { 297 | assert.True(t, utf8.ValidString(d.Text)) 298 | } 299 | 300 | // TODO define the expected outcome 301 | } 302 | } 303 | 304 | func TestDiffLinesToChars(t *testing.T) { 305 | type TestCase struct { 306 | Text1 string 307 | Text2 string 308 | 309 | ExpectedChars1 string 310 | ExpectedChars2 string 311 | ExpectedLines []string 312 | } 313 | 314 | dmp := New() 315 | 316 | for i, tc := range []TestCase{ 317 | {"", "alpha\r\nbeta\r\n\r\n\r\n", "", "\x01\x02\x03\x03", []string{"", "alpha\r\n", "beta\r\n", "\r\n"}}, 318 | {"a", "b", "\x01", "\x02", []string{"", "a", "b"}}, 319 | // Omit final newline. 320 | {"alpha\nbeta\nalpha", "", "\x01\x02\x03", "", []string{"", "alpha\n", "beta\n", "alpha"}}, 321 | // Same lines in Text1 and Text2 322 | {"abc\ndefg\n12345\n", "abc\ndef\n12345\n678", "\x01\x02\x03", "\x01\x04\x03\x05", []string{"", "abc\n", "defg\n", "12345\n", "def\n", "678"}}, 323 | } { 324 | actualChars1, actualChars2, actualLines := dmp.DiffLinesToChars(tc.Text1, tc.Text2) 325 | assert.Equal(t, tc.ExpectedChars1, actualChars1, fmt.Sprintf("Test case #%d, %#v", i, tc)) 326 | assert.Equal(t, tc.ExpectedChars2, actualChars2, fmt.Sprintf("Test case #%d, %#v", i, tc)) 327 | assert.Equal(t, tc.ExpectedLines, actualLines, fmt.Sprintf("Test case #%d, %#v", i, tc)) 328 | } 329 | 330 | // More than 256 to reveal any 8-bit limitations. 331 | n := 300 332 | lineList := []string{ 333 | "", // Account for the initial empty element of the lines array. 334 | } 335 | var charList []index 336 | for x := 1; x < n+1; x++ { 337 | lineList = append(lineList, strconv.Itoa(x)+"\n") 338 | charList = append(charList, index(x)) 339 | } 340 | lines := strings.Join(lineList, "") 341 | chars := indexesToString(charList) 342 | assert.Equal(t, n, len(charList)) 343 | 344 | actualChars1, actualChars2, actualLines := dmp.DiffLinesToChars(lines, "") 345 | assert.Equal(t, chars, actualChars1) 346 | assert.Equal(t, "", actualChars2) 347 | assert.Equal(t, lineList, actualLines) 348 | } 349 | 350 | func TestDiffCharsToLines(t *testing.T) { 351 | type TestCase struct { 352 | Diffs []Diff 353 | Lines []string 354 | 355 | Expected []Diff 356 | } 357 | 358 | dmp := New() 359 | 360 | for i, tc := range []TestCase{ 361 | { 362 | Diffs: []Diff{ 363 | {DiffEqual, "\x01\x02\x01"}, 364 | {DiffInsert, "\x02\x01\x02"}, 365 | }, 366 | Lines: []string{"", "alpha\n", "beta\n"}, 367 | 368 | Expected: []Diff{ 369 | {DiffEqual, "alpha\nbeta\nalpha\n"}, 370 | {DiffInsert, "beta\nalpha\nbeta\n"}, 371 | }, 372 | }, 373 | } { 374 | actual := dmp.DiffCharsToLines(tc.Diffs, tc.Lines) 375 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 376 | } 377 | 378 | // More than 256 to reveal any 8-bit limitations. 379 | n := 300 380 | lineList := []string{ 381 | "", // Account for the initial empty element of the lines array. 382 | } 383 | charList := []index{} 384 | for x := 1; x <= n; x++ { 385 | lineList = append(lineList, strconv.Itoa(x)+"\n") 386 | charList = append(charList, index(x)) 387 | } 388 | assert.Equal(t, n, len(charList)) 389 | chars := indexesToString(charList) 390 | 391 | actual := dmp.DiffCharsToLines([]Diff{Diff{DiffDelete, chars}}, lineList) 392 | assert.Equal(t, []Diff{Diff{DiffDelete, strings.Join(lineList, "")}}, actual) 393 | } 394 | 395 | func TestDiffCleanupMerge(t *testing.T) { 396 | type TestCase struct { 397 | Name string 398 | 399 | Diffs []Diff 400 | 401 | Expected []Diff 402 | } 403 | 404 | dmp := New() 405 | 406 | for i, tc := range []TestCase{ 407 | { 408 | "Null case", 409 | []Diff{}, 410 | []Diff{}, 411 | }, 412 | { 413 | "No Diff case", 414 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "b"}, Diff{DiffInsert, "c"}}, 415 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "b"}, Diff{DiffInsert, "c"}}, 416 | }, 417 | { 418 | "Merge equalities", 419 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffEqual, "b"}, Diff{DiffEqual, "c"}}, 420 | []Diff{Diff{DiffEqual, "abc"}}, 421 | }, 422 | { 423 | "Merge deletions", 424 | []Diff{Diff{DiffDelete, "a"}, Diff{DiffDelete, "b"}, Diff{DiffDelete, "c"}}, 425 | []Diff{Diff{DiffDelete, "abc"}}, 426 | }, 427 | { 428 | "Merge insertions", 429 | []Diff{Diff{DiffInsert, "a"}, Diff{DiffInsert, "b"}, Diff{DiffInsert, "c"}}, 430 | []Diff{Diff{DiffInsert, "abc"}}, 431 | }, 432 | { 433 | "Merge interweave", 434 | []Diff{Diff{DiffDelete, "a"}, Diff{DiffInsert, "b"}, Diff{DiffDelete, "c"}, Diff{DiffInsert, "d"}, Diff{DiffEqual, "e"}, Diff{DiffEqual, "f"}}, 435 | []Diff{Diff{DiffDelete, "ac"}, Diff{DiffInsert, "bd"}, Diff{DiffEqual, "ef"}}, 436 | }, 437 | { 438 | "Prefix and suffix detection", 439 | []Diff{Diff{DiffDelete, "a"}, Diff{DiffInsert, "abc"}, Diff{DiffDelete, "dc"}}, 440 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "d"}, Diff{DiffInsert, "b"}, Diff{DiffEqual, "c"}}, 441 | }, 442 | { 443 | "Prefix and suffix detection with equalities", 444 | []Diff{Diff{DiffEqual, "x"}, Diff{DiffDelete, "a"}, Diff{DiffInsert, "abc"}, Diff{DiffDelete, "dc"}, Diff{DiffEqual, "y"}}, 445 | []Diff{Diff{DiffEqual, "xa"}, Diff{DiffDelete, "d"}, Diff{DiffInsert, "b"}, Diff{DiffEqual, "cy"}}, 446 | }, 447 | { 448 | "Same test as above but with unicode (\u0101 will appear in diffs with at least 257 unique lines)", 449 | []Diff{Diff{DiffEqual, "x"}, Diff{DiffDelete, "\u0101"}, Diff{DiffInsert, "\u0101bc"}, Diff{DiffDelete, "dc"}, Diff{DiffEqual, "y"}}, 450 | []Diff{Diff{DiffEqual, "x\u0101"}, Diff{DiffDelete, "d"}, Diff{DiffInsert, "b"}, Diff{DiffEqual, "cy"}}, 451 | }, 452 | { 453 | "Slide edit left", 454 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffInsert, "ba"}, Diff{DiffEqual, "c"}}, 455 | []Diff{Diff{DiffInsert, "ab"}, Diff{DiffEqual, "ac"}}, 456 | }, 457 | { 458 | "Slide edit right", 459 | []Diff{Diff{DiffEqual, "c"}, Diff{DiffInsert, "ab"}, Diff{DiffEqual, "a"}}, 460 | []Diff{Diff{DiffEqual, "ca"}, Diff{DiffInsert, "ba"}}, 461 | }, 462 | { 463 | "Slide edit left recursive", 464 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "b"}, Diff{DiffEqual, "c"}, Diff{DiffDelete, "ac"}, Diff{DiffEqual, "x"}}, 465 | []Diff{Diff{DiffDelete, "abc"}, Diff{DiffEqual, "acx"}}, 466 | }, 467 | { 468 | "Slide edit right recursive", 469 | []Diff{Diff{DiffEqual, "x"}, Diff{DiffDelete, "ca"}, Diff{DiffEqual, "c"}, Diff{DiffDelete, "b"}, Diff{DiffEqual, "a"}}, 470 | []Diff{Diff{DiffEqual, "xca"}, Diff{DiffDelete, "cba"}}, 471 | }, 472 | } { 473 | actual := dmp.DiffCleanupMerge(tc.Diffs) 474 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 475 | } 476 | } 477 | 478 | func TestDiffCleanupSemanticLossless(t *testing.T) { 479 | type TestCase struct { 480 | Name string 481 | 482 | Diffs []Diff 483 | 484 | Expected []Diff 485 | } 486 | 487 | dmp := New() 488 | 489 | for i, tc := range []TestCase{ 490 | { 491 | "Null case", 492 | []Diff{}, 493 | []Diff{}, 494 | }, 495 | { 496 | "Blank lines", 497 | []Diff{ 498 | Diff{DiffEqual, "AAA\r\n\r\nBBB"}, 499 | Diff{DiffInsert, "\r\nDDD\r\n\r\nBBB"}, 500 | Diff{DiffEqual, "\r\nEEE"}, 501 | }, 502 | []Diff{ 503 | Diff{DiffEqual, "AAA\r\n\r\n"}, 504 | Diff{DiffInsert, "BBB\r\nDDD\r\n\r\n"}, 505 | Diff{DiffEqual, "BBB\r\nEEE"}, 506 | }, 507 | }, 508 | { 509 | "Line boundaries", 510 | []Diff{ 511 | Diff{DiffEqual, "AAA\r\nBBB"}, 512 | Diff{DiffInsert, " DDD\r\nBBB"}, 513 | Diff{DiffEqual, " EEE"}, 514 | }, 515 | []Diff{ 516 | Diff{DiffEqual, "AAA\r\n"}, 517 | Diff{DiffInsert, "BBB DDD\r\n"}, 518 | Diff{DiffEqual, "BBB EEE"}, 519 | }, 520 | }, 521 | { 522 | "Word boundaries", 523 | []Diff{ 524 | Diff{DiffEqual, "The c"}, 525 | Diff{DiffInsert, "ow and the c"}, 526 | Diff{DiffEqual, "at."}, 527 | }, 528 | []Diff{ 529 | Diff{DiffEqual, "The "}, 530 | Diff{DiffInsert, "cow and the "}, 531 | Diff{DiffEqual, "cat."}, 532 | }, 533 | }, 534 | { 535 | "Alphanumeric boundaries", 536 | []Diff{ 537 | Diff{DiffEqual, "The-c"}, 538 | Diff{DiffInsert, "ow-and-the-c"}, 539 | Diff{DiffEqual, "at."}, 540 | }, 541 | []Diff{ 542 | Diff{DiffEqual, "The-"}, 543 | Diff{DiffInsert, "cow-and-the-"}, 544 | Diff{DiffEqual, "cat."}, 545 | }, 546 | }, 547 | { 548 | "Hitting the start", 549 | []Diff{ 550 | Diff{DiffEqual, "a"}, 551 | Diff{DiffDelete, "a"}, 552 | Diff{DiffEqual, "ax"}, 553 | }, 554 | []Diff{ 555 | Diff{DiffDelete, "a"}, 556 | Diff{DiffEqual, "aax"}, 557 | }, 558 | }, 559 | { 560 | "Hitting the end", 561 | []Diff{ 562 | Diff{DiffEqual, "xa"}, 563 | Diff{DiffDelete, "a"}, 564 | Diff{DiffEqual, "a"}, 565 | }, 566 | []Diff{ 567 | Diff{DiffEqual, "xaa"}, 568 | Diff{DiffDelete, "a"}, 569 | }, 570 | }, 571 | { 572 | "Sentence boundaries", 573 | []Diff{ 574 | Diff{DiffEqual, "The xxx. The "}, 575 | Diff{DiffInsert, "zzz. The "}, 576 | Diff{DiffEqual, "yyy."}, 577 | }, 578 | []Diff{ 579 | Diff{DiffEqual, "The xxx."}, 580 | Diff{DiffInsert, " The zzz."}, 581 | Diff{DiffEqual, " The yyy."}, 582 | }, 583 | }, 584 | { 585 | "UTF-8 strings", 586 | []Diff{ 587 | Diff{DiffEqual, "The ♕. The "}, 588 | Diff{DiffInsert, "♔. The "}, 589 | Diff{DiffEqual, "♖."}, 590 | }, 591 | []Diff{ 592 | Diff{DiffEqual, "The ♕."}, 593 | Diff{DiffInsert, " The ♔."}, 594 | Diff{DiffEqual, " The ♖."}, 595 | }, 596 | }, 597 | { 598 | "Rune boundaries", 599 | []Diff{ 600 | Diff{DiffEqual, "♕♕"}, 601 | Diff{DiffInsert, "♔♔"}, 602 | Diff{DiffEqual, "♖♖"}, 603 | }, 604 | []Diff{ 605 | Diff{DiffEqual, "♕♕"}, 606 | Diff{DiffInsert, "♔♔"}, 607 | Diff{DiffEqual, "♖♖"}, 608 | }, 609 | }, 610 | } { 611 | actual := dmp.DiffCleanupSemanticLossless(tc.Diffs) 612 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 613 | } 614 | } 615 | 616 | func TestDiffCleanupSemantic(t *testing.T) { 617 | type TestCase struct { 618 | Name string 619 | 620 | Diffs []Diff 621 | 622 | Expected []Diff 623 | } 624 | 625 | dmp := New() 626 | 627 | for i, tc := range []TestCase{ 628 | { 629 | "Null case", 630 | []Diff{}, 631 | []Diff{}, 632 | }, 633 | { 634 | "No elimination #1", 635 | []Diff{ 636 | {DiffDelete, "ab"}, 637 | {DiffInsert, "cd"}, 638 | {DiffEqual, "12"}, 639 | {DiffDelete, "e"}, 640 | }, 641 | []Diff{ 642 | {DiffDelete, "ab"}, 643 | {DiffInsert, "cd"}, 644 | {DiffEqual, "12"}, 645 | {DiffDelete, "e"}, 646 | }, 647 | }, 648 | { 649 | "No elimination #2", 650 | []Diff{ 651 | {DiffDelete, "abc"}, 652 | {DiffInsert, "ABC"}, 653 | {DiffEqual, "1234"}, 654 | {DiffDelete, "wxyz"}, 655 | }, 656 | []Diff{ 657 | {DiffDelete, "abc"}, 658 | {DiffInsert, "ABC"}, 659 | {DiffEqual, "1234"}, 660 | {DiffDelete, "wxyz"}, 661 | }, 662 | }, 663 | { 664 | "No elimination #3", 665 | []Diff{ 666 | {DiffEqual, "2016-09-01T03:07:1"}, 667 | {DiffInsert, "5.15"}, 668 | {DiffEqual, "4"}, 669 | {DiffDelete, "."}, 670 | {DiffEqual, "80"}, 671 | {DiffInsert, "0"}, 672 | {DiffEqual, "78"}, 673 | {DiffDelete, "3074"}, 674 | {DiffEqual, "1Z"}, 675 | }, 676 | []Diff{ 677 | {DiffEqual, "2016-09-01T03:07:1"}, 678 | {DiffInsert, "5.15"}, 679 | {DiffEqual, "4"}, 680 | {DiffDelete, "."}, 681 | {DiffEqual, "80"}, 682 | {DiffInsert, "0"}, 683 | {DiffEqual, "78"}, 684 | {DiffDelete, "3074"}, 685 | {DiffEqual, "1Z"}, 686 | }, 687 | }, 688 | { 689 | "Simple elimination", 690 | []Diff{ 691 | {DiffDelete, "a"}, 692 | {DiffEqual, "b"}, 693 | {DiffDelete, "c"}, 694 | }, 695 | []Diff{ 696 | {DiffDelete, "abc"}, 697 | {DiffInsert, "b"}, 698 | }, 699 | }, 700 | { 701 | "Backpass elimination", 702 | []Diff{ 703 | {DiffDelete, "ab"}, 704 | {DiffEqual, "cd"}, 705 | {DiffDelete, "e"}, 706 | {DiffEqual, "f"}, 707 | {DiffInsert, "g"}, 708 | }, 709 | []Diff{ 710 | {DiffDelete, "abcdef"}, 711 | {DiffInsert, "cdfg"}, 712 | }, 713 | }, 714 | { 715 | "Multiple eliminations", 716 | []Diff{ 717 | {DiffInsert, "1"}, 718 | {DiffEqual, "A"}, 719 | {DiffDelete, "B"}, 720 | {DiffInsert, "2"}, 721 | {DiffEqual, "_"}, 722 | {DiffInsert, "1"}, 723 | {DiffEqual, "A"}, 724 | {DiffDelete, "B"}, 725 | {DiffInsert, "2"}, 726 | }, 727 | []Diff{ 728 | {DiffDelete, "AB_AB"}, 729 | {DiffInsert, "1A2_1A2"}, 730 | }, 731 | }, 732 | { 733 | "Word boundaries", 734 | []Diff{ 735 | {DiffEqual, "The c"}, 736 | {DiffDelete, "ow and the c"}, 737 | {DiffEqual, "at."}, 738 | }, 739 | []Diff{ 740 | {DiffEqual, "The "}, 741 | {DiffDelete, "cow and the "}, 742 | {DiffEqual, "cat."}, 743 | }, 744 | }, 745 | { 746 | "No overlap elimination", 747 | []Diff{ 748 | {DiffDelete, "abcxx"}, 749 | {DiffInsert, "xxdef"}, 750 | }, 751 | []Diff{ 752 | {DiffDelete, "abcxx"}, 753 | {DiffInsert, "xxdef"}, 754 | }, 755 | }, 756 | { 757 | "Overlap elimination", 758 | []Diff{ 759 | {DiffDelete, "abcxxx"}, 760 | {DiffInsert, "xxxdef"}, 761 | }, 762 | []Diff{ 763 | {DiffDelete, "abc"}, 764 | {DiffEqual, "xxx"}, 765 | {DiffInsert, "def"}, 766 | }, 767 | }, 768 | { 769 | "Reverse overlap elimination", 770 | []Diff{ 771 | {DiffDelete, "xxxabc"}, 772 | {DiffInsert, "defxxx"}, 773 | }, 774 | []Diff{ 775 | {DiffInsert, "def"}, 776 | {DiffEqual, "xxx"}, 777 | {DiffDelete, "abc"}, 778 | }, 779 | }, 780 | { 781 | "Two overlap eliminations", 782 | []Diff{ 783 | {DiffDelete, "abcd1212"}, 784 | {DiffInsert, "1212efghi"}, 785 | {DiffEqual, "----"}, 786 | {DiffDelete, "A3"}, 787 | {DiffInsert, "3BC"}, 788 | }, 789 | []Diff{ 790 | {DiffDelete, "abcd"}, 791 | {DiffEqual, "1212"}, 792 | {DiffInsert, "efghi"}, 793 | {DiffEqual, "----"}, 794 | {DiffDelete, "A"}, 795 | {DiffEqual, "3"}, 796 | {DiffInsert, "BC"}, 797 | }, 798 | }, 799 | { 800 | "Test case for adapting DiffCleanupSemantic to be equal to the Python version #19", 801 | []Diff{ 802 | {DiffEqual, "James McCarthy "}, 803 | {DiffDelete, "close to "}, 804 | {DiffEqual, "sign"}, 805 | {DiffDelete, "ing"}, 806 | {DiffInsert, "s"}, 807 | {DiffEqual, " new "}, 808 | {DiffDelete, "E"}, 809 | {DiffInsert, "fi"}, 810 | {DiffEqual, "ve"}, 811 | {DiffInsert, "-yea"}, 812 | {DiffEqual, "r"}, 813 | {DiffDelete, "ton"}, 814 | {DiffEqual, " deal"}, 815 | {DiffInsert, " at Everton"}, 816 | }, 817 | []Diff{ 818 | {DiffEqual, "James McCarthy "}, 819 | {DiffDelete, "close to "}, 820 | {DiffEqual, "sign"}, 821 | {DiffDelete, "ing"}, 822 | {DiffInsert, "s"}, 823 | {DiffEqual, " new "}, 824 | {DiffInsert, "five-year deal at "}, 825 | {DiffEqual, "Everton"}, 826 | {DiffDelete, " deal"}, 827 | }, 828 | }, 829 | { 830 | "Taken from python / CPP library", 831 | []Diff{ 832 | {DiffInsert, "星球大戰:新的希望 "}, 833 | {DiffEqual, "star wars: "}, 834 | {DiffDelete, "episodio iv - un"}, 835 | {DiffEqual, "a n"}, 836 | {DiffDelete, "u"}, 837 | {DiffEqual, "e"}, 838 | {DiffDelete, "va"}, 839 | {DiffInsert, "w"}, 840 | {DiffEqual, " "}, 841 | {DiffDelete, "es"}, 842 | {DiffInsert, "ho"}, 843 | {DiffEqual, "pe"}, 844 | {DiffDelete, "ranza"}, 845 | }, 846 | []Diff{ 847 | {DiffInsert, "星球大戰:新的希望 "}, 848 | {DiffEqual, "star wars: "}, 849 | {DiffDelete, "episodio iv - una nueva esperanza"}, 850 | {DiffInsert, "a new hope"}, 851 | }, 852 | }, 853 | { 854 | "panic", 855 | []Diff{ 856 | {DiffInsert, "킬러 인 "}, 857 | {DiffEqual, "리커버리"}, 858 | {DiffDelete, " 보이즈"}, 859 | }, 860 | []Diff{ 861 | {DiffInsert, "킬러 인 "}, 862 | {DiffEqual, "리커버리"}, 863 | {DiffDelete, " 보이즈"}, 864 | }, 865 | }, 866 | } { 867 | actual := dmp.DiffCleanupSemantic(tc.Diffs) 868 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 869 | } 870 | } 871 | 872 | func BenchmarkDiffCleanupSemantic(b *testing.B) { 873 | s1, s2 := speedtestTexts() 874 | 875 | dmp := New() 876 | 877 | diffs := dmp.DiffMain(s1, s2, false) 878 | 879 | b.ResetTimer() 880 | 881 | for i := 0; i < b.N; i++ { 882 | dmp.DiffCleanupSemantic(diffs) 883 | } 884 | } 885 | 886 | func TestDiffCleanupEfficiency(t *testing.T) { 887 | type TestCase struct { 888 | Name string 889 | 890 | Diffs []Diff 891 | 892 | Expected []Diff 893 | } 894 | 895 | dmp := New() 896 | dmp.DiffEditCost = 4 897 | 898 | for i, tc := range []TestCase{ 899 | { 900 | "Null case", 901 | []Diff{}, 902 | []Diff{}, 903 | }, 904 | { 905 | "No elimination", 906 | []Diff{ 907 | Diff{DiffDelete, "ab"}, 908 | Diff{DiffInsert, "12"}, 909 | Diff{DiffEqual, "wxyz"}, 910 | Diff{DiffDelete, "cd"}, 911 | Diff{DiffInsert, "34"}, 912 | }, 913 | []Diff{ 914 | Diff{DiffDelete, "ab"}, 915 | Diff{DiffInsert, "12"}, 916 | Diff{DiffEqual, "wxyz"}, 917 | Diff{DiffDelete, "cd"}, 918 | Diff{DiffInsert, "34"}, 919 | }, 920 | }, 921 | { 922 | "Four-edit elimination", 923 | []Diff{ 924 | Diff{DiffDelete, "ab"}, 925 | Diff{DiffInsert, "12"}, 926 | Diff{DiffEqual, "xyz"}, 927 | Diff{DiffDelete, "cd"}, 928 | Diff{DiffInsert, "34"}, 929 | }, 930 | []Diff{ 931 | Diff{DiffDelete, "abxyzcd"}, 932 | Diff{DiffInsert, "12xyz34"}, 933 | }, 934 | }, 935 | { 936 | "Three-edit elimination", 937 | []Diff{ 938 | Diff{DiffInsert, "12"}, 939 | Diff{DiffEqual, "x"}, 940 | Diff{DiffDelete, "cd"}, 941 | Diff{DiffInsert, "34"}, 942 | }, 943 | []Diff{ 944 | Diff{DiffDelete, "xcd"}, 945 | Diff{DiffInsert, "12x34"}, 946 | }, 947 | }, 948 | { 949 | "Backpass elimination", 950 | []Diff{ 951 | Diff{DiffDelete, "ab"}, 952 | Diff{DiffInsert, "12"}, 953 | Diff{DiffEqual, "xy"}, 954 | Diff{DiffInsert, "34"}, 955 | Diff{DiffEqual, "z"}, 956 | Diff{DiffDelete, "cd"}, 957 | Diff{DiffInsert, "56"}, 958 | }, 959 | []Diff{ 960 | Diff{DiffDelete, "abxyzcd"}, 961 | Diff{DiffInsert, "12xy34z56"}, 962 | }, 963 | }, 964 | } { 965 | actual := dmp.DiffCleanupEfficiency(tc.Diffs) 966 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 967 | } 968 | 969 | dmp.DiffEditCost = 5 970 | 971 | for i, tc := range []TestCase{ 972 | { 973 | "High cost elimination", 974 | []Diff{ 975 | Diff{DiffDelete, "ab"}, 976 | Diff{DiffInsert, "12"}, 977 | Diff{DiffEqual, "wxyz"}, 978 | Diff{DiffDelete, "cd"}, 979 | Diff{DiffInsert, "34"}, 980 | }, 981 | []Diff{ 982 | Diff{DiffDelete, "abwxyzcd"}, 983 | Diff{DiffInsert, "12wxyz34"}, 984 | }, 985 | }, 986 | } { 987 | actual := dmp.DiffCleanupEfficiency(tc.Diffs) 988 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 989 | } 990 | } 991 | 992 | func TestDiffPrettyHtml(t *testing.T) { 993 | type TestCase struct { 994 | Diffs []Diff 995 | 996 | Expected string 997 | } 998 | 999 | dmp := New() 1000 | 1001 | for i, tc := range []TestCase{ 1002 | { 1003 | Diffs: []Diff{ 1004 | {DiffEqual, "a\n"}, 1005 | {DiffDelete, "b"}, 1006 | {DiffInsert, "c&d"}, 1007 | }, 1008 | 1009 | Expected: "
<B>b</B>c&d", 1010 | }, 1011 | } { 1012 | actual := dmp.DiffPrettyHtml(tc.Diffs) 1013 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1014 | } 1015 | } 1016 | 1017 | func TestDiffPrettyText(t *testing.T) { 1018 | type TestCase struct { 1019 | Diffs []Diff 1020 | 1021 | Expected string 1022 | } 1023 | 1024 | dmp := New() 1025 | 1026 | for i, tc := range []TestCase{ 1027 | { 1028 | Diffs: []Diff{ 1029 | {DiffEqual, "a\n"}, 1030 | {DiffDelete, "b"}, 1031 | {DiffInsert, "c&d"}, 1032 | }, 1033 | 1034 | Expected: "a\n\x1b[31mb\x1b[0m\x1b[32mc&d\x1b[0m", 1035 | }, 1036 | { 1037 | Diffs: []Diff{ 1038 | {Type: DiffEqual, Text: "a\n"}, 1039 | {Type: DiffDelete, Text: "b\nc\n"}, 1040 | {Type: DiffEqual, Text: "def"}, 1041 | {Type: DiffInsert, Text: "\ng\nh"}, 1042 | {Type: DiffEqual, Text: "\ni"}, 1043 | }, 1044 | 1045 | Expected: "a\n\x1b[31mb\x1b[0m\n\x1b[31mc\x1b[0m\n\x1b[31m\x1b[0mdef\x1b[32m\x1b[0m\n\x1b[32mg\x1b[0m\n\x1b[32mh\x1b[0m\ni", 1046 | }, 1047 | } { 1048 | actual := dmp.DiffPrettyText(tc.Diffs) 1049 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1050 | } 1051 | } 1052 | 1053 | func TestDiffText(t *testing.T) { 1054 | type TestCase struct { 1055 | Diffs []Diff 1056 | 1057 | ExpectedText1 string 1058 | ExpectedText2 string 1059 | } 1060 | 1061 | dmp := New() 1062 | 1063 | for i, tc := range []TestCase{ 1064 | { 1065 | Diffs: []Diff{ 1066 | {DiffEqual, "jump"}, 1067 | {DiffDelete, "s"}, 1068 | {DiffInsert, "ed"}, 1069 | {DiffEqual, " over "}, 1070 | {DiffDelete, "the"}, 1071 | {DiffInsert, "a"}, 1072 | {DiffEqual, " lazy"}, 1073 | }, 1074 | 1075 | ExpectedText1: "jumps over the lazy", 1076 | ExpectedText2: "jumped over a lazy", 1077 | }, 1078 | } { 1079 | actualText1 := dmp.DiffText1(tc.Diffs) 1080 | assert.Equal(t, tc.ExpectedText1, actualText1, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1081 | 1082 | actualText2 := dmp.DiffText2(tc.Diffs) 1083 | assert.Equal(t, tc.ExpectedText2, actualText2, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1084 | } 1085 | } 1086 | 1087 | func TestDiffDelta(t *testing.T) { 1088 | type TestCase struct { 1089 | Name string 1090 | 1091 | Text string 1092 | Delta string 1093 | 1094 | ErrorMessagePrefix string 1095 | } 1096 | 1097 | dmp := New() 1098 | 1099 | for i, tc := range []TestCase{ 1100 | {"Delta shorter than text", "jumps over the lazyx", "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", "Delta length (19) is different from source text length (20)"}, 1101 | {"Delta longer than text", "umps over the lazy", "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", "Delta length (19) is different from source text length (18)"}, 1102 | {"Invalid URL escaping", "", "+%c3%xy", "invalid URL escape \"%xy\""}, 1103 | {"Invalid UTF-8 sequence", "", "+%c3xy", "invalid UTF-8 token: \"\\xc3xy\""}, 1104 | {"Invalid diff operation", "", "a", "Invalid diff operation in DiffFromDelta: a"}, 1105 | {"Invalid diff syntax", "", "-", "strconv.ParseInt: parsing \"\": invalid syntax"}, 1106 | {"Negative number in delta", "", "--1", "Negative number in DiffFromDelta: -1"}, 1107 | {"Empty case", "", "", ""}, 1108 | } { 1109 | diffs, err := dmp.DiffFromDelta(tc.Text, tc.Delta) 1110 | msg := fmt.Sprintf("Test case #%d, %s", i, tc.Name) 1111 | if tc.ErrorMessagePrefix == "" { 1112 | assert.Nil(t, err, msg) 1113 | assert.Nil(t, diffs, msg) 1114 | } else { 1115 | e := err.Error() 1116 | if strings.HasPrefix(e, tc.ErrorMessagePrefix) { 1117 | e = tc.ErrorMessagePrefix 1118 | } 1119 | assert.Nil(t, diffs, msg) 1120 | assert.Equal(t, tc.ErrorMessagePrefix, e, msg) 1121 | } 1122 | } 1123 | 1124 | // Convert a diff into delta string. 1125 | diffs := []Diff{ 1126 | Diff{DiffEqual, "jump"}, 1127 | Diff{DiffDelete, "s"}, 1128 | Diff{DiffInsert, "ed"}, 1129 | Diff{DiffEqual, " over "}, 1130 | Diff{DiffDelete, "the"}, 1131 | Diff{DiffInsert, "a"}, 1132 | Diff{DiffEqual, " lazy"}, 1133 | Diff{DiffInsert, "old dog"}, 1134 | } 1135 | text1 := dmp.DiffText1(diffs) 1136 | assert.Equal(t, "jumps over the lazy", text1) 1137 | 1138 | delta := dmp.DiffToDelta(diffs) 1139 | assert.Equal(t, "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta) 1140 | 1141 | // Convert delta string into a diff. 1142 | deltaDiffs, err := dmp.DiffFromDelta(text1, delta) 1143 | assert.Equal(t, diffs, deltaDiffs) 1144 | 1145 | // Test deltas with special characters. 1146 | diffs = []Diff{ 1147 | Diff{DiffEqual, "\u0680 \x00 \t %"}, 1148 | Diff{DiffDelete, "\u0681 \x01 \n ^"}, 1149 | Diff{DiffInsert, "\u0682 \x02 \\ |"}, 1150 | } 1151 | text1 = dmp.DiffText1(diffs) 1152 | assert.Equal(t, "\u0680 \x00 \t %\u0681 \x01 \n ^", text1) 1153 | 1154 | // Lowercase, due to UrlEncode uses lower. 1155 | delta = dmp.DiffToDelta(diffs) 1156 | assert.Equal(t, "=7\t-7\t+%DA%82 %02 %5C %7C", delta) 1157 | 1158 | deltaDiffs, err = dmp.DiffFromDelta(text1, delta) 1159 | assert.Equal(t, diffs, deltaDiffs) 1160 | assert.Nil(t, err) 1161 | 1162 | // Verify pool of unchanged characters. 1163 | diffs = []Diff{ 1164 | Diff{DiffInsert, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "}, 1165 | } 1166 | 1167 | delta = dmp.DiffToDelta(diffs) 1168 | assert.Equal(t, "+A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # ", delta, "Unchanged characters.") 1169 | 1170 | // Convert delta string into a diff. 1171 | deltaDiffs, err = dmp.DiffFromDelta("", delta) 1172 | assert.Equal(t, diffs, deltaDiffs) 1173 | assert.Nil(t, err) 1174 | } 1175 | 1176 | func TestDiffXIndex(t *testing.T) { 1177 | type TestCase struct { 1178 | Name string 1179 | 1180 | Diffs []Diff 1181 | Location int 1182 | 1183 | Expected int 1184 | } 1185 | 1186 | dmp := New() 1187 | 1188 | for i, tc := range []TestCase{ 1189 | {"Translation on equality", []Diff{{DiffDelete, "a"}, {DiffInsert, "1234"}, {DiffEqual, "xyz"}}, 2, 5}, 1190 | {"Translation on deletion", []Diff{{DiffEqual, "a"}, {DiffDelete, "1234"}, {DiffEqual, "xyz"}}, 3, 1}, 1191 | } { 1192 | actual := dmp.DiffXIndex(tc.Diffs, tc.Location) 1193 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 1194 | } 1195 | } 1196 | 1197 | func TestDiffLevenshtein(t *testing.T) { 1198 | type TestCase struct { 1199 | Name string 1200 | 1201 | Diffs []Diff 1202 | 1203 | Expected int 1204 | } 1205 | 1206 | dmp := New() 1207 | 1208 | for i, tc := range []TestCase{ 1209 | {"Levenshtein with trailing equality", []Diff{{DiffDelete, "абв"}, {DiffInsert, "1234"}, {DiffEqual, "эюя"}}, 4}, 1210 | {"Levenshtein with leading equality", []Diff{{DiffEqual, "эюя"}, {DiffDelete, "абв"}, {DiffInsert, "1234"}}, 4}, 1211 | {"Levenshtein with middle equality", []Diff{{DiffDelete, "абв"}, {DiffEqual, "эюя"}, {DiffInsert, "1234"}}, 7}, 1212 | } { 1213 | actual := dmp.DiffLevenshtein(tc.Diffs) 1214 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 1215 | } 1216 | } 1217 | 1218 | func TestDiffBisect(t *testing.T) { 1219 | type TestCase struct { 1220 | Name string 1221 | 1222 | Time time.Time 1223 | 1224 | Expected []Diff 1225 | } 1226 | 1227 | dmp := New() 1228 | 1229 | for i, tc := range []TestCase{ 1230 | { 1231 | Name: "normal", 1232 | Time: time.Date(9999, time.December, 31, 23, 59, 59, 59, time.UTC), 1233 | 1234 | Expected: []Diff{ 1235 | {DiffDelete, "c"}, 1236 | {DiffInsert, "m"}, 1237 | {DiffEqual, "a"}, 1238 | {DiffDelete, "t"}, 1239 | {DiffInsert, "p"}, 1240 | }, 1241 | }, 1242 | { 1243 | Name: "Negative deadlines count as having infinite time", 1244 | Time: time.Date(0001, time.January, 01, 00, 00, 00, 00, time.UTC), 1245 | 1246 | Expected: []Diff{ 1247 | {DiffDelete, "c"}, 1248 | {DiffInsert, "m"}, 1249 | {DiffEqual, "a"}, 1250 | {DiffDelete, "t"}, 1251 | {DiffInsert, "p"}, 1252 | }, 1253 | }, 1254 | { 1255 | Name: "Timeout", 1256 | Time: time.Now().Add(time.Nanosecond), 1257 | 1258 | Expected: []Diff{ 1259 | {DiffDelete, "cat"}, 1260 | {DiffInsert, "map"}, 1261 | }, 1262 | }, 1263 | } { 1264 | actual := dmp.DiffBisect("cat", "map", tc.Time) 1265 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 1266 | } 1267 | 1268 | // Test for invalid UTF-8 sequences 1269 | assert.Equal(t, []Diff{ 1270 | Diff{DiffEqual, "��"}, 1271 | }, dmp.DiffBisect("\xe0\xe5", "\xe0\xe5", time.Now().Add(time.Minute))) 1272 | } 1273 | 1274 | func TestDiffMain(t *testing.T) { 1275 | type TestCase struct { 1276 | Text1 string 1277 | Text2 string 1278 | 1279 | Expected []Diff 1280 | } 1281 | 1282 | dmp := New() 1283 | 1284 | // Perform a trivial diff. 1285 | for i, tc := range []TestCase{ 1286 | { 1287 | "", 1288 | "", 1289 | nil, 1290 | }, 1291 | { 1292 | "abc", 1293 | "abc", 1294 | []Diff{Diff{DiffEqual, "abc"}}, 1295 | }, 1296 | { 1297 | "abc", 1298 | "ab123c", 1299 | []Diff{Diff{DiffEqual, "ab"}, Diff{DiffInsert, "123"}, Diff{DiffEqual, "c"}}, 1300 | }, 1301 | { 1302 | "a123bc", 1303 | "abc", 1304 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "123"}, Diff{DiffEqual, "bc"}}, 1305 | }, 1306 | { 1307 | "abc", 1308 | "a123b456c", 1309 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffInsert, "123"}, Diff{DiffEqual, "b"}, Diff{DiffInsert, "456"}, Diff{DiffEqual, "c"}}, 1310 | }, 1311 | { 1312 | "a123b456c", 1313 | "abc", 1314 | []Diff{Diff{DiffEqual, "a"}, Diff{DiffDelete, "123"}, Diff{DiffEqual, "b"}, Diff{DiffDelete, "456"}, Diff{DiffEqual, "c"}}, 1315 | }, 1316 | } { 1317 | actual := dmp.DiffMain(tc.Text1, tc.Text2, false) 1318 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1319 | } 1320 | 1321 | // Perform a real diff and switch off the timeout. 1322 | dmp.DiffTimeout = 0 1323 | 1324 | for i, tc := range []TestCase{ 1325 | { 1326 | "a", 1327 | "b", 1328 | []Diff{Diff{DiffDelete, "a"}, Diff{DiffInsert, "b"}}, 1329 | }, 1330 | { 1331 | "Apples are a fruit.", 1332 | "Bananas are also fruit.", 1333 | []Diff{ 1334 | Diff{DiffDelete, "Apple"}, 1335 | Diff{DiffInsert, "Banana"}, 1336 | Diff{DiffEqual, "s are a"}, 1337 | Diff{DiffInsert, "lso"}, 1338 | Diff{DiffEqual, " fruit."}, 1339 | }, 1340 | }, 1341 | { 1342 | "ax\t", 1343 | "\u0680x\u0000", 1344 | []Diff{ 1345 | Diff{DiffDelete, "a"}, 1346 | Diff{DiffInsert, "\u0680"}, 1347 | Diff{DiffEqual, "x"}, 1348 | Diff{DiffDelete, "\t"}, 1349 | Diff{DiffInsert, "\u0000"}, 1350 | }, 1351 | }, 1352 | { 1353 | "1ayb2", 1354 | "abxab", 1355 | []Diff{ 1356 | Diff{DiffDelete, "1"}, 1357 | Diff{DiffEqual, "a"}, 1358 | Diff{DiffDelete, "y"}, 1359 | Diff{DiffEqual, "b"}, 1360 | Diff{DiffDelete, "2"}, 1361 | Diff{DiffInsert, "xab"}, 1362 | }, 1363 | }, 1364 | { 1365 | "abcy", 1366 | "xaxcxabc", 1367 | []Diff{ 1368 | Diff{DiffInsert, "xaxcx"}, 1369 | Diff{DiffEqual, "abc"}, Diff{DiffDelete, "y"}, 1370 | }, 1371 | }, 1372 | { 1373 | "ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg", 1374 | "a-bcd-efghijklmnopqrs", 1375 | []Diff{ 1376 | Diff{DiffDelete, "ABCD"}, 1377 | Diff{DiffEqual, "a"}, 1378 | Diff{DiffDelete, "="}, 1379 | Diff{DiffInsert, "-"}, 1380 | Diff{DiffEqual, "bcd"}, 1381 | Diff{DiffDelete, "="}, 1382 | Diff{DiffInsert, "-"}, 1383 | Diff{DiffEqual, "efghijklmnopqrs"}, 1384 | Diff{DiffDelete, "EFGHIJKLMNOefg"}, 1385 | }, 1386 | }, 1387 | { 1388 | "a [[Pennsylvania]] and [[New", 1389 | " and [[Pennsylvania]]", 1390 | []Diff{ 1391 | Diff{DiffInsert, " "}, 1392 | Diff{DiffEqual, "a"}, 1393 | Diff{DiffInsert, "nd"}, 1394 | Diff{DiffEqual, " [[Pennsylvania]]"}, 1395 | Diff{DiffDelete, " and [[New"}, 1396 | }, 1397 | }, 1398 | } { 1399 | actual := dmp.DiffMain(tc.Text1, tc.Text2, false) 1400 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1401 | } 1402 | 1403 | // Test for invalid UTF-8 sequences 1404 | assert.Equal(t, []Diff{ 1405 | Diff{DiffDelete, "��"}, 1406 | }, dmp.DiffMain("\xe0\xe5", "", false)) 1407 | } 1408 | 1409 | func TestDiffMainWithTimeout(t *testing.T) { 1410 | dmp := New() 1411 | dmp.DiffTimeout = 200 * time.Millisecond 1412 | 1413 | a := "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n" 1414 | b := "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n" 1415 | // Increase the text lengths by 1024 times to ensure a timeout. 1416 | for x := 0; x < 13; x++ { 1417 | a = a + a 1418 | b = b + b 1419 | } 1420 | 1421 | startTime := time.Now() 1422 | dmp.DiffMain(a, b, true) 1423 | endTime := time.Now() 1424 | 1425 | delta := endTime.Sub(startTime) 1426 | 1427 | // Test that we took at least the timeout period. 1428 | assert.True(t, delta >= dmp.DiffTimeout, fmt.Sprintf("%v !>= %v", delta, dmp.DiffTimeout)) 1429 | 1430 | // Test that we didn't take forever (be very forgiving). Theoretically this test could fail very occasionally if the OS task swaps or locks up for a second at the wrong moment. 1431 | assert.True(t, delta < (dmp.DiffTimeout*100), fmt.Sprintf("%v !< %v", delta, dmp.DiffTimeout*100)) 1432 | } 1433 | 1434 | func TestDiffMainWithCheckLines(t *testing.T) { 1435 | type TestCase struct { 1436 | Text1 string 1437 | Text2 string 1438 | } 1439 | 1440 | dmp := New() 1441 | dmp.DiffTimeout = 0 1442 | 1443 | // Test cases must be at least 100 chars long to pass the cutoff. 1444 | for i, tc := range []TestCase{ 1445 | { 1446 | "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n", 1447 | "abcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\n", 1448 | }, 1449 | { 1450 | "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 1451 | "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij", 1452 | }, 1453 | { 1454 | "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n", 1455 | "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n", 1456 | }, 1457 | } { 1458 | resultWithoutCheckLines := dmp.DiffMain(tc.Text1, tc.Text2, false) 1459 | resultWithCheckLines := dmp.DiffMain(tc.Text1, tc.Text2, true) 1460 | 1461 | // TODO this fails for the third test case, why? 1462 | if i != 2 { 1463 | assert.Equal(t, resultWithoutCheckLines, resultWithCheckLines, fmt.Sprintf("Test case #%d, %#v", i, tc)) 1464 | } 1465 | assert.Equal(t, diffRebuildTexts(resultWithoutCheckLines), diffRebuildTexts(resultWithCheckLines), fmt.Sprintf("Test case #%d, %#v", i, tc)) 1466 | } 1467 | } 1468 | 1469 | func TestMassiveRuneDiffConversion(t *testing.T) { 1470 | sNew, err := ioutil.ReadFile("../testdata/fixture.go") 1471 | if err != nil { 1472 | panic(err) 1473 | } 1474 | 1475 | dmp := New() 1476 | t1, t2, tt := dmp.DiffLinesToChars("", string(sNew)) 1477 | diffs := dmp.DiffMain(t1, t2, false) 1478 | diffs = dmp.DiffCharsToLines(diffs, tt) 1479 | assert.NotEmpty(t, diffs) 1480 | } 1481 | 1482 | func TestDiffPartialLineIndex(t *testing.T) { 1483 | dmp := New() 1484 | t1, t2, tt := dmp.DiffLinesToChars( 1485 | `line 1 1486 | line 2 1487 | line 3 1488 | line 4 1489 | line 5 1490 | line 6 1491 | line 7 1492 | line 8 1493 | line 9 1494 | line 10 text1`, 1495 | `line 1 1496 | line 2 1497 | line 3 1498 | line 4 1499 | line 5 1500 | line 6 1501 | line 7 1502 | line 8 1503 | line 9 1504 | line 10 text2`) 1505 | diffs := dmp.DiffMain(t1, t2, false) 1506 | diffs = dmp.DiffCharsToLines(diffs, tt) 1507 | assert.Equal(t, []Diff{ 1508 | Diff{DiffEqual, "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\n"}, 1509 | Diff{DiffDelete, "line 10 text1"}, 1510 | Diff{DiffInsert, "line 10 text2"}, 1511 | }, diffs) 1512 | } 1513 | 1514 | func BenchmarkDiffMain(bench *testing.B) { 1515 | s1 := "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n" 1516 | s2 := "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n" 1517 | 1518 | // Increase the text lengths by 1024 times to ensure a timeout. 1519 | for x := 0; x < 10; x++ { 1520 | s1 = s1 + s1 1521 | s2 = s2 + s2 1522 | } 1523 | 1524 | dmp := New() 1525 | dmp.DiffTimeout = time.Second 1526 | 1527 | bench.ResetTimer() 1528 | 1529 | for i := 0; i < bench.N; i++ { 1530 | dmp.DiffMain(s1, s2, true) 1531 | } 1532 | } 1533 | 1534 | func BenchmarkDiffMainLarge(b *testing.B) { 1535 | s1, s2 := speedtestTexts() 1536 | 1537 | dmp := New() 1538 | 1539 | b.ResetTimer() 1540 | 1541 | for i := 0; i < b.N; i++ { 1542 | dmp.DiffMain(s1, s2, true) 1543 | } 1544 | } 1545 | 1546 | func BenchmarkDiffMainRunesLargeLines(b *testing.B) { 1547 | s1, s2 := speedtestTexts() 1548 | 1549 | dmp := New() 1550 | 1551 | b.ResetTimer() 1552 | 1553 | for i := 0; i < b.N; i++ { 1554 | text1, text2, linearray := dmp.DiffLinesToRunes(s1, s2) 1555 | 1556 | diffs := dmp.DiffMainRunes(text1, text2, false) 1557 | diffs = dmp.DiffCharsToLines(diffs, linearray) 1558 | } 1559 | } 1560 | 1561 | func BenchmarkDiffMainRunesLargeDiffLines(b *testing.B) { 1562 | fp, _ := os.Open("../testdata/diff10klinestest.txt") 1563 | defer fp.Close() 1564 | data, _ := ioutil.ReadAll(fp) 1565 | 1566 | dmp := New() 1567 | 1568 | b.ResetTimer() 1569 | 1570 | for i := 0; i < b.N; i++ { 1571 | text1, text2, linearray := dmp.DiffLinesToRunes(string(data), "") 1572 | 1573 | diffs := dmp.DiffMainRunes(text1, text2, false) 1574 | diffs = dmp.DiffCharsToLines(diffs, linearray) 1575 | } 1576 | } 1577 | -------------------------------------------------------------------------------- /diffmatchpatch/diffmatchpatch.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | // Package diffmatchpatch offers robust algorithms to perform the operations required for synchronizing plain text. 10 | package diffmatchpatch 11 | 12 | import ( 13 | "time" 14 | ) 15 | 16 | // DiffMatchPatch holds the configuration for diff-match-patch operations. 17 | type DiffMatchPatch struct { 18 | // Number of seconds to map a diff before giving up (0 for infinity). 19 | DiffTimeout time.Duration 20 | // Cost of an empty edit operation in terms of edit characters. 21 | DiffEditCost int 22 | // How far to search for a match (0 = exact location, 1000+ = broad match). A match this many characters away from the expected location will add 1.0 to the score (0.0 is a perfect match). 23 | MatchDistance int 24 | // When deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents. (0.0 = perfection, 1.0 = very loose). Note that MatchThreshold controls how closely the end points of a delete need to match. 25 | PatchDeleteThreshold float64 26 | // Chunk size for context length. 27 | PatchMargin int 28 | // The number of bits in an int. 29 | MatchMaxBits int 30 | // At what point is no match declared (0.0 = perfection, 1.0 = very loose). 31 | MatchThreshold float64 32 | } 33 | 34 | // New creates a new DiffMatchPatch object with default parameters. 35 | func New() *DiffMatchPatch { 36 | // Defaults. 37 | return &DiffMatchPatch{ 38 | DiffTimeout: time.Second, 39 | DiffEditCost: 4, 40 | MatchThreshold: 0.5, 41 | MatchDistance: 1000, 42 | PatchDeleteThreshold: 0.5, 43 | PatchMargin: 4, 44 | MatchMaxBits: 32, 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /diffmatchpatch/index.go: -------------------------------------------------------------------------------- 1 | package diffmatchpatch 2 | 3 | type index uint32 4 | 5 | const runeSkipStart = 0xd800 6 | const runeSkipEnd = 0xdfff + 1 7 | const runeMax = 0x110000 // next invalid code point 8 | 9 | func stringToIndex(text string) []index { 10 | runes := []rune(text) 11 | indexes := make([]index, len(runes)) 12 | for i, r := range runes { 13 | if r < runeSkipEnd { 14 | indexes[i] = index(r) 15 | } else { 16 | indexes[i] = index(r) - (runeSkipEnd - runeSkipStart) 17 | } 18 | } 19 | return indexes 20 | } 21 | 22 | func indexesToString(indexes []index) string { 23 | runes := make([]rune, len(indexes)) 24 | for i, index := range indexes { 25 | if index < runeSkipStart { 26 | runes[i] = rune(index) 27 | } else { 28 | runes[i] = rune(index + (runeSkipEnd - runeSkipStart)) 29 | } 30 | } 31 | return string(runes) 32 | } 33 | -------------------------------------------------------------------------------- /diffmatchpatch/index_test.go: -------------------------------------------------------------------------------- 1 | package diffmatchpatch 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "testing" 6 | ) 7 | 8 | func TestIndexConversion(t *testing.T) { 9 | n := runeMax - (runeSkipEnd - runeSkipStart) 10 | indexes := make([]index, n) 11 | for i := 0; i < n; i++ { 12 | indexes[i] = index(i) 13 | } 14 | indexes2 := stringToIndex(indexesToString(indexes)) 15 | assert.EqualValues(t, indexes, indexes2) 16 | } 17 | -------------------------------------------------------------------------------- /diffmatchpatch/match.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "math" 13 | ) 14 | 15 | // MatchMain locates the best instance of 'pattern' in 'text' near 'loc'. 16 | // Returns -1 if no match found. 17 | func (dmp *DiffMatchPatch) MatchMain(text, pattern string, loc int) int { 18 | // Check for null inputs not needed since null can't be passed in C#. 19 | 20 | loc = int(math.Max(0, math.Min(float64(loc), float64(len(text))))) 21 | if text == pattern { 22 | // Shortcut (potentially not guaranteed by the algorithm) 23 | return 0 24 | } else if len(text) == 0 { 25 | // Nothing to match. 26 | return -1 27 | } else if loc+len(pattern) <= len(text) && text[loc:loc+len(pattern)] == pattern { 28 | // Perfect match at the perfect spot! (Includes case of null pattern) 29 | return loc 30 | } 31 | // Do a fuzzy compare. 32 | return dmp.MatchBitap(text, pattern, loc) 33 | } 34 | 35 | // MatchBitap locates the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm. 36 | // Returns -1 if no match was found. 37 | func (dmp *DiffMatchPatch) MatchBitap(text, pattern string, loc int) int { 38 | // Initialise the alphabet. 39 | s := dmp.MatchAlphabet(pattern) 40 | 41 | // Highest score beyond which we give up. 42 | scoreThreshold := dmp.MatchThreshold 43 | // Is there a nearby exact match? (speedup) 44 | bestLoc := indexOf(text, pattern, loc) 45 | if bestLoc != -1 { 46 | scoreThreshold = math.Min(dmp.matchBitapScore(0, bestLoc, loc, 47 | pattern), scoreThreshold) 48 | // What about in the other direction? (speedup) 49 | bestLoc = lastIndexOf(text, pattern, loc+len(pattern)) 50 | if bestLoc != -1 { 51 | scoreThreshold = math.Min(dmp.matchBitapScore(0, bestLoc, loc, 52 | pattern), scoreThreshold) 53 | } 54 | } 55 | 56 | // Initialise the bit arrays. 57 | matchmask := 1 << uint((len(pattern) - 1)) 58 | bestLoc = -1 59 | 60 | var binMin, binMid int 61 | binMax := len(pattern) + len(text) 62 | lastRd := []int{} 63 | for d := 0; d < len(pattern); d++ { 64 | // Scan for the best match; each iteration allows for one more error. Run a binary search to determine how far from 'loc' we can stray at this error level. 65 | binMin = 0 66 | binMid = binMax 67 | for binMin < binMid { 68 | if dmp.matchBitapScore(d, loc+binMid, loc, pattern) <= scoreThreshold { 69 | binMin = binMid 70 | } else { 71 | binMax = binMid 72 | } 73 | binMid = (binMax-binMin)/2 + binMin 74 | } 75 | // Use the result from this iteration as the maximum for the next. 76 | binMax = binMid 77 | start := int(math.Max(1, float64(loc-binMid+1))) 78 | finish := int(math.Min(float64(loc+binMid), float64(len(text))) + float64(len(pattern))) 79 | 80 | rd := make([]int, finish+2) 81 | rd[finish+1] = (1 << uint(d)) - 1 82 | 83 | for j := finish; j >= start; j-- { 84 | var charMatch int 85 | if len(text) <= j-1 { 86 | // Out of range. 87 | charMatch = 0 88 | } else if _, ok := s[text[j-1]]; !ok { 89 | charMatch = 0 90 | } else { 91 | charMatch = s[text[j-1]] 92 | } 93 | 94 | if d == 0 { 95 | // First pass: exact match. 96 | rd[j] = ((rd[j+1] << 1) | 1) & charMatch 97 | } else { 98 | // Subsequent passes: fuzzy match. 99 | rd[j] = ((rd[j+1]<<1)|1)&charMatch | (((lastRd[j+1] | lastRd[j]) << 1) | 1) | lastRd[j+1] 100 | } 101 | if (rd[j] & matchmask) != 0 { 102 | score := dmp.matchBitapScore(d, j-1, loc, pattern) 103 | // This match will almost certainly be better than any existing match. But check anyway. 104 | if score <= scoreThreshold { 105 | // Told you so. 106 | scoreThreshold = score 107 | bestLoc = j - 1 108 | if bestLoc > loc { 109 | // When passing loc, don't exceed our current distance from loc. 110 | start = int(math.Max(1, float64(2*loc-bestLoc))) 111 | } else { 112 | // Already passed loc, downhill from here on in. 113 | break 114 | } 115 | } 116 | } 117 | } 118 | if dmp.matchBitapScore(d+1, loc, loc, pattern) > scoreThreshold { 119 | // No hope for a (better) match at greater error levels. 120 | break 121 | } 122 | lastRd = rd 123 | } 124 | return bestLoc 125 | } 126 | 127 | // matchBitapScore computes and returns the score for a match with e errors and x location. 128 | func (dmp *DiffMatchPatch) matchBitapScore(e, x, loc int, pattern string) float64 { 129 | accuracy := float64(e) / float64(len(pattern)) 130 | proximity := math.Abs(float64(loc - x)) 131 | if dmp.MatchDistance == 0 { 132 | // Dodge divide by zero error. 133 | if proximity == 0 { 134 | return accuracy 135 | } 136 | 137 | return 1.0 138 | } 139 | return accuracy + (proximity / float64(dmp.MatchDistance)) 140 | } 141 | 142 | // MatchAlphabet initialises the alphabet for the Bitap algorithm. 143 | func (dmp *DiffMatchPatch) MatchAlphabet(pattern string) map[byte]int { 144 | s := map[byte]int{} 145 | charPattern := []byte(pattern) 146 | for _, c := range charPattern { 147 | _, ok := s[c] 148 | if !ok { 149 | s[c] = 0 150 | } 151 | } 152 | i := 0 153 | 154 | for _, c := range charPattern { 155 | value := s[c] | int(uint(1)< y { 20 | return x 21 | } 22 | return y 23 | } 24 | -------------------------------------------------------------------------------- /diffmatchpatch/operation_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -type=Operation -trimprefix=Diff"; DO NOT EDIT. 2 | 3 | package diffmatchpatch 4 | 5 | import "fmt" 6 | 7 | const _Operation_name = "DeleteEqualInsert" 8 | 9 | var _Operation_index = [...]uint8{0, 6, 11, 17} 10 | 11 | func (i Operation) String() string { 12 | i -= -1 13 | if i < 0 || i >= Operation(len(_Operation_index)-1) { 14 | return fmt.Sprintf("Operation(%d)", i+-1) 15 | } 16 | return _Operation_name[_Operation_index[i]:_Operation_index[i+1]] 17 | } 18 | -------------------------------------------------------------------------------- /diffmatchpatch/patch.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "bytes" 13 | "errors" 14 | "math" 15 | "net/url" 16 | "regexp" 17 | "strconv" 18 | "strings" 19 | ) 20 | 21 | // Patch represents one patch operation. 22 | type Patch struct { 23 | diffs []Diff 24 | Start1 int 25 | Start2 int 26 | Length1 int 27 | Length2 int 28 | } 29 | 30 | // String emulates GNU diff's format. 31 | // Header: @@ -382,8 +481,9 @@ 32 | // Indices are printed as 1-based, not 0-based. 33 | func (p *Patch) String() string { 34 | var coords1, coords2 string 35 | 36 | if p.Length1 == 0 { 37 | coords1 = strconv.Itoa(p.Start1) + ",0" 38 | } else if p.Length1 == 1 { 39 | coords1 = strconv.Itoa(p.Start1 + 1) 40 | } else { 41 | coords1 = strconv.Itoa(p.Start1+1) + "," + strconv.Itoa(p.Length1) 42 | } 43 | 44 | if p.Length2 == 0 { 45 | coords2 = strconv.Itoa(p.Start2) + ",0" 46 | } else if p.Length2 == 1 { 47 | coords2 = strconv.Itoa(p.Start2 + 1) 48 | } else { 49 | coords2 = strconv.Itoa(p.Start2+1) + "," + strconv.Itoa(p.Length2) 50 | } 51 | 52 | var text bytes.Buffer 53 | _, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n") 54 | 55 | // Escape the body of the patch with %xx notation. 56 | for _, aDiff := range p.diffs { 57 | switch aDiff.Type { 58 | case DiffInsert: 59 | _, _ = text.WriteString("+") 60 | case DiffDelete: 61 | _, _ = text.WriteString("-") 62 | case DiffEqual: 63 | _, _ = text.WriteString(" ") 64 | } 65 | 66 | _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1)) 67 | _, _ = text.WriteString("\n") 68 | } 69 | 70 | return unescaper.Replace(text.String()) 71 | } 72 | 73 | // PatchAddContext increases the context until it is unique, but doesn't let the pattern expand beyond MatchMaxBits. 74 | func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch { 75 | if len(text) == 0 { 76 | return patch 77 | } 78 | 79 | pattern := text[patch.Start2 : patch.Start2+patch.Length1] 80 | padding := 0 81 | 82 | // Look for the first and last matches of pattern in text. If two different matches are found, increase the pattern length. 83 | for strings.Index(text, pattern) != strings.LastIndex(text, pattern) && 84 | len(pattern) < dmp.MatchMaxBits-2*dmp.PatchMargin { 85 | padding += dmp.PatchMargin 86 | maxStart := max(0, patch.Start2-padding) 87 | minEnd := min(len(text), patch.Start2+patch.Length1+padding) 88 | pattern = text[maxStart:minEnd] 89 | } 90 | // Add one chunk for good luck. 91 | padding += dmp.PatchMargin 92 | 93 | // Add the prefix. 94 | prefix := text[max(0, patch.Start2-padding):patch.Start2] 95 | if len(prefix) != 0 { 96 | patch.diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.diffs...) 97 | } 98 | // Add the suffix. 99 | suffix := text[patch.Start2+patch.Length1 : min(len(text), patch.Start2+patch.Length1+padding)] 100 | if len(suffix) != 0 { 101 | patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix}) 102 | } 103 | 104 | // Roll back the start points. 105 | patch.Start1 -= len(prefix) 106 | patch.Start2 -= len(prefix) 107 | // Extend the lengths. 108 | patch.Length1 += len(prefix) + len(suffix) 109 | patch.Length2 += len(prefix) + len(suffix) 110 | 111 | return patch 112 | } 113 | 114 | // PatchMake computes a list of patches. 115 | func (dmp *DiffMatchPatch) PatchMake(opt ...interface{}) []Patch { 116 | if len(opt) == 1 { 117 | diffs, _ := opt[0].([]Diff) 118 | text1 := dmp.DiffText1(diffs) 119 | return dmp.PatchMake(text1, diffs) 120 | } else if len(opt) == 2 { 121 | text1 := opt[0].(string) 122 | switch t := opt[1].(type) { 123 | case string: 124 | diffs := dmp.DiffMain(text1, t, true) 125 | if len(diffs) > 2 { 126 | diffs = dmp.DiffCleanupSemantic(diffs) 127 | diffs = dmp.DiffCleanupEfficiency(diffs) 128 | } 129 | return dmp.PatchMake(text1, diffs) 130 | case []Diff: 131 | return dmp.patchMake2(text1, t) 132 | } 133 | } else if len(opt) == 3 { 134 | return dmp.PatchMake(opt[0], opt[2]) 135 | } 136 | return []Patch{} 137 | } 138 | 139 | // patchMake2 computes a list of patches to turn text1 into text2. 140 | // text2 is not provided, diffs are the delta between text1 and text2. 141 | func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch { 142 | // Check for null inputs not needed since null can't be passed in C#. 143 | patches := []Patch{} 144 | if len(diffs) == 0 { 145 | return patches // Get rid of the null case. 146 | } 147 | 148 | patch := Patch{} 149 | charCount1 := 0 // Number of characters into the text1 string. 150 | charCount2 := 0 // Number of characters into the text2 string. 151 | // Start with text1 (prepatchText) and apply the diffs until we arrive at text2 (postpatchText). We recreate the patches one by one to determine context info. 152 | prepatchText := text1 153 | postpatchText := text1 154 | 155 | for i, aDiff := range diffs { 156 | if len(patch.diffs) == 0 && aDiff.Type != DiffEqual { 157 | // A new patch starts here. 158 | patch.Start1 = charCount1 159 | patch.Start2 = charCount2 160 | } 161 | 162 | switch aDiff.Type { 163 | case DiffInsert: 164 | patch.diffs = append(patch.diffs, aDiff) 165 | patch.Length2 += len(aDiff.Text) 166 | postpatchText = postpatchText[:charCount2] + 167 | aDiff.Text + postpatchText[charCount2:] 168 | case DiffDelete: 169 | patch.Length1 += len(aDiff.Text) 170 | patch.diffs = append(patch.diffs, aDiff) 171 | postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):] 172 | case DiffEqual: 173 | if len(aDiff.Text) <= 2*dmp.PatchMargin && 174 | len(patch.diffs) != 0 && i != len(diffs)-1 { 175 | // Small equality inside a patch. 176 | patch.diffs = append(patch.diffs, aDiff) 177 | patch.Length1 += len(aDiff.Text) 178 | patch.Length2 += len(aDiff.Text) 179 | } 180 | if len(aDiff.Text) >= 2*dmp.PatchMargin { 181 | // Time for a new patch. 182 | if len(patch.diffs) != 0 { 183 | patch = dmp.PatchAddContext(patch, prepatchText) 184 | patches = append(patches, patch) 185 | patch = Patch{} 186 | // Unlike Unidiff, our patch lists have a rolling context. http://code.google.com/p/google-diff-match-patch/wiki/Unidiff Update prepatch text & pos to reflect the application of the just completed patch. 187 | prepatchText = postpatchText 188 | charCount1 = charCount2 189 | } 190 | } 191 | } 192 | 193 | // Update the current character count. 194 | if aDiff.Type != DiffInsert { 195 | charCount1 += len(aDiff.Text) 196 | } 197 | if aDiff.Type != DiffDelete { 198 | charCount2 += len(aDiff.Text) 199 | } 200 | } 201 | 202 | // Pick up the leftover patch if not empty. 203 | if len(patch.diffs) != 0 { 204 | patch = dmp.PatchAddContext(patch, prepatchText) 205 | patches = append(patches, patch) 206 | } 207 | 208 | return patches 209 | } 210 | 211 | // PatchDeepCopy returns an array that is identical to a given an array of patches. 212 | func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch { 213 | patchesCopy := []Patch{} 214 | for _, aPatch := range patches { 215 | patchCopy := Patch{} 216 | for _, aDiff := range aPatch.diffs { 217 | patchCopy.diffs = append(patchCopy.diffs, Diff{ 218 | aDiff.Type, 219 | aDiff.Text, 220 | }) 221 | } 222 | patchCopy.Start1 = aPatch.Start1 223 | patchCopy.Start2 = aPatch.Start2 224 | patchCopy.Length1 = aPatch.Length1 225 | patchCopy.Length2 = aPatch.Length2 226 | patchesCopy = append(patchesCopy, patchCopy) 227 | } 228 | return patchesCopy 229 | } 230 | 231 | // PatchApply merges a set of patches onto the text. Returns a patched text, as well as an array of true/false values indicating which patches were applied. 232 | func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []bool) { 233 | if len(patches) == 0 { 234 | return text, []bool{} 235 | } 236 | 237 | // Deep copy the patches so that no changes are made to originals. 238 | patches = dmp.PatchDeepCopy(patches) 239 | 240 | nullPadding := dmp.PatchAddPadding(patches) 241 | text = nullPadding + text + nullPadding 242 | patches = dmp.PatchSplitMax(patches) 243 | 244 | x := 0 245 | // delta keeps track of the offset between the expected and actual location of the previous patch. If there are patches expected at positions 10 and 20, but the first patch was found at 12, delta is 2 and the second patch has an effective expected position of 22. 246 | delta := 0 247 | results := make([]bool, len(patches)) 248 | for _, aPatch := range patches { 249 | expectedLoc := aPatch.Start2 + delta 250 | text1 := dmp.DiffText1(aPatch.diffs) 251 | var startLoc int 252 | endLoc := -1 253 | if len(text1) > dmp.MatchMaxBits { 254 | // PatchSplitMax will only provide an oversized pattern in the case of a monster delete. 255 | startLoc = dmp.MatchMain(text, text1[:dmp.MatchMaxBits], expectedLoc) 256 | if startLoc != -1 { 257 | endLoc = dmp.MatchMain(text, 258 | text1[len(text1)-dmp.MatchMaxBits:], expectedLoc+len(text1)-dmp.MatchMaxBits) 259 | if endLoc == -1 || startLoc >= endLoc { 260 | // Can't find valid trailing context. Drop this patch. 261 | startLoc = -1 262 | } 263 | } 264 | } else { 265 | startLoc = dmp.MatchMain(text, text1, expectedLoc) 266 | } 267 | if startLoc == -1 { 268 | // No match found. :( 269 | results[x] = false 270 | // Subtract the delta for this failed patch from subsequent patches. 271 | delta -= aPatch.Length2 - aPatch.Length1 272 | } else { 273 | // Found a match. :) 274 | results[x] = true 275 | delta = startLoc - expectedLoc 276 | var text2 string 277 | if endLoc == -1 { 278 | text2 = text[startLoc:int(math.Min(float64(startLoc+len(text1)), float64(len(text))))] 279 | } else { 280 | text2 = text[startLoc:int(math.Min(float64(endLoc+dmp.MatchMaxBits), float64(len(text))))] 281 | } 282 | if text1 == text2 { 283 | // Perfect match, just shove the Replacement text in. 284 | text = text[:startLoc] + dmp.DiffText2(aPatch.diffs) + text[startLoc+len(text1):] 285 | } else { 286 | // Imperfect match. Run a diff to get a framework of equivalent indices. 287 | diffs := dmp.DiffMain(text1, text2, false) 288 | if len(text1) > dmp.MatchMaxBits && float64(dmp.DiffLevenshtein(diffs))/float64(len(text1)) > dmp.PatchDeleteThreshold { 289 | // The end points match, but the content is unacceptably bad. 290 | results[x] = false 291 | } else { 292 | diffs = dmp.DiffCleanupSemanticLossless(diffs) 293 | index1 := 0 294 | for _, aDiff := range aPatch.diffs { 295 | if aDiff.Type != DiffEqual { 296 | index2 := dmp.DiffXIndex(diffs, index1) 297 | if aDiff.Type == DiffInsert { 298 | // Insertion 299 | text = text[:startLoc+index2] + aDiff.Text + text[startLoc+index2:] 300 | } else if aDiff.Type == DiffDelete { 301 | // Deletion 302 | startIndex := startLoc + index2 303 | text = text[:startIndex] + 304 | text[startIndex+dmp.DiffXIndex(diffs, index1+len(aDiff.Text))-index2:] 305 | } 306 | } 307 | if aDiff.Type != DiffDelete { 308 | index1 += len(aDiff.Text) 309 | } 310 | } 311 | } 312 | } 313 | } 314 | x++ 315 | } 316 | // Strip the padding off. 317 | text = text[len(nullPadding) : len(nullPadding)+(len(text)-2*len(nullPadding))] 318 | return text, results 319 | } 320 | 321 | // PatchAddPadding adds some padding on text start and end so that edges can match something. 322 | // Intended to be called only from within patchApply. 323 | func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string { 324 | paddingLength := dmp.PatchMargin 325 | nullPadding := "" 326 | for x := 1; x <= paddingLength; x++ { 327 | nullPadding += string(rune(x)) 328 | } 329 | 330 | // Bump all the patches forward. 331 | for i := range patches { 332 | patches[i].Start1 += paddingLength 333 | patches[i].Start2 += paddingLength 334 | } 335 | 336 | // Add some padding on start of first diff. 337 | if len(patches[0].diffs) == 0 || patches[0].diffs[0].Type != DiffEqual { 338 | // Add nullPadding equality. 339 | patches[0].diffs = append([]Diff{Diff{DiffEqual, nullPadding}}, patches[0].diffs...) 340 | patches[0].Start1 -= paddingLength // Should be 0. 341 | patches[0].Start2 -= paddingLength // Should be 0. 342 | patches[0].Length1 += paddingLength 343 | patches[0].Length2 += paddingLength 344 | } else if paddingLength > len(patches[0].diffs[0].Text) { 345 | // Grow first equality. 346 | extraLength := paddingLength - len(patches[0].diffs[0].Text) 347 | patches[0].diffs[0].Text = nullPadding[len(patches[0].diffs[0].Text):] + patches[0].diffs[0].Text 348 | patches[0].Start1 -= extraLength 349 | patches[0].Start2 -= extraLength 350 | patches[0].Length1 += extraLength 351 | patches[0].Length2 += extraLength 352 | } 353 | 354 | // Add some padding on end of last diff. 355 | last := len(patches) - 1 356 | if len(patches[last].diffs) == 0 || patches[last].diffs[len(patches[last].diffs)-1].Type != DiffEqual { 357 | // Add nullPadding equality. 358 | patches[last].diffs = append(patches[last].diffs, Diff{DiffEqual, nullPadding}) 359 | patches[last].Length1 += paddingLength 360 | patches[last].Length2 += paddingLength 361 | } else if paddingLength > len(patches[last].diffs[len(patches[last].diffs)-1].Text) { 362 | // Grow last equality. 363 | lastDiff := patches[last].diffs[len(patches[last].diffs)-1] 364 | extraLength := paddingLength - len(lastDiff.Text) 365 | patches[last].diffs[len(patches[last].diffs)-1].Text += nullPadding[:extraLength] 366 | patches[last].Length1 += extraLength 367 | patches[last].Length2 += extraLength 368 | } 369 | 370 | return nullPadding 371 | } 372 | 373 | // PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm. 374 | // Intended to be called only from within patchApply. 375 | func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch { 376 | patchSize := dmp.MatchMaxBits 377 | for x := 0; x < len(patches); x++ { 378 | if patches[x].Length1 <= patchSize { 379 | continue 380 | } 381 | bigpatch := patches[x] 382 | // Remove the big old patch. 383 | patches = append(patches[:x], patches[x+1:]...) 384 | x-- 385 | 386 | Start1 := bigpatch.Start1 387 | Start2 := bigpatch.Start2 388 | precontext := "" 389 | for len(bigpatch.diffs) != 0 { 390 | // Create one of several smaller patches. 391 | patch := Patch{} 392 | empty := true 393 | patch.Start1 = Start1 - len(precontext) 394 | patch.Start2 = Start2 - len(precontext) 395 | if len(precontext) != 0 { 396 | patch.Length1 = len(precontext) 397 | patch.Length2 = len(precontext) 398 | patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext}) 399 | } 400 | for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin { 401 | diffType := bigpatch.diffs[0].Type 402 | diffText := bigpatch.diffs[0].Text 403 | if diffType == DiffInsert { 404 | // Insertions are harmless. 405 | patch.Length2 += len(diffText) 406 | Start2 += len(diffText) 407 | patch.diffs = append(patch.diffs, bigpatch.diffs[0]) 408 | bigpatch.diffs = bigpatch.diffs[1:] 409 | empty = false 410 | } else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize { 411 | // This is a large deletion. Let it pass in one chunk. 412 | patch.Length1 += len(diffText) 413 | Start1 += len(diffText) 414 | empty = false 415 | patch.diffs = append(patch.diffs, Diff{diffType, diffText}) 416 | bigpatch.diffs = bigpatch.diffs[1:] 417 | } else { 418 | // Deletion or equality. Only take as much as we can stomach. 419 | diffText = diffText[:min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)] 420 | 421 | patch.Length1 += len(diffText) 422 | Start1 += len(diffText) 423 | if diffType == DiffEqual { 424 | patch.Length2 += len(diffText) 425 | Start2 += len(diffText) 426 | } else { 427 | empty = false 428 | } 429 | patch.diffs = append(patch.diffs, Diff{diffType, diffText}) 430 | if diffText == bigpatch.diffs[0].Text { 431 | bigpatch.diffs = bigpatch.diffs[1:] 432 | } else { 433 | bigpatch.diffs[0].Text = 434 | bigpatch.diffs[0].Text[len(diffText):] 435 | } 436 | } 437 | } 438 | // Compute the head context for the next patch. 439 | precontext = dmp.DiffText2(patch.diffs) 440 | precontext = precontext[max(0, len(precontext)-dmp.PatchMargin):] 441 | 442 | postcontext := "" 443 | // Append the end context for this patch. 444 | if len(dmp.DiffText1(bigpatch.diffs)) > dmp.PatchMargin { 445 | postcontext = dmp.DiffText1(bigpatch.diffs)[:dmp.PatchMargin] 446 | } else { 447 | postcontext = dmp.DiffText1(bigpatch.diffs) 448 | } 449 | 450 | if len(postcontext) != 0 { 451 | patch.Length1 += len(postcontext) 452 | patch.Length2 += len(postcontext) 453 | if len(patch.diffs) != 0 && patch.diffs[len(patch.diffs)-1].Type == DiffEqual { 454 | patch.diffs[len(patch.diffs)-1].Text += postcontext 455 | } else { 456 | patch.diffs = append(patch.diffs, Diff{DiffEqual, postcontext}) 457 | } 458 | } 459 | if !empty { 460 | x++ 461 | patches = append(patches[:x], append([]Patch{patch}, patches[x:]...)...) 462 | } 463 | } 464 | } 465 | return patches 466 | } 467 | 468 | // PatchToText takes a list of patches and returns a textual representation. 469 | func (dmp *DiffMatchPatch) PatchToText(patches []Patch) string { 470 | var text bytes.Buffer 471 | for _, aPatch := range patches { 472 | _, _ = text.WriteString(aPatch.String()) 473 | } 474 | return text.String() 475 | } 476 | 477 | // PatchFromText parses a textual representation of patches and returns a List of Patch objects. 478 | func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) { 479 | patches := []Patch{} 480 | if len(textline) == 0 { 481 | return patches, nil 482 | } 483 | text := strings.Split(textline, "\n") 484 | textPointer := 0 485 | patchHeader := regexp.MustCompile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$") 486 | 487 | var patch Patch 488 | var sign uint8 489 | var line string 490 | for textPointer < len(text) { 491 | 492 | if !patchHeader.MatchString(text[textPointer]) { 493 | return patches, errors.New("Invalid patch string: " + text[textPointer]) 494 | } 495 | 496 | patch = Patch{} 497 | m := patchHeader.FindStringSubmatch(text[textPointer]) 498 | 499 | patch.Start1, _ = strconv.Atoi(m[1]) 500 | if len(m[2]) == 0 { 501 | patch.Start1-- 502 | patch.Length1 = 1 503 | } else if m[2] == "0" { 504 | patch.Length1 = 0 505 | } else { 506 | patch.Start1-- 507 | patch.Length1, _ = strconv.Atoi(m[2]) 508 | } 509 | 510 | patch.Start2, _ = strconv.Atoi(m[3]) 511 | 512 | if len(m[4]) == 0 { 513 | patch.Start2-- 514 | patch.Length2 = 1 515 | } else if m[4] == "0" { 516 | patch.Length2 = 0 517 | } else { 518 | patch.Start2-- 519 | patch.Length2, _ = strconv.Atoi(m[4]) 520 | } 521 | textPointer++ 522 | 523 | for textPointer < len(text) { 524 | if len(text[textPointer]) > 0 { 525 | sign = text[textPointer][0] 526 | } else { 527 | textPointer++ 528 | continue 529 | } 530 | 531 | line = text[textPointer][1:] 532 | line = strings.Replace(line, "+", "%2b", -1) 533 | line, _ = url.QueryUnescape(line) 534 | if sign == '-' { 535 | // Deletion. 536 | patch.diffs = append(patch.diffs, Diff{DiffDelete, line}) 537 | } else if sign == '+' { 538 | // Insertion. 539 | patch.diffs = append(patch.diffs, Diff{DiffInsert, line}) 540 | } else if sign == ' ' { 541 | // Minor equality. 542 | patch.diffs = append(patch.diffs, Diff{DiffEqual, line}) 543 | } else if sign == '@' { 544 | // Start of next patch. 545 | break 546 | } else { 547 | // WTF? 548 | return patches, errors.New("Invalid patch mode '" + string(sign) + "' in: " + string(line)) 549 | } 550 | textPointer++ 551 | } 552 | 553 | patches = append(patches, patch) 554 | } 555 | return patches, nil 556 | } 557 | -------------------------------------------------------------------------------- /diffmatchpatch/patch_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "fmt" 13 | "strings" 14 | "testing" 15 | 16 | "github.com/stretchr/testify/assert" 17 | ) 18 | 19 | func TestPatchString(t *testing.T) { 20 | type TestCase struct { 21 | Patch Patch 22 | 23 | Expected string 24 | } 25 | 26 | for i, tc := range []TestCase{ 27 | { 28 | Patch: Patch{ 29 | Start1: 20, 30 | Start2: 21, 31 | Length1: 18, 32 | Length2: 17, 33 | 34 | diffs: []Diff{ 35 | {DiffEqual, "jump"}, 36 | {DiffDelete, "s"}, 37 | {DiffInsert, "ed"}, 38 | {DiffEqual, " over "}, 39 | {DiffDelete, "the"}, 40 | {DiffInsert, "a"}, 41 | {DiffEqual, "\nlaz"}, 42 | }, 43 | }, 44 | 45 | Expected: "@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n %0Alaz\n", 46 | }, 47 | } { 48 | actual := tc.Patch.String() 49 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 50 | } 51 | } 52 | 53 | func TestPatchFromText(t *testing.T) { 54 | type TestCase struct { 55 | Patch string 56 | 57 | ErrorMessagePrefix string 58 | } 59 | 60 | dmp := New() 61 | 62 | for i, tc := range []TestCase{ 63 | {"", ""}, 64 | {"@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n %0Alaz\n", ""}, 65 | {"@@ -1 +1 @@\n-a\n+b\n", ""}, 66 | {"@@ -1,3 +0,0 @@\n-abc\n", ""}, 67 | {"@@ -0,0 +1,3 @@\n+abc\n", ""}, 68 | {"@@ _0,0 +0,0 @@\n+abc\n", "Invalid patch string: @@ _0,0 +0,0 @@"}, 69 | {"Bad\nPatch\n", "Invalid patch string"}, 70 | } { 71 | patches, err := dmp.PatchFromText(tc.Patch) 72 | if tc.ErrorMessagePrefix == "" { 73 | assert.Nil(t, err) 74 | 75 | if tc.Patch == "" { 76 | assert.Equal(t, []Patch{}, patches, fmt.Sprintf("Test case #%d, %#v", i, tc)) 77 | } else { 78 | assert.Equal(t, tc.Patch, patches[0].String(), fmt.Sprintf("Test case #%d, %#v", i, tc)) 79 | } 80 | } else { 81 | e := err.Error() 82 | if strings.HasPrefix(e, tc.ErrorMessagePrefix) { 83 | e = tc.ErrorMessagePrefix 84 | } 85 | assert.Equal(t, tc.ErrorMessagePrefix, e) 86 | } 87 | } 88 | 89 | diffs := []Diff{ 90 | {DiffDelete, "`1234567890-=[]\\;',./"}, 91 | {DiffInsert, "~!@#$%^&*()_+{}|:\"<>?"}, 92 | } 93 | 94 | patches, err := dmp.PatchFromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n") 95 | assert.Len(t, patches, 1) 96 | assert.Equal(t, diffs, 97 | patches[0].diffs, 98 | ) 99 | assert.Nil(t, err) 100 | } 101 | 102 | func TestPatchToText(t *testing.T) { 103 | type TestCase struct { 104 | Patch string 105 | } 106 | 107 | dmp := New() 108 | 109 | for i, tc := range []TestCase{ 110 | {"@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"}, 111 | {"@@ -1,9 +1,9 @@\n-f\n+F\n oo+fooba\n@@ -7,9 +7,9 @@\n obar\n-,\n+.\n tes\n"}, 112 | } { 113 | patches, err := dmp.PatchFromText(tc.Patch) 114 | assert.Nil(t, err) 115 | 116 | actual := dmp.PatchToText(patches) 117 | assert.Equal(t, tc.Patch, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 118 | } 119 | } 120 | 121 | func TestPatchAddContext(t *testing.T) { 122 | type TestCase struct { 123 | Name string 124 | 125 | Patch string 126 | Text string 127 | 128 | Expected string 129 | } 130 | 131 | dmp := New() 132 | dmp.PatchMargin = 4 133 | 134 | for i, tc := range []TestCase{ 135 | {"Simple case", "@@ -21,4 +21,10 @@\n-jump\n+somersault\n", "The quick brown fox jumps over the lazy dog.", "@@ -17,12 +17,18 @@\n fox \n-jump\n+somersault\n s ov\n"}, 136 | {"Not enough trailing context", "@@ -21,4 +21,10 @@\n-jump\n+somersault\n", "The quick brown fox jumps.", "@@ -17,10 +17,16 @@\n fox \n-jump\n+somersault\n s.\n"}, 137 | {"Not enough leading context", "@@ -3 +3,2 @@\n-e\n+at\n", "The quick brown fox jumps.", "@@ -1,7 +1,8 @@\n Th\n-e\n+at\n qui\n"}, 138 | {"Ambiguity", "@@ -3 +3,2 @@\n-e\n+at\n", "The quick brown fox jumps. The quick brown fox crashes.", "@@ -1,27 +1,28 @@\n Th\n-e\n+at\n quick brown fox jumps. \n"}, 139 | } { 140 | patches, err := dmp.PatchFromText(tc.Patch) 141 | assert.Nil(t, err) 142 | 143 | actual := dmp.PatchAddContext(patches[0], tc.Text) 144 | assert.Equal(t, tc.Expected, actual.String(), fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 145 | } 146 | } 147 | 148 | func TestPatchMakeAndPatchToText(t *testing.T) { 149 | type TestCase struct { 150 | Name string 151 | 152 | Input1 interface{} 153 | Input2 interface{} 154 | Input3 interface{} 155 | 156 | Expected string 157 | } 158 | 159 | dmp := New() 160 | 161 | text1 := "The quick brown fox jumps over the lazy dog." 162 | text2 := "That quick brown fox jumped over a lazy dog." 163 | 164 | for i, tc := range []TestCase{ 165 | {"Null case", "", "", nil, ""}, 166 | {"Text2+Text1 inputs", text2, text1, nil, "@@ -1,8 +1,7 @@\n Th\n-at\n+e\n qui\n@@ -21,17 +21,18 @@\n jump\n-ed\n+s\n over \n-a\n+the\n laz\n"}, 167 | {"Text1+Text2 inputs", text1, text2, nil, "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"}, 168 | {"Diff input", dmp.DiffMain(text1, text2, false), nil, nil, "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"}, 169 | {"Text1+Diff inputs", text1, dmp.DiffMain(text1, text2, false), nil, "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"}, 170 | {"Text1+Text2+Diff inputs (deprecated)", text1, text2, dmp.DiffMain(text1, text2, false), "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"}, 171 | {"Character encoding", "`1234567890-=[]\\;',./", "~!@#$%^&*()_+{}|:\"<>?", nil, "@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n"}, 172 | {"Long string with repeats", strings.Repeat("abcdef", 100), strings.Repeat("abcdef", 100) + "123", nil, "@@ -573,28 +573,31 @@\n cdefabcdefabcdefabcdefabcdef\n+123\n"}, 173 | {"Corner case of #31 fixed by #32", "2016-09-01T03:07:14.807830741Z", "2016-09-01T03:07:15.154800781Z", nil, "@@ -15,16 +15,16 @@\n 07:1\n+5.15\n 4\n-.\n 80\n+0\n 78\n-3074\n 1Z\n"}, 174 | } { 175 | var patches []Patch 176 | if tc.Input3 != nil { 177 | patches = dmp.PatchMake(tc.Input1, tc.Input2, tc.Input3) 178 | } else if tc.Input2 != nil { 179 | patches = dmp.PatchMake(tc.Input1, tc.Input2) 180 | } else if ps, ok := tc.Input1.([]Patch); ok { 181 | patches = ps 182 | } else { 183 | patches = dmp.PatchMake(tc.Input1) 184 | } 185 | 186 | actual := dmp.PatchToText(patches) 187 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 188 | } 189 | 190 | // Corner case of #28 wrong patch with timeout of 0 191 | dmp.DiffTimeout = 0 192 | 193 | text1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum libero vel." 194 | text2 = "Lorem a ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum liberovel." 195 | 196 | diffs := dmp.DiffMain(text1, text2, true) 197 | // Additional check that the diff texts are equal to the originals even if we are using DiffMain with checklines=true #29 198 | assert.Equal(t, text1, dmp.DiffText1(diffs)) 199 | assert.Equal(t, text2, dmp.DiffText2(diffs)) 200 | 201 | patches := dmp.PatchMake(text1, diffs) 202 | 203 | actual := dmp.PatchToText(patches) 204 | assert.Equal(t, "@@ -1,14 +1,16 @@\n Lorem \n+a \n ipsum do\n@@ -148,13 +148,12 @@\n m libero\n- \n vel.\n", actual) 205 | 206 | // Check that empty Patch array is returned for no parameter call 207 | patches = dmp.PatchMake() 208 | assert.Equal(t, []Patch{}, patches) 209 | } 210 | 211 | func TestPatchSplitMax(t *testing.T) { 212 | type TestCase struct { 213 | Text1 string 214 | Text2 string 215 | 216 | Expected string 217 | } 218 | 219 | dmp := New() 220 | 221 | for i, tc := range []TestCase{ 222 | {"abcdefghijklmnopqrstuvwxyz01234567890", "XabXcdXefXghXijXklXmnXopXqrXstXuvXwxXyzX01X23X45X67X89X0", "@@ -1,32 +1,46 @@\n+X\n ab\n+X\n cd\n+X\n ef\n+X\n gh\n+X\n ij\n+X\n kl\n+X\n mn\n+X\n op\n+X\n qr\n+X\n st\n+X\n uv\n+X\n wx\n+X\n yz\n+X\n 012345\n@@ -25,13 +39,18 @@\n zX01\n+X\n 23\n+X\n 45\n+X\n 67\n+X\n 89\n+X\n 0\n"}, 223 | {"abcdef1234567890123456789012345678901234567890123456789012345678901234567890uvwxyz", "abcdefuvwxyz", "@@ -3,78 +3,8 @@\n cdef\n-1234567890123456789012345678901234567890123456789012345678901234567890\n uvwx\n"}, 224 | {"1234567890123456789012345678901234567890123456789012345678901234567890", "abc", "@@ -1,32 +1,4 @@\n-1234567890123456789012345678\n 9012\n@@ -29,32 +1,4 @@\n-9012345678901234567890123456\n 7890\n@@ -57,14 +1,3 @@\n-78901234567890\n+abc\n"}, 225 | {"abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1", "abcdefghij , h : 1 , t : 1 abcdefghij , h : 1 , t : 1 abcdefghij , h : 0 , t : 1", "@@ -2,32 +2,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n@@ -29,32 +29,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n"}, 226 | } { 227 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 228 | patches = dmp.PatchSplitMax(patches) 229 | 230 | actual := dmp.PatchToText(patches) 231 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 232 | } 233 | } 234 | 235 | func TestPatchAddPadding(t *testing.T) { 236 | type TestCase struct { 237 | Name string 238 | 239 | Text1 string 240 | Text2 string 241 | 242 | Expected string 243 | ExpectedWithPadding string 244 | } 245 | 246 | dmp := New() 247 | 248 | for i, tc := range []TestCase{ 249 | {"Both edges full", "", "test", "@@ -0,0 +1,4 @@\n+test\n", "@@ -1,8 +1,12 @@\n %01%02%03%04\n+test\n %01%02%03%04\n"}, 250 | {"Both edges partial", "XY", "XtestY", "@@ -1,2 +1,6 @@\n X\n+test\n Y\n", "@@ -2,8 +2,12 @@\n %02%03%04X\n+test\n Y%01%02%03\n"}, 251 | {"Both edges none", "XXXXYYYY", "XXXXtestYYYY", "@@ -1,8 +1,12 @@\n XXXX\n+test\n YYYY\n", "@@ -5,8 +5,12 @@\n XXXX\n+test\n YYYY\n"}, 252 | } { 253 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 254 | 255 | actual := dmp.PatchToText(patches) 256 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 257 | 258 | dmp.PatchAddPadding(patches) 259 | 260 | actualWithPadding := dmp.PatchToText(patches) 261 | assert.Equal(t, tc.ExpectedWithPadding, actualWithPadding, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 262 | } 263 | } 264 | 265 | func TestPatchApply(t *testing.T) { 266 | type TestCase struct { 267 | Name string 268 | 269 | Text1 string 270 | Text2 string 271 | TextBase string 272 | 273 | Expected string 274 | ExpectedApplies []bool 275 | } 276 | 277 | dmp := New() 278 | dmp.MatchDistance = 1000 279 | dmp.MatchThreshold = 0.5 280 | dmp.PatchDeleteThreshold = 0.5 281 | 282 | for i, tc := range []TestCase{ 283 | {"Null case", "", "", "Hello world.", "Hello world.", []bool{}}, 284 | {"Exact match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", []bool{true, true}}, 285 | {"Partial match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "The quick red rabbit jumps over the tired tiger.", "That quick red rabbit jumped over a tired tiger.", []bool{true, true}}, 286 | {"Failed match", "The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.", "I am the very model of a modern major general.", "I am the very model of a modern major general.", []bool{false, false}}, 287 | {"Big delete, small Diff", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y", "xabcy", []bool{true, true}}, 288 | {"Big delete, big Diff 1", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabc12345678901234567890---------------++++++++++---------------12345678901234567890y", []bool{false, true}}, 289 | } { 290 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 291 | 292 | actual, actualApplies := dmp.PatchApply(patches, tc.TextBase) 293 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 294 | assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 295 | } 296 | 297 | dmp.PatchDeleteThreshold = 0.6 298 | 299 | for i, tc := range []TestCase{ 300 | {"Big delete, big Diff 2", "x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy", "x12345678901234567890---------------++++++++++---------------12345678901234567890y", "xabcy", []bool{true, true}}, 301 | } { 302 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 303 | 304 | actual, actualApplies := dmp.PatchApply(patches, tc.TextBase) 305 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 306 | assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 307 | } 308 | 309 | dmp.MatchDistance = 0 310 | dmp.MatchThreshold = 0.0 311 | dmp.PatchDeleteThreshold = 0.5 312 | 313 | for i, tc := range []TestCase{ 314 | {"Compensate for failed patch", "abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890", []bool{false, true}}, 315 | } { 316 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 317 | 318 | actual, actualApplies := dmp.PatchApply(patches, tc.TextBase) 319 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 320 | assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 321 | } 322 | 323 | dmp.MatchThreshold = 0.5 324 | dmp.MatchDistance = 1000 325 | 326 | for i, tc := range []TestCase{ 327 | {"No side effects", "", "test", "", "test", []bool{true}}, 328 | {"No side effects with major delete", "The quick brown fox jumps over the lazy dog.", "Woof", "The quick brown fox jumps over the lazy dog.", "Woof", []bool{true, true}}, 329 | {"Edge exact match", "", "test", "", "test", []bool{true}}, 330 | {"Near edge exact match", "XY", "XtestY", "XY", "XtestY", []bool{true}}, 331 | {"Edge partial match", "y", "y123", "x", "x123", []bool{true}}, 332 | } { 333 | patches := dmp.PatchMake(tc.Text1, tc.Text2) 334 | 335 | actual, actualApplies := dmp.PatchApply(patches, tc.TextBase) 336 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 337 | assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name)) 338 | } 339 | } 340 | 341 | func TestPatchMakeOutOfRangePanic(t *testing.T) { 342 | text1 := ` 343 | 1111111111111 000000 344 | ------------- ------ 345 | xxxxxxxxxxxxx ------ 346 | xxxxxxxxxxxxx ------ 347 | xxxxxxxxxxxxx xxxxxx 348 | xxxxxxxxxxxxx ...... 349 | xxxxxxxxxxxxx 111111 350 | xxxxxxxxxxxxx ?????? 351 | xxxxxxxxxxxxx 333333 352 | xxxxxxxxxxxxx 555555 353 | xxxxxxxxxx xxxxx 354 | xxxxxxxxxx xxxxx 355 | xxxxxxxxxx xxxxx 356 | xxxxxxxxxx xxxxx 357 | ` 358 | text2 := ` 359 | 2222222222222 000000 360 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx` 361 | dmp := New() 362 | patches := dmp.PatchMake(text1, text2) 363 | assert.Equal(t, 6, len(patches), "TestPatchMakeOutOfRangePanic") 364 | } 365 | -------------------------------------------------------------------------------- /diffmatchpatch/stringutil.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "fmt" 13 | "strings" 14 | "unicode/utf8" 15 | ) 16 | 17 | const UNICODE_INVALID_RANGE_START = 0xD800 18 | const UNICODE_INVALID_RANGE_END = 0xDFFF 19 | const UNICODE_INVALID_RANGE_DELTA = UNICODE_INVALID_RANGE_END - UNICODE_INVALID_RANGE_START + 1 20 | const UNICODE_RANGE_MAX = 0x10FFFF 21 | 22 | // unescaper unescapes selected chars for compatibility with JavaScript's encodeURI. 23 | // In speed critical applications this could be dropped since the receiving application will certainly decode these fine. Note that this function is case-sensitive. Thus "%3F" would not be unescaped. But this is ok because it is only called with the output of HttpUtility.UrlEncode which returns lowercase hex. Example: "%3f" -> "?", "%24" -> "$", etc. 24 | var unescaper = strings.NewReplacer( 25 | "%21", "!", "%7E", "~", "%27", "'", 26 | "%28", "(", "%29", ")", "%3B", ";", 27 | "%2F", "/", "%3F", "?", "%3A", ":", 28 | "%40", "@", "%26", "&", "%3D", "=", 29 | "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*") 30 | 31 | // indexOf returns the first index of pattern in str, starting at str[i]. 32 | func indexOf(str string, pattern string, i int) int { 33 | if i > len(str)-1 { 34 | return -1 35 | } 36 | if i <= 0 { 37 | return strings.Index(str, pattern) 38 | } 39 | ind := strings.Index(str[i:], pattern) 40 | if ind == -1 { 41 | return -1 42 | } 43 | return ind + i 44 | } 45 | 46 | // lastIndexOf returns the last index of pattern in str, starting at str[i]. 47 | func lastIndexOf(str string, pattern string, i int) int { 48 | if i < 0 { 49 | return -1 50 | } 51 | if i >= len(str) { 52 | return strings.LastIndex(str, pattern) 53 | } 54 | _, size := utf8.DecodeRuneInString(str[i:]) 55 | return strings.LastIndex(str[:i+size], pattern) 56 | } 57 | 58 | // runesIndexOf returns the index of pattern in target, starting at target[i]. 59 | func runesIndexOf(target, pattern []rune, i int) int { 60 | if i > len(target)-1 { 61 | return -1 62 | } 63 | if i <= 0 { 64 | return runesIndex(target, pattern) 65 | } 66 | ind := runesIndex(target[i:], pattern) 67 | if ind == -1 { 68 | return -1 69 | } 70 | return ind + i 71 | } 72 | 73 | func runesEqual(r1, r2 []rune) bool { 74 | if len(r1) != len(r2) { 75 | return false 76 | } 77 | for i, c := range r1 { 78 | if c != r2[i] { 79 | return false 80 | } 81 | } 82 | return true 83 | } 84 | 85 | // runesIndex is the equivalent of strings.Index for rune slices. 86 | func runesIndex(r1, r2 []rune) int { 87 | last := len(r1) - len(r2) 88 | for i := 0; i <= last; i++ { 89 | if runesEqual(r1[i:i+len(r2)], r2) { 90 | return i 91 | } 92 | } 93 | return -1 94 | } 95 | 96 | func intArrayToString(ns []index) string { 97 | if len(ns) == 0 { 98 | return "" 99 | } 100 | 101 | b := []rune{} 102 | for _, n := range ns { 103 | b = append(b, intToRune(uint32(n))) 104 | } 105 | return string(b) 106 | } 107 | 108 | // These constants define the number of bits representable 109 | // in 1,2,3,4 byte utf8 sequences, respectively. 110 | const ONE_BYTE_BITS = 7 111 | const TWO_BYTE_BITS = 11 112 | const THREE_BYTE_BITS = 16 113 | const FOUR_BYTE_BITS = 21 114 | 115 | // Helper for getting a sequence of bits from an integer. 116 | func getBits(i uint32, cnt byte, from byte) byte { 117 | return byte((i >> from) & ((1 << cnt) - 1)) 118 | } 119 | 120 | // Converts an integer in the range 0~1112060 into a rune. 121 | // Based on the ranges table in https://en.wikipedia.org/wiki/UTF-8 122 | func intToRune(i uint32) rune { 123 | if i < (1 << ONE_BYTE_BITS) { 124 | return rune(i) 125 | } 126 | 127 | if i < (1 << TWO_BYTE_BITS) { 128 | r, size := utf8.DecodeRune([]byte{0b11000000 | getBits(i, 5, 6), 0b10000000 | getBits(i, 6, 0)}) 129 | if size != 2 || r == utf8.RuneError { 130 | panic(fmt.Sprintf("Error encoding an int %d with size 2, got rune %v and size %d", size, r, i)) 131 | } 132 | return r 133 | } 134 | 135 | // Last -3 here needed because for some reason 3rd to last codepoint 65533 in this range 136 | // was returning utf8.RuneError during encoding. 137 | if i < ((1 << THREE_BYTE_BITS) - UNICODE_INVALID_RANGE_DELTA - 3) { 138 | if i >= UNICODE_INVALID_RANGE_START { 139 | i += UNICODE_INVALID_RANGE_DELTA 140 | } 141 | 142 | r, size := utf8.DecodeRune([]byte{0b11100000 | getBits(i, 4, 12), 0b10000000 | getBits(i, 6, 6), 0b10000000 | getBits(i, 6, 0)}) 143 | if size != 3 || r == utf8.RuneError { 144 | panic(fmt.Sprintf("Error encoding an int %d with size 3, got rune %v and size %d", size, r, i)) 145 | } 146 | return r 147 | } 148 | 149 | if i < (1<= UNICODE_INVALID_RANGE_END { 178 | return result - UNICODE_INVALID_RANGE_DELTA 179 | } 180 | 181 | return result 182 | } 183 | 184 | if size == 4 { 185 | result := uint32(bytes[0]&0b111)<<18 | uint32(bytes[1]&0b111111)<<12 | uint32(bytes[2]&0b111111)<<6 | uint32(bytes[3]&0b111111) 186 | return result - UNICODE_INVALID_RANGE_DELTA - 3 187 | } 188 | 189 | panic(fmt.Sprintf("Unexpected state decoding rune=%v size=%d", r, size)) 190 | } 191 | -------------------------------------------------------------------------------- /diffmatchpatch/stringutil_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. 2 | // https://github.com/sergi/go-diff 3 | // See the included LICENSE file for license details. 4 | // 5 | // go-diff is a Go implementation of Google's Diff, Match, and Patch library 6 | // Original library is Copyright (c) 2006 Google Inc. 7 | // http://code.google.com/p/google-diff-match-patch/ 8 | 9 | package diffmatchpatch 10 | 11 | import ( 12 | "fmt" 13 | "testing" 14 | 15 | "github.com/stretchr/testify/assert" 16 | ) 17 | 18 | func TestRunesIndexOf(t *testing.T) { 19 | type TestCase struct { 20 | Pattern string 21 | Start int 22 | 23 | Expected int 24 | } 25 | 26 | for i, tc := range []TestCase{ 27 | {"abc", 0, 0}, 28 | {"cde", 0, 2}, 29 | {"e", 0, 4}, 30 | {"cdef", 0, -1}, 31 | {"abcdef", 0, -1}, 32 | {"abc", 2, -1}, 33 | {"cde", 2, 2}, 34 | {"e", 2, 4}, 35 | {"cdef", 2, -1}, 36 | {"abcdef", 2, -1}, 37 | {"e", 6, -1}, 38 | } { 39 | actual := runesIndexOf([]rune("abcde"), []rune(tc.Pattern), tc.Start) 40 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 41 | } 42 | } 43 | 44 | func TestIndexOf(t *testing.T) { 45 | type TestCase struct { 46 | String string 47 | Pattern string 48 | Position int 49 | 50 | Expected int 51 | } 52 | 53 | for i, tc := range []TestCase{ 54 | {"hi world", "world", -1, 3}, 55 | {"hi world", "world", 0, 3}, 56 | {"hi world", "world", 1, 3}, 57 | {"hi world", "world", 2, 3}, 58 | {"hi world", "world", 3, 3}, 59 | {"hi world", "world", 4, -1}, 60 | {"abbc", "b", -1, 1}, 61 | {"abbc", "b", 0, 1}, 62 | {"abbc", "b", 1, 1}, 63 | {"abbc", "b", 2, 2}, 64 | {"abbc", "b", 3, -1}, 65 | {"abbc", "b", 4, -1}, 66 | // The greek letter beta is the two-byte sequence of "\u03b2". 67 | {"a\u03b2\u03b2c", "\u03b2", -1, 1}, 68 | {"a\u03b2\u03b2c", "\u03b2", 0, 1}, 69 | {"a\u03b2\u03b2c", "\u03b2", 1, 1}, 70 | {"a\u03b2\u03b2c", "\u03b2", 3, 3}, 71 | {"a\u03b2\u03b2c", "\u03b2", 5, -1}, 72 | {"a\u03b2\u03b2c", "\u03b2", 6, -1}, 73 | } { 74 | actual := indexOf(tc.String, tc.Pattern, tc.Position) 75 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 76 | } 77 | } 78 | 79 | func TestLastIndexOf(t *testing.T) { 80 | type TestCase struct { 81 | String string 82 | Pattern string 83 | Position int 84 | 85 | Expected int 86 | } 87 | 88 | for i, tc := range []TestCase{ 89 | {"hi world", "world", -1, -1}, 90 | {"hi world", "world", 0, -1}, 91 | {"hi world", "world", 1, -1}, 92 | {"hi world", "world", 2, -1}, 93 | {"hi world", "world", 3, -1}, 94 | {"hi world", "world", 4, -1}, 95 | {"hi world", "world", 5, -1}, 96 | {"hi world", "world", 6, -1}, 97 | {"hi world", "world", 7, 3}, 98 | {"hi world", "world", 8, 3}, 99 | {"abbc", "b", -1, -1}, 100 | {"abbc", "b", 0, -1}, 101 | {"abbc", "b", 1, 1}, 102 | {"abbc", "b", 2, 2}, 103 | {"abbc", "b", 3, 2}, 104 | {"abbc", "b", 4, 2}, 105 | // The greek letter beta is the two-byte sequence of "\u03b2". 106 | {"a\u03b2\u03b2c", "\u03b2", -1, -1}, 107 | {"a\u03b2\u03b2c", "\u03b2", 0, -1}, 108 | {"a\u03b2\u03b2c", "\u03b2", 1, 1}, 109 | {"a\u03b2\u03b2c", "\u03b2", 3, 3}, 110 | {"a\u03b2\u03b2c", "\u03b2", 5, 3}, 111 | {"a\u03b2\u03b2c", "\u03b2", 6, 3}, 112 | } { 113 | actual := lastIndexOf(tc.String, tc.Pattern, tc.Position) 114 | assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) 115 | } 116 | } 117 | 118 | // Exhaustive check for all ints from 0 to 1112060 for correctness of implementation 119 | // of `intToRune() -> runeToInt()`. 120 | // This test is slow and runs longer than 5 seconds but it does provide a safety 121 | // guarantee that these 2 functions are correct for the ranges we support. 122 | func TestRuneToInt(t *testing.T) { 123 | 124 | for i := uint32(0); i <= UNICODE_RANGE_MAX-UNICODE_INVALID_RANGE_DELTA-3; i += 1 { 125 | r := intToRune(i) 126 | ic := runeToInt(r) 127 | 128 | assert.Equal(t, i, ic, fmt.Sprintf("intToRune(%d)=%d and runeToInt(%d)=%d", i, r, r, ic)) 129 | } 130 | 131 | assert.Panics(t, func() { 132 | intToRune(UNICODE_RANGE_MAX - UNICODE_INVALID_RANGE_DELTA - 2) 133 | }) 134 | } 135 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/sergi/go-diff 2 | 3 | require ( 4 | github.com/davecgh/go-spew v1.1.1 // indirect 5 | github.com/kr/pretty v0.1.0 // indirect 6 | github.com/stretchr/testify v1.4.0 7 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect 8 | gopkg.in/yaml.v2 v2.4.0 // indirect 9 | ) 10 | 11 | go 1.13 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 6 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 7 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 8 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 9 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 10 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 11 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 12 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 13 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 14 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 15 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 16 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 17 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 18 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 19 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 20 | gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= 21 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 22 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 23 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 24 | -------------------------------------------------------------------------------- /scripts/lint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -z ${PKG+x} ]; then echo "PKG is not set"; exit 1; fi 4 | if [ -z ${ROOT_DIR+x} ]; then echo "ROOT_DIR is not set"; exit 1; fi 5 | 6 | echo "gofmt:" 7 | OUT=$(gofmt -l $ROOT_DIR) 8 | if [ $(echo "$OUT\c" | wc -l) -ne 0 ]; then echo "$OUT"; PROBLEM=1; fi 9 | 10 | echo "errcheck:" 11 | OUT=$(errcheck $PKG/...) 12 | if [ $(echo "$OUT\c" | wc -l) -ne 0 ]; then echo "$OUT"; PROBLEM=1; fi 13 | 14 | echo "go vet:" 15 | OUT=$(go tool vet -all=true -v=true $ROOT_DIR 2>&1 | grep --invert-match -E "(Checking file|\%p of wrong type|can't check non-constant format)") 16 | if [ $(echo "$OUT\c" | wc -l) -ne 0 ]; then echo "$OUT"; PROBLEM=1; fi 17 | 18 | echo "golint:" 19 | OUT=$(golint $PKG/... | grep --invert-match -E "(method DiffPrettyHtml should be DiffPrettyHTML)") 20 | if [ $(echo "$OUT\c" | wc -l) -ne 0 ]; then echo "$OUT"; PROBLEM=1; fi 21 | 22 | if [ -n "$PROBLEM" ]; then exit 1; fi 23 | -------------------------------------------------------------------------------- /testdata/speedtest1.txt: -------------------------------------------------------------------------------- 1 | This is a '''list of newspapers published by [[Journal Register Company]]'''. 2 | 3 | The company owns daily and weekly newspapers, other print media properties and newspaper-affiliated local Websites in the [[U.S.]] states of [[Connecticut]], [[Michigan]], [[New York]], [[Ohio]] and [[Pennsylvania]], organized in six geographic "clusters":[http://www.journalregister.com/newspapers.html Journal Register Company: Our Newspapers], accessed February 10, 2008. 4 | 5 | == Capital-Saratoga == 6 | Three dailies, associated weeklies and [[pennysaver]]s in greater [[Albany, New York]]; also [http://www.capitalcentral.com capitalcentral.com] and [http://www.jobsinnewyork.com JobsInNewYork.com]. 7 | 8 | * ''The Oneida Daily Dispatch'' {{WS|oneidadispatch.com}} of [[Oneida, New York]] 9 | * ''[[The Record (Troy)|The Record]]'' {{WS|troyrecord.com}} of [[Troy, New York]] 10 | * ''[[The Saratogian]]'' {{WS|saratogian.com}} of [[Saratoga Springs, New York]] 11 | * Weeklies: 12 | ** ''Community News'' {{WS|cnweekly.com}} weekly of [[Clifton Park, New York]] 13 | ** ''Rome Observer'' of [[Rome, New York]] 14 | ** ''Life & Times of Utica'' of [[Utica, New York]] 15 | 16 | == Connecticut == 17 | Five dailies, associated weeklies and [[pennysaver]]s in the state of [[Connecticut]]; also [http://www.ctcentral.com CTcentral.com], [http://www.ctcarsandtrucks.com CTCarsAndTrucks.com] and [http://www.jobsinct.com JobsInCT.com]. 18 | 19 | * ''The Middletown Press'' {{WS|middletownpress.com}} of [[Middletown, Connecticut|Middletown]] 20 | * ''[[New Haven Register]]'' {{WS|newhavenregister.com}} of [[New Haven, Connecticut|New Haven]] 21 | * ''The Register Citizen'' {{WS|registercitizen.com}} of [[Torrington, Connecticut|Torrington]] 22 | 23 | * [[New Haven Register#Competitors|Elm City Newspapers]] {{WS|ctcentral.com}} 24 | ** ''The Advertiser'' of [[East Haven, Connecticut|East Haven]] 25 | ** ''Hamden Chronicle'' of [[Hamden, Connecticut|Hamden]] 26 | ** ''Milford Weekly'' of [[Milford, Connecticut|Milford]] 27 | ** ''The Orange Bulletin'' of [[Orange, Connecticut|Orange]] 28 | ** ''The Post'' of [[North Haven, Connecticut|North Haven]] 29 | ** ''Shelton Weekly'' of [[Shelton, Connecticut|Shelton]] 30 | ** ''The Stratford Bard'' of [[Stratford, Connecticut|Stratford]] 31 | ** ''Wallingford Voice'' of [[Wallingford, Connecticut|Wallingford]] 32 | ** ''West Haven News'' of [[West Haven, Connecticut|West Haven]] 33 | * Housatonic Publications 34 | ** ''The New Milford Times'' {{WS|newmilfordtimes.com}} of [[New Milford, Connecticut|New Milford]] 35 | ** ''The Brookfield Journal'' of [[Brookfield, Connecticut|Brookfield]] 36 | ** ''The Kent Good Times Dispatch'' of [[Kent, Connecticut|Kent]] 37 | ** ''The Bethel Beacon'' of [[Bethel, Connecticut|Bethel]] 38 | ** ''The Litchfield Enquirer'' of [[Litchfield, Connecticut|Litchfield]] 39 | ** ''Litchfield County Times'' of [[Litchfield, Connecticut|Litchfield]] 40 | * Imprint Newspapers {{WS|imprintnewspapers.com}} 41 | ** ''West Hartford News'' of [[West Hartford, Connecticut|West Hartford]] 42 | ** ''Windsor Journal'' of [[Windsor, Connecticut|Windsor]] 43 | ** ''Windsor Locks Journal'' of [[Windsor Locks, Connecticut|Windsor Locks]] 44 | ** ''Avon Post'' of [[Avon, Connecticut|Avon]] 45 | ** ''Farmington Post'' of [[Farmington, Connecticut|Farmington]] 46 | ** ''Simsbury Post'' of [[Simsbury, Connecticut|Simsbury]] 47 | ** ''Tri-Town Post'' of [[Burlington, Connecticut|Burlington]], [[Canton, Connecticut|Canton]] and [[Harwinton, Connecticut|Harwinton]] 48 | * Minuteman Publications 49 | ** ''[[Fairfield Minuteman]]'' of [[Fairfield, Connecticut|Fairfield]] 50 | ** ''The Westport Minuteman'' {{WS|westportminuteman.com}} of [[Westport, Connecticut|Westport]] 51 | * Shoreline Newspapers weeklies: 52 | ** ''Branford Review'' of [[Branford, Connecticut|Branford]] 53 | ** ''Clinton Recorder'' of [[Clinton, Connecticut|Clinton]] 54 | ** ''The Dolphin'' of [[Naval Submarine Base New London]] in [[New London, Connecticut|New London]] 55 | ** ''Main Street News'' {{WS|ctmainstreetnews.com}} of [[Essex, Connecticut|Essex]] 56 | ** ''Pictorial Gazette'' of [[Old Saybrook, Connecticut|Old Saybrook]] 57 | ** ''Regional Express'' of [[Colchester, Connecticut|Colchester]] 58 | ** ''Regional Standard'' of [[Colchester, Connecticut|Colchester]] 59 | ** ''Shoreline Times'' {{WS|shorelinetimes.com}} of [[Guilford, Connecticut|Guilford]] 60 | ** ''Shore View East'' of [[Madison, Connecticut|Madison]] 61 | ** ''Shore View West'' of [[Guilford, Connecticut|Guilford]] 62 | * Other weeklies: 63 | ** ''Registro'' {{WS|registroct.com}} of [[New Haven, Connecticut|New Haven]] 64 | ** ''Thomaston Express'' {{WS|thomastownexpress.com}} of [[Thomaston, Connecticut|Thomaston]] 65 | ** ''Foothills Traders'' {{WS|foothillstrader.com}} of Torrington, Bristol, Canton 66 | 67 | == Michigan == 68 | Four dailies, associated weeklies and [[pennysaver]]s in the state of [[Michigan]]; also [http://www.micentralhomes.com MIcentralhomes.com] and [http://www.micentralautos.com MIcentralautos.com] 69 | * ''[[Oakland Press]]'' {{WS|theoaklandpress.com}} of [[Oakland, Michigan|Oakland]] 70 | * ''Daily Tribune'' {{WS|dailytribune.com}} of [[Royal Oak, Michigan|Royal Oak]] 71 | * ''Macomb Daily'' {{WS|macombdaily.com}} of [[Mt. Clemens, Michigan|Mt. Clemens]] 72 | * ''[[Morning Sun]]'' {{WS|themorningsun.com}} of [[Mount Pleasant, Michigan|Mount Pleasant]] 73 | * Heritage Newspapers {{WS|heritage.com}} 74 | ** ''Belleville View'' 75 | ** ''Ile Camera'' 76 | ** ''Monroe Guardian'' 77 | ** ''Ypsilanti Courier'' 78 | ** ''News-Herald'' 79 | ** ''Press & Guide'' 80 | ** ''Chelsea Standard & Dexter Leader'' 81 | ** ''Manchester Enterprise'' 82 | ** ''Milan News-Leader'' 83 | ** ''Saline Reporter'' 84 | * Independent Newspapers {{WS|sourcenewspapers.com}} 85 | ** ''Advisor'' 86 | ** ''Source'' 87 | * Morning Star {{WS|morningstarpublishing.com}} 88 | ** ''Alma Reminder'' 89 | ** ''Alpena Star'' 90 | ** ''Antrim County News'' 91 | ** ''Carson City Reminder'' 92 | ** ''The Leader & Kalkaskian'' 93 | ** ''Ogemaw/Oscoda County Star'' 94 | ** ''Petoskey/Charlevoix Star'' 95 | ** ''Presque Isle Star'' 96 | ** ''Preview Community Weekly'' 97 | ** ''Roscommon County Star'' 98 | ** ''St. Johns Reminder'' 99 | ** ''Straits Area Star'' 100 | ** ''The (Edmore) Advertiser'' 101 | * Voice Newspapers {{WS|voicenews.com}} 102 | ** ''Armada Times'' 103 | ** ''Bay Voice'' 104 | ** ''Blue Water Voice'' 105 | ** ''Downriver Voice'' 106 | ** ''Macomb Township Voice'' 107 | ** ''North Macomb Voice'' 108 | ** ''Weekend Voice'' 109 | ** ''Suburban Lifestyles'' {{WS|suburbanlifestyles.com}} 110 | 111 | == Mid-Hudson == 112 | One daily, associated magazines in the [[Hudson River Valley]] of [[New York]]; also [http://www.midhudsoncentral.com MidHudsonCentral.com] and [http://www.jobsinnewyork.com JobsInNewYork.com]. 113 | 114 | * ''[[Daily Freeman]]'' {{WS|dailyfreeman.com}} of [[Kingston, New York]] 115 | 116 | == Ohio == 117 | Two dailies, associated magazines and three shared Websites, all in the state of [[Ohio]]: [http://www.allaroundcleveland.com AllAroundCleveland.com], [http://www.allaroundclevelandcars.com AllAroundClevelandCars.com] and [http://www.allaroundclevelandjobs.com AllAroundClevelandJobs.com]. 118 | 119 | * ''[[The News-Herald (Ohio)|The News-Herald]]'' {{WS|news-herald.com}} of [[Willoughby, Ohio|Willoughby]] 120 | * ''[[The Morning Journal]]'' {{WS|morningjournal.com}} of [[Lorain, Ohio|Lorain]] 121 | 122 | == Philadelphia area == 123 | Seven dailies and associated weeklies and magazines in [[Pennsylvania]] and [[New Jersey]], and associated Websites: [http://www.allaroundphilly.com AllAroundPhilly.com], [http://www.jobsinnj.com JobsInNJ.com], [http://www.jobsinpa.com JobsInPA.com], and [http://www.phillycarsearch.com PhillyCarSearch.com]. 124 | 125 | * ''The Daily Local'' {{WS|dailylocal.com}} of [[West Chester, Pennsylvania|West Chester]] 126 | * ''[[Delaware County Daily and Sunday Times]] {{WS|delcotimes.com}} of Primos 127 | * ''[[The Mercury (Pennsylvania)|The Mercury]]'' {{WS|pottstownmercury.com}} of [[Pottstown, Pennsylvania|Pottstown]] 128 | * ''The Phoenix'' {{WS|phoenixvillenews.com}} of [[Phoenixville, Pennsylvania|Phoenixville]] 129 | * ''[[The Reporter (Lansdale)|The Reporter]]'' {{WS|thereporteronline.com}} of [[Lansdale, Pennsylvania|Lansdale]] 130 | * ''The Times Herald'' {{WS|timesherald.com}} of [[Norristown, Pennsylvania|Norristown]] 131 | * ''[[The Trentonian]]'' {{WS|trentonian.com}} of [[Trenton, New Jersey]] 132 | 133 | * Weeklies 134 | ** ''El Latino Expreso'' of [[Trenton, New Jersey]] 135 | ** ''La Voz'' of [[Norristown, Pennsylvania]] 136 | ** ''The Village News'' of [[Downingtown, Pennsylvania]] 137 | ** ''The Times Record'' of [[Kennett Square, Pennsylvania]] 138 | ** ''The Tri-County Record'' {{WS|tricountyrecord.com}} of [[Morgantown, Pennsylvania]] 139 | ** ''News of Delaware County'' {{WS|newsofdelawarecounty.com}}of [[Havertown, Pennsylvania]] 140 | ** ''Main Line Times'' {{WS|mainlinetimes.com}}of [[Ardmore, Pennsylvania]] 141 | ** ''Penny Pincher'' of [[Pottstown, Pennsylvania]] 142 | ** ''Town Talk'' {{WS|towntalknews.com}} of [[Ridley, Pennsylvania]] 143 | * Chesapeake Publishing {{WS|pa8newsgroup.com}} 144 | ** ''Solanco Sun Ledger'' of [[Quarryville, Pennsylvania]] 145 | ** ''Columbia Ledger'' of [[Columbia, Pennsylvania]] 146 | ** ''Coatesville Ledger'' of [[Downingtown, Pennsylvania]] 147 | ** ''Parkesburg Post Ledger'' of [[Quarryville, Pennsylvania]] 148 | ** ''Downingtown Ledger'' of [[Downingtown, Pennsylvania]] 149 | ** ''The Kennett Paper'' of [[Kennett Square, Pennsylvania]] 150 | ** ''Avon Grove Sun'' of [[West Grove, Pennsylvania]] 151 | ** ''Oxford Tribune'' of [[Oxford, Pennsylvania]] 152 | ** ''Elizabethtown Chronicle'' of [[Elizabethtown, Pennsylvania]] 153 | ** ''Donegal Ledger'' of [[Donegal, Pennsylvania]] 154 | ** ''Chadds Ford Post'' of [[Chadds Ford, Pennsylvania]] 155 | ** ''The Central Record'' of [[Medford, New Jersey]] 156 | ** ''Maple Shade Progress'' of [[Maple Shade, New Jersey]] 157 | * Intercounty Newspapers {{WS|buckslocalnews.com}} 158 | ** ''The Review'' of Roxborough, Pennsylvania 159 | ** ''The Recorder'' of [[Conshohocken, Pennsylvania]] 160 | ** ''The Leader'' of [[Mount Airy, Pennsylvania|Mount Airy]] and West Oak Lake, Pennsylvania 161 | ** ''The Pennington Post'' of [[Pennington, New Jersey]] 162 | ** ''The Bristol Pilot'' of [[Bristol, Pennsylvania]] 163 | ** ''Yardley News'' of [[Yardley, Pennsylvania]] 164 | ** ''New Hope Gazette'' of [[New Hope, Pennsylvania]] 165 | ** ''Doylestown Patriot'' of [[Doylestown, Pennsylvania]] 166 | ** ''Newtown Advance'' of [[Newtown, Pennsylvania]] 167 | ** ''The Plain Dealer'' of [[Williamstown, New Jersey]] 168 | ** ''News Report'' of [[Sewell, New Jersey]] 169 | ** ''Record Breeze'' of [[Berlin, New Jersey]] 170 | ** ''Newsweekly'' of [[Moorestown, New Jersey]] 171 | ** ''Haddon Herald'' of [[Haddonfield, New Jersey]] 172 | ** ''New Egypt Press'' of [[New Egypt, New Jersey]] 173 | ** ''Community News'' of [[Pemberton, New Jersey]] 174 | ** ''Plymouth Meeting Journal'' of [[Plymouth Meeting, Pennsylvania]] 175 | ** ''Lafayette Hill Journal'' of [[Lafayette Hill, Pennsylvania]] 176 | * Montgomery Newspapers {{WS|montgomerynews.com}} 177 | ** ''Ambler Gazette'' of [[Ambler, Pennsylvania]] 178 | ** ''Central Bucks Life'' of [[Bucks County, Pennsylvania]] 179 | ** ''The Colonial'' of [[Plymouth Meeting, Pennsylvania]] 180 | ** ''Glenside News'' of [[Glenside, Pennsylvania]] 181 | ** ''The Globe'' of [[Lower Moreland Township, Pennsylvania]] 182 | ** ''Main Line Life'' of [[Ardmore, Pennsylvania]] 183 | ** ''Montgomery Life'' of [[Fort Washington, Pennsylvania]] 184 | ** ''North Penn Life'' of [[Lansdale, Pennsylvania]] 185 | ** ''Perkasie News Herald'' of [[Perkasie, Pennsylvania]] 186 | ** ''Public Spirit'' of [[Hatboro, Pennsylvania]] 187 | ** ''Souderton Independent'' of [[Souderton, Pennsylvania]] 188 | ** ''Springfield Sun'' of [[Springfield, Pennsylvania]] 189 | ** ''Spring-Ford Reporter'' of [[Royersford, Pennsylvania]] 190 | ** ''Times Chronicle'' of [[Jenkintown, Pennsylvania]] 191 | ** ''Valley Item'' of [[Perkiomenville, Pennsylvania]] 192 | ** ''Willow Grove Guide'' of [[Willow Grove, Pennsylvania]] 193 | * News Gleaner Publications (closed December 2008) {{WS|newsgleaner.com}} 194 | ** ''Life Newspapers'' of [[Philadelphia, Pennsylvania]] 195 | * Suburban Publications 196 | ** ''The Suburban & Wayne Times'' {{WS|waynesuburban.com}} of [[Wayne, Pennsylvania]] 197 | ** ''The Suburban Advertiser'' of [[Exton, Pennsylvania]] 198 | ** ''The King of Prussia Courier'' of [[King of Prussia, Pennsylvania]] 199 | * Press Newspapers {{WS|countypressonline.com}} 200 | ** ''County Press'' of [[Newtown Square, Pennsylvania]] 201 | ** ''Garnet Valley Press'' of [[Glen Mills, Pennsylvania]] 202 | ** ''Haverford Press'' of [[Newtown Square, Pennsylvania]] (closed January 2009) 203 | ** ''Hometown Press'' of [[Glen Mills, Pennsylvania]] (closed January 2009) 204 | ** ''Media Press'' of [[Newtown Square, Pennsylvania]] (closed January 2009) 205 | ** ''Springfield Press'' of [[Springfield, Pennsylvania]] 206 | * Berks-Mont Newspapers {{WS|berksmontnews.com}} 207 | ** ''The Boyertown Area Times'' of [[Boyertown, Pennsylvania]] 208 | ** ''The Kutztown Area Patriot'' of [[Kutztown, Pennsylvania]] 209 | ** ''The Hamburg Area Item'' of [[Hamburg, Pennsylvania]] 210 | ** ''The Southern Berks News'' of [[Exeter Township, Berks County, Pennsylvania]] 211 | ** ''The Free Press'' of [[Quakertown, Pennsylvania]] 212 | ** ''The Saucon News'' of [[Quakertown, Pennsylvania]] 213 | ** ''Westside Weekly'' of [[Reading, Pennsylvania]] 214 | 215 | * Magazines 216 | ** ''Bucks Co. Town & Country Living'' 217 | ** ''Chester Co. Town & Country Living'' 218 | ** ''Montomgery Co. Town & Country Living'' 219 | ** ''Garden State Town & Country Living'' 220 | ** ''Montgomery Homes'' 221 | ** ''Philadelphia Golfer'' 222 | ** ''Parents Express'' 223 | ** ''Art Matters'' 224 | 225 | {{JRC}} 226 | 227 | ==References== 228 | 229 | 230 | [[Category:Journal Register publications|*]] 231 | -------------------------------------------------------------------------------- /testdata/speedtest2.txt: -------------------------------------------------------------------------------- 1 | This is a '''list of newspapers published by [[Journal Register Company]]'''. 2 | 3 | The company owns daily and weekly newspapers, other print media properties and newspaper-affiliated local Websites in the [[U.S.]] states of [[Connecticut]], [[Michigan]], [[New York]], [[Ohio]], [[Pennsylvania]] and [[New Jersey]], organized in six geographic "clusters":[http://www.journalregister.com/publications.html Journal Register Company: Our Publications], accessed April 21, 2010. 4 | 5 | == Capital-Saratoga == 6 | Three dailies, associated weeklies and [[pennysaver]]s in greater [[Albany, New York]]; also [http://www.capitalcentral.com capitalcentral.com] and [http://www.jobsinnewyork.com JobsInNewYork.com]. 7 | 8 | * ''The Oneida Daily Dispatch'' {{WS|oneidadispatch.com}} of [[Oneida, New York]] 9 | * ''[[The Record (Troy)|The Record]]'' {{WS|troyrecord.com}} of [[Troy, New York]] 10 | * ''[[The Saratogian]]'' {{WS|saratogian.com}} of [[Saratoga Springs, New York]] 11 | * Weeklies: 12 | ** ''Community News'' {{WS|cnweekly.com}} weekly of [[Clifton Park, New York]] 13 | ** ''Rome Observer'' {{WS|romeobserver.com}} of [[Rome, New York]] 14 | ** ''WG Life '' {{WS|saratogian.com/wglife/}} of [[Wilton, New York]] 15 | ** ''Ballston Spa Life '' {{WS|saratogian.com/bspalife}} of [[Ballston Spa, New York]] 16 | ** ''Greenbush Life'' {{WS|troyrecord.com/greenbush}} of [[Troy, New York]] 17 | ** ''Latham Life'' {{WS|troyrecord.com/latham}} of [[Latham, New York]] 18 | ** ''River Life'' {{WS|troyrecord.com/river}} of [[Troy, New York]] 19 | 20 | == Connecticut == 21 | Three dailies, associated weeklies and [[pennysaver]]s in the state of [[Connecticut]]; also [http://www.ctcentral.com CTcentral.com], [http://www.ctcarsandtrucks.com CTCarsAndTrucks.com] and [http://www.jobsinct.com JobsInCT.com]. 22 | 23 | * ''The Middletown Press'' {{WS|middletownpress.com}} of [[Middletown, Connecticut|Middletown]] 24 | * ''[[New Haven Register]]'' {{WS|newhavenregister.com}} of [[New Haven, Connecticut|New Haven]] 25 | * ''The Register Citizen'' {{WS|registercitizen.com}} of [[Torrington, Connecticut|Torrington]] 26 | 27 | * Housatonic Publications 28 | ** ''The Housatonic Times'' {{WS|housatonictimes.com}} of [[New Milford, Connecticut|New Milford]] 29 | ** ''Litchfield County Times'' {{WS|countytimes.com}} of [[Litchfield, Connecticut|Litchfield]] 30 | 31 | * Minuteman Publications 32 | ** ''[[Fairfield Minuteman]]'' {{WS|fairfieldminuteman.com}}of [[Fairfield, Connecticut|Fairfield]] 33 | ** ''The Westport Minuteman'' {{WS|westportminuteman.com}} of [[Westport, Connecticut|Westport]] 34 | 35 | * Shoreline Newspapers 36 | ** ''The Dolphin'' {{WS|dolphin-news.com}} of [[Naval Submarine Base New London]] in [[New London, Connecticut|New London]] 37 | ** ''Shoreline Times'' {{WS|shorelinetimes.com}} of [[Guilford, Connecticut|Guilford]] 38 | 39 | * Foothills Media Group {{WS|foothillsmediagroup.com}} 40 | ** ''Thomaston Express'' {{WS|thomastonexpress.com}} of [[Thomaston, Connecticut|Thomaston]] 41 | ** ''Good News About Torrington'' {{WS|goodnewsabouttorrington.com}} of [[Torrington, Connecticut|Torrington]] 42 | ** ''Granby News'' {{WS|foothillsmediagroup.com/granby}} of [[Granby, Connecticut|Granby]] 43 | ** ''Canton News'' {{WS|foothillsmediagroup.com/canton}} of [[Canton, Connecticut|Canton]] 44 | ** ''Avon News'' {{WS|foothillsmediagroup.com/avon}} of [[Avon, Connecticut|Avon]] 45 | ** ''Simsbury News'' {{WS|foothillsmediagroup.com/simsbury}} of [[Simsbury, Connecticut|Simsbury]] 46 | ** ''Litchfield News'' {{WS|foothillsmediagroup.com/litchfield}} of [[Litchfield, Connecticut|Litchfield]] 47 | ** ''Foothills Trader'' {{WS|foothillstrader.com}} of Torrington, Bristol, Canton 48 | 49 | * Other weeklies 50 | ** ''The Milford-Orange Bulletin'' {{WS|ctbulletin.com}} of [[Orange, Connecticut|Orange]] 51 | ** ''The Post-Chronicle'' {{WS|ctpostchronicle.com}} of [[North Haven, Connecticut|North Haven]] 52 | ** ''West Hartford News'' {{WS|westhartfordnews.com}} of [[West Hartford, Connecticut|West Hartford]] 53 | 54 | * Magazines 55 | ** ''The Connecticut Bride'' {{WS|connecticutmag.com}} 56 | ** ''Connecticut Magazine'' {{WS|theconnecticutbride.com}} 57 | ** ''Passport Magazine'' {{WS|passport-mag.com}} 58 | 59 | == Michigan == 60 | Four dailies, associated weeklies and [[pennysaver]]s in the state of [[Michigan]]; also [http://www.micentralhomes.com MIcentralhomes.com] and [http://www.micentralautos.com MIcentralautos.com] 61 | * ''[[Oakland Press]]'' {{WS|theoaklandpress.com}} of [[Oakland, Michigan|Oakland]] 62 | * ''Daily Tribune'' {{WS|dailytribune.com}} of [[Royal Oak, Michigan|Royal Oak]] 63 | * ''Macomb Daily'' {{WS|macombdaily.com}} of [[Mt. Clemens, Michigan|Mt. Clemens]] 64 | * ''[[Morning Sun]]'' {{WS|themorningsun.com}} of [[Mount Pleasant, Michigan|Mount Pleasant]] 65 | 66 | * Heritage Newspapers {{WS|heritage.com}} 67 | ** ''Belleville View'' {{WS|bellevilleview.com}} 68 | ** ''Ile Camera'' {{WS|thenewsherald.com/ile_camera}} 69 | ** ''Monroe Guardian'' {{WS|monreguardian.com}} 70 | ** ''Ypsilanti Courier'' {{WS|ypsilanticourier.com}} 71 | ** ''News-Herald'' {{WS|thenewsherald.com}} 72 | ** ''Press & Guide'' {{WS|pressandguide.com}} 73 | ** ''Chelsea Standard & Dexter Leader'' {{WS|chelseastandard.com}} 74 | ** ''Manchester Enterprise'' {{WS|manchesterguardian.com}} 75 | ** ''Milan News-Leader'' {{WS|milannews.com}} 76 | ** ''Saline Reporter'' {{WS|salinereporter.com}} 77 | * Independent Newspapers 78 | ** ''Advisor'' {{WS|sourcenewspapers.com}} 79 | ** ''Source'' {{WS|sourcenewspapers.com}} 80 | * Morning Star {{WS|morningstarpublishing.com}} 81 | ** ''The Leader & Kalkaskian'' {{WS|leaderandkalkaskian.com}} 82 | ** ''Grand Traverse Insider'' {{WS|grandtraverseinsider.com}} 83 | ** ''Alma Reminder'' 84 | ** ''Alpena Star'' 85 | ** ''Ogemaw/Oscoda County Star'' 86 | ** ''Presque Isle Star'' 87 | ** ''St. Johns Reminder'' 88 | 89 | * Voice Newspapers {{WS|voicenews.com}} 90 | ** ''Armada Times'' 91 | ** ''Bay Voice'' 92 | ** ''Blue Water Voice'' 93 | ** ''Downriver Voice'' 94 | ** ''Macomb Township Voice'' 95 | ** ''North Macomb Voice'' 96 | ** ''Weekend Voice'' 97 | 98 | == Mid-Hudson == 99 | One daily, associated magazines in the [[Hudson River Valley]] of [[New York]]; also [http://www.midhudsoncentral.com MidHudsonCentral.com] and [http://www.jobsinnewyork.com JobsInNewYork.com]. 100 | 101 | * ''[[Daily Freeman]]'' {{WS|dailyfreeman.com}} of [[Kingston, New York]] 102 | * ''Las Noticias'' {{WS|lasnoticiasny.com}} of [[Kingston, New York]] 103 | 104 | == Ohio == 105 | Two dailies, associated magazines and three shared Websites, all in the state of [[Ohio]]: [http://www.allaroundcleveland.com AllAroundCleveland.com], [http://www.allaroundclevelandcars.com AllAroundClevelandCars.com] and [http://www.allaroundclevelandjobs.com AllAroundClevelandJobs.com]. 106 | 107 | * ''[[The News-Herald (Ohio)|The News-Herald]]'' {{WS|news-herald.com}} of [[Willoughby, Ohio|Willoughby]] 108 | * ''[[The Morning Journal]]'' {{WS|morningjournal.com}} of [[Lorain, Ohio|Lorain]] 109 | * ''El Latino Expreso'' {{WS|lorainlatino.com}} of [[Lorain, Ohio|Lorain]] 110 | 111 | == Philadelphia area == 112 | Seven dailies and associated weeklies and magazines in [[Pennsylvania]] and [[New Jersey]], and associated Websites: [http://www.allaroundphilly.com AllAroundPhilly.com], [http://www.jobsinnj.com JobsInNJ.com], [http://www.jobsinpa.com JobsInPA.com], and [http://www.phillycarsearch.com PhillyCarSearch.com]. 113 | 114 | * ''[[The Daily Local News]]'' {{WS|dailylocal.com}} of [[West Chester, Pennsylvania|West Chester]] 115 | * ''[[Delaware County Daily and Sunday Times]] {{WS|delcotimes.com}} of Primos [[Upper Darby Township, Pennsylvania]] 116 | * ''[[The Mercury (Pennsylvania)|The Mercury]]'' {{WS|pottstownmercury.com}} of [[Pottstown, Pennsylvania|Pottstown]] 117 | * ''[[The Reporter (Lansdale)|The Reporter]]'' {{WS|thereporteronline.com}} of [[Lansdale, Pennsylvania|Lansdale]] 118 | * ''The Times Herald'' {{WS|timesherald.com}} of [[Norristown, Pennsylvania|Norristown]] 119 | * ''[[The Trentonian]]'' {{WS|trentonian.com}} of [[Trenton, New Jersey]] 120 | 121 | * Weeklies 122 | * ''The Phoenix'' {{WS|phoenixvillenews.com}} of [[Phoenixville, Pennsylvania]] 123 | ** ''El Latino Expreso'' {{WS|njexpreso.com}} of [[Trenton, New Jersey]] 124 | ** ''La Voz'' {{WS|lavozpa.com}} of [[Norristown, Pennsylvania]] 125 | ** ''The Tri County Record'' {{WS|tricountyrecord.com}} of [[Morgantown, Pennsylvania]] 126 | ** ''Penny Pincher'' {{WS|pennypincherpa.com}}of [[Pottstown, Pennsylvania]] 127 | 128 | * Chesapeake Publishing {{WS|southernchestercountyweeklies.com}} 129 | ** ''The Kennett Paper'' {{WS|kennettpaper.com}} of [[Kennett Square, Pennsylvania]] 130 | ** ''Avon Grove Sun'' {{WS|avongrovesun.com}} of [[West Grove, Pennsylvania]] 131 | ** ''The Central Record'' {{WS|medfordcentralrecord.com}} of [[Medford, New Jersey]] 132 | ** ''Maple Shade Progress'' {{WS|mapleshadeprogress.com}} of [[Maple Shade, New Jersey]] 133 | 134 | * Intercounty Newspapers {{WS|buckslocalnews.com}} {{WS|southjerseylocalnews.com}} 135 | ** ''The Pennington Post'' {{WS|penningtonpost.com}} of [[Pennington, New Jersey]] 136 | ** ''The Bristol Pilot'' {{WS|bristolpilot.com}} of [[Bristol, Pennsylvania]] 137 | ** ''Yardley News'' {{WS|yardleynews.com}} of [[Yardley, Pennsylvania]] 138 | ** ''Advance of Bucks County'' {{WS|advanceofbucks.com}} of [[Newtown, Pennsylvania]] 139 | ** ''Record Breeze'' {{WS|recordbreeze.com}} of [[Berlin, New Jersey]] 140 | ** ''Community News'' {{WS|sjcommunitynews.com}} of [[Pemberton, New Jersey]] 141 | 142 | * Montgomery Newspapers {{WS|montgomerynews.com}} 143 | ** ''Ambler Gazette'' {{WS|amblergazette.com}} of [[Ambler, Pennsylvania]] 144 | ** ''The Colonial'' {{WS|colonialnews.com}} of [[Plymouth Meeting, Pennsylvania]] 145 | ** ''Glenside News'' {{WS|glensidenews.com}} of [[Glenside, Pennsylvania]] 146 | ** ''The Globe'' {{WS|globenewspaper.com}} of [[Lower Moreland Township, Pennsylvania]] 147 | ** ''Montgomery Life'' {{WS|montgomerylife.com}} of [[Fort Washington, Pennsylvania]] 148 | ** ''North Penn Life'' {{WS|northpennlife.com}} of [[Lansdale, Pennsylvania]] 149 | ** ''Perkasie News Herald'' {{WS|perkasienewsherald.com}} of [[Perkasie, Pennsylvania]] 150 | ** ''Public Spirit'' {{WS|thepublicspirit.com}} of [[Hatboro, Pennsylvania]] 151 | ** ''Souderton Independent'' {{WS|soudertonindependent.com}} of [[Souderton, Pennsylvania]] 152 | ** ''Springfield Sun'' {{WS|springfieldsun.com}} of [[Springfield, Pennsylvania]] 153 | ** ''Spring-Ford Reporter'' {{WS|springfordreporter.com}} of [[Royersford, Pennsylvania]] 154 | ** ''Times Chronicle'' {{WS|thetimeschronicle.com}} of [[Jenkintown, Pennsylvania]] 155 | ** ''Valley Item'' {{WS|valleyitem.com}} of [[Perkiomenville, Pennsylvania]] 156 | ** ''Willow Grove Guide'' {{WS|willowgroveguide.com}} of [[Willow Grove, Pennsylvania]] 157 | ** ''The Review'' {{WS|roxreview.com}} of [[Roxborough, Philadelphia, Pennsylvania]] 158 | 159 | * Main Line Media News {{WS|mainlinemedianews.com}} 160 | ** ''Main Line Times'' {{WS|mainlinetimes.com}} of [[Ardmore, Pennsylvania]] 161 | ** ''Main Line Life'' {{WS|mainlinelife.com}} of [[Ardmore, Pennsylvania]] 162 | ** ''The King of Prussia Courier'' {{WS|kingofprussiacourier.com}} of [[King of Prussia, Pennsylvania]] 163 | 164 | * Delaware County News Network {{WS|delconewsnetwork.com}} 165 | ** ''News of Delaware County'' {{WS|newsofdelawarecounty.com}} of [[Havertown, Pennsylvania]] 166 | ** ''County Press'' {{WS|countypressonline.com}} of [[Newtown Square, Pennsylvania]] 167 | ** ''Garnet Valley Press'' {{WS|countypressonline.com}} of [[Glen Mills, Pennsylvania]] 168 | ** ''Springfield Press'' {{WS|countypressonline.com}} of [[Springfield, Pennsylvania]] 169 | ** ''Town Talk'' {{WS|towntalknews.com}} of [[Ridley, Pennsylvania]] 170 | 171 | * Berks-Mont Newspapers {{WS|berksmontnews.com}} 172 | ** ''The Boyertown Area Times'' {{WS|berksmontnews.com/boyertown_area_times}} of [[Boyertown, Pennsylvania]] 173 | ** ''The Kutztown Area Patriot'' {{WS|berksmontnews.com/kutztown_area_patriot}} of [[Kutztown, Pennsylvania]] 174 | ** ''The Hamburg Area Item'' {{WS|berksmontnews.com/hamburg_area_item}} of [[Hamburg, Pennsylvania]] 175 | ** ''The Southern Berks News'' {{WS|berksmontnews.com/southern_berks_news}} of [[Exeter Township, Berks County, Pennsylvania]] 176 | ** ''Community Connection'' {{WS|berksmontnews.com/community_connection}} of [[Boyertown, Pennsylvania]] 177 | 178 | * Magazines 179 | ** ''Bucks Co. Town & Country Living'' {{WS|buckscountymagazine.com}} 180 | ** ''Parents Express'' {{WS|parents-express.com}} 181 | ** ''Real Men, Rednecks'' {{WS|realmenredneck.com}} 182 | 183 | {{JRC}} 184 | 185 | ==References== 186 | 187 | 188 | [[Category:Journal Register publications|*]] 189 | --------------------------------------------------------------------------------