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

Function diffCleanupSemanticScore

diffmatchpatch/diff.go:732–770  ·  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

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.
732func 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 c<ins>at c</ins>ame. -> The <ins>cat </ins>came.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…