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

Function alignRuneStart

diffmatchpatch/patch.go:37–45  ·  view source on GitHub ↗

alignRuneStart backs up byte index idx to the nearest UTF-8 rune start byte. idx <= 0 or idx >= len(s) is returned unchanged. Motivation: Google's reference JS diff-match-patch uses string.substring (UTF-16 code unit) for all slicing — surrogate-pair-middle splits are well-defined. This Go port sli

(s string, idx int)

Source from the content-addressed store, hash-verified

35// slice point but guarantees the resulting string is always valid UTF-8 and
36// no MatchMain / DiffMain pass sees a continuation-byte-prefixed string.
37func alignRuneStart(s string, idx int) int {
38 if idx <= 0 || idx >= len(s) {
39 return idx
40 }
41 for idx > 0 && !utf8.RuneStart(s[idx]) {
42 idx--
43 }
44 return idx
45}
46
47// alignRuneEnd moves byte index idx forward to the next UTF-8 rune start.
48// idx <= 0 or idx >= len(s) is returned unchanged.

Callers 5

PatchAddContextMethod · 0.85
PatchApplyMethod · 0.85
PatchSplitMaxMethod · 0.85
diffText1PrefixFunction · 0.85
TestAlignRuneStartFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestAlignRuneStartFunction · 0.68