From 65165c5bf578ccf7b0952e4a5b36c7de1046d05b Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 8 Jan 2025 07:11:29 -0600 Subject: [PATCH 1/2] Escape Whitespace in DiffPrettyText Displace escape sequence for whitespace characters in diffs. --- diffmatchpatch/diff.go | 26 ++++++++++++++++++++++++++ diffmatchpatch/diff_test.go | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/diffmatchpatch/diff.go b/diffmatchpatch/diff.go index 915d509..7c1ca77 100644 --- a/diffmatchpatch/diff.go +++ b/diffmatchpatch/diff.go @@ -1138,6 +1138,30 @@ func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string { return buff.String() } +func escapeWhitespace(input string) string { + // Define a map of whitespace characters and their escaped versions + escapeMap := map[string]string{ + "\n": "\\n", + "\t": "\\t", + "\r": "\\r", + " ": "\\s", + } + + // Use a regular expression to match all whitespace characters + re := regexp.MustCompile(`\s`) + + // Replace each whitespace character using the escapeMap + result := re.ReplaceAllStringFunc(input, func(match string) string { + escaped, exists := escapeMap[match] + if !exists { + escaped = fmt.Sprintf("\\x%x", match[0]) // Default escape for unknown whitespace + } + return escaped + }) + + return result +} + // DiffPrettyText converts a []Diff into a colored text report. func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string { var buff bytes.Buffer @@ -1146,10 +1170,12 @@ func (dmp *DiffMatchPatch) DiffPrettyText(diffs []Diff) string { switch diff.Type { case DiffInsert: + text = escapeWhitespace(text) _, _ = buff.WriteString("\x1b[32m") _, _ = buff.WriteString(text) _, _ = buff.WriteString("\x1b[0m") case DiffDelete: + text = escapeWhitespace(text) _, _ = buff.WriteString("\x1b[31m") _, _ = buff.WriteString(text) _, _ = buff.WriteString("\x1b[0m") diff --git a/diffmatchpatch/diff_test.go b/diffmatchpatch/diff_test.go index 4532b79..dfe20ab 100644 --- a/diffmatchpatch/diff_test.go +++ b/diffmatchpatch/diff_test.go @@ -1031,6 +1031,15 @@ func TestDiffPrettyText(t *testing.T) { Expected: "a\n\x1b[31mb\x1b[0m\x1b[32mc&d\x1b[0m", }, + { + Diffs: []Diff{ + {DiffEqual, "a\n"}, + {DiffDelete, "\n"}, + {DiffInsert, "c\td e"}, + }, + + Expected: "a\n\x1b[31m\\n\x1b[0m\x1b[32mc\\td\\se\x1b[0m", + }, } { actual := dmp.DiffPrettyText(tc.Diffs) assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc)) From 5510bcb7896d6ffdf06e3b799657d3140b515a1b Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 21 May 2025 13:42:54 -0500 Subject: [PATCH 2/2] Fork github.com/sergi/go-diff --- Makefile | 2 +- README.md | 12 ++++++------ diffmatchpatch/benchutil_test.go | 2 +- diffmatchpatch/diff.go | 4 ++-- diffmatchpatch/diff_test.go | 2 +- diffmatchpatch/diffmatchpatch.go | 2 +- diffmatchpatch/match.go | 2 +- diffmatchpatch/match_test.go | 2 +- diffmatchpatch/mathutil.go | 2 +- diffmatchpatch/patch.go | 2 +- diffmatchpatch/patch_test.go | 2 +- diffmatchpatch/stringutil.go | 2 +- diffmatchpatch/stringutil_test.go | 2 +- go.mod | 2 +- 14 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 710bc83..4ddf133 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: all clean clean-coverage install install-dependencies install-tools lint test test-verbose test-with-coverage export ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) -export PKG := github.com/sergi/go-diff +export PKG := github.com/cwarden/go-diff export ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) $(eval $(ARGS):;@:) # turn arguments into do-nothing targets diff --git a/README.md b/README.md index 597437b..a1c326d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 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) +# go-diff [![GoDoc](https://godoc.org/github.com/cwarden/go-diff?status.png)](https://godoc.org/github.com/cwarden/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) go-diff offers algorithms to perform operations required for synchronizing plain text: @@ -9,7 +9,7 @@ go-diff offers algorithms to perform operations required for synchronizing plain ## Installation ```bash -go get -u github.com/sergi/go-diff/... +go get -u github.com/cwarden/go-diff/... ``` ## Usage @@ -22,7 +22,7 @@ package main import ( "fmt" - "github.com/sergi/go-diff/diffmatchpatch" + "github.com/cwarden/go-diff/diffmatchpatch" ) const ( @@ -41,11 +41,11 @@ func main() { ## Found a bug or are you missing a feature in go-diff? -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). +Please make sure to have the latest version of go-diff. If the problem still persists go through the [open issues](https://github.com/cwarden/go-diff/issues) in the tracker first. If you cannot find your request just open up a [new issue](https://github.com/cwarden/go-diff/issues/new). ## How to contribute? -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. +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/cwarden/go-diff/issues). Just choose something you think you can work on and discuss your plans in the issue by commenting on it. 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. @@ -75,7 +75,7 @@ This Go version of Diff, Match and Patch Library is licensed under the [MIT Lice Go version of Diff, Match and Patch Library > Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -> +> 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: diff --git a/diffmatchpatch/benchutil_test.go b/diffmatchpatch/benchutil_test.go index b8e404d..2063e77 100644 --- a/diffmatchpatch/benchutil_test.go +++ b/diffmatchpatch/benchutil_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/diff.go b/diffmatchpatch/diff.go index 7c1ca77..e70a620 100644 --- a/diffmatchpatch/diff.go +++ b/diffmatchpatch/diff.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library @@ -444,7 +444,7 @@ func commonPrefixLength(text1, text2 []rune) int { // commonSuffixLength returns the length of the common suffix of two rune slices. func commonSuffixLength(text1, text2 []rune) int { // Use linear search rather than the binary search discussed at https://neil.fraser.name/news/2007/10/09/. - // See discussion at https://github.com/sergi/go-diff/issues/54. + // See discussion at https://github.com/cwarden/go-diff/issues/54. i1 := len(text1) i2 := len(text2) for n := 0; ; n++ { diff --git a/diffmatchpatch/diff_test.go b/diffmatchpatch/diff_test.go index dfe20ab..21043bd 100644 --- a/diffmatchpatch/diff_test.go +++ b/diffmatchpatch/diff_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/diffmatchpatch.go b/diffmatchpatch/diffmatchpatch.go index d3acc32..8d91b4a 100644 --- a/diffmatchpatch/diffmatchpatch.go +++ b/diffmatchpatch/diffmatchpatch.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/match.go b/diffmatchpatch/match.go index 17374e1..c73e86a 100644 --- a/diffmatchpatch/match.go +++ b/diffmatchpatch/match.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/match_test.go b/diffmatchpatch/match_test.go index f9abe60..5c9ee84 100644 --- a/diffmatchpatch/match_test.go +++ b/diffmatchpatch/match_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/mathutil.go b/diffmatchpatch/mathutil.go index aed242b..8e89c85 100644 --- a/diffmatchpatch/mathutil.go +++ b/diffmatchpatch/mathutil.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/patch.go b/diffmatchpatch/patch.go index 0dbe3bd..4abc3f9 100644 --- a/diffmatchpatch/patch.go +++ b/diffmatchpatch/patch.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/patch_test.go b/diffmatchpatch/patch_test.go index b019f88..0b74edb 100644 --- a/diffmatchpatch/patch_test.go +++ b/diffmatchpatch/patch_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/stringutil.go b/diffmatchpatch/stringutil.go index eb727bb..b131ba7 100644 --- a/diffmatchpatch/stringutil.go +++ b/diffmatchpatch/stringutil.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/diffmatchpatch/stringutil_test.go b/diffmatchpatch/stringutil_test.go index 73ab6ca..4ebcde7 100644 --- a/diffmatchpatch/stringutil_test.go +++ b/diffmatchpatch/stringutil_test.go @@ -1,5 +1,5 @@ // Copyright (c) 2012-2016 The go-diff authors. All rights reserved. -// https://github.com/sergi/go-diff +// https://github.com/cwarden/go-diff // See the included LICENSE file for license details. // // go-diff is a Go implementation of Google's Diff, Match, and Patch library diff --git a/go.mod b/go.mod index c7886ce..66d2a55 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/sergi/go-diff +module github.com/cwarden/go-diff require ( github.com/davecgh/go-spew v1.1.1 // indirect