MCPcopy Create free account
hub / github.com/sergi/go-diff / diffCleanupSemanticScore

Function diffCleanupSemanticScore

diffmatchpatch/diff.go:749–787  ·  view source on GitHub ↗

diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries. Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables.

(one, two string)

Source from the content-addressed store, hash-verified

747// diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries.
748// Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables.
749func diffCleanupSemanticScore(one, two string) int {
750 if len(one) == 0 || len(two) == 0 {
751 // Edges are the best.
752 return 6
753 }
754
755 // 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.
756 rune1, _ := utf8.DecodeLastRuneInString(one)
757 rune2, _ := utf8.DecodeRuneInString(two)
758 char1 := string(rune1)
759 char2 := string(rune2)
760
761 nonAlphaNumeric1 := nonAlphaNumericRegex.MatchString(char1)
762 nonAlphaNumeric2 := nonAlphaNumericRegex.MatchString(char2)
763 whitespace1 := nonAlphaNumeric1 && whitespaceRegex.MatchString(char1)
764 whitespace2 := nonAlphaNumeric2 && whitespaceRegex.MatchString(char2)
765 lineBreak1 := whitespace1 && linebreakRegex.MatchString(char1)
766 lineBreak2 := whitespace2 && linebreakRegex.MatchString(char2)
767 blankLine1 := lineBreak1 && blanklineEndRegex.MatchString(one)
768 blankLine2 := lineBreak2 && blanklineStartRegex.MatchString(two)
769
770 if blankLine1 || blankLine2 {
771 // Five points for blank lines.
772 return 5
773 } else if lineBreak1 || lineBreak2 {
774 // Four points for line breaks.
775 return 4
776 } else if nonAlphaNumeric1 && !whitespace1 && whitespace2 {
777 // Three points for end of sentences.
778 return 3
779 } else if whitespace1 || whitespace2 {
780 // Two points for whitespace.
781 return 2
782 } else if nonAlphaNumeric1 || nonAlphaNumeric2 {
783 // One point for non-alphanumeric.
784 return 1
785 }
786 return 0
787}
788
789// DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.
790// E.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.

Callers 2

Calls

no outgoing calls

Tested by 1