MCPcopy
hub / github.com/duke-git/lancet / Rotate

Function Rotate

strutil/string.go:705–729  ·  view source on GitHub ↗

Rotate rotates the string by the specified number of characters. Play: https://go.dev/play/p/Kf03iOeT5bd

(str string, shift int)

Source from the content-addressed store, hash-verified

703// Rotate rotates the string by the specified number of characters.
704// Play: https://go.dev/play/p/Kf03iOeT5bd
705func Rotate(str string, shift int) string {
706 if shift == 0 {
707 return str
708 }
709
710 runes := []rune(str)
711 length := len(runes)
712 if length == 0 {
713 return str
714 }
715
716 shift = shift % length
717
718 if shift < 0 {
719 shift = length + shift
720 }
721
722 var sb strings.Builder
723 sb.Grow(length)
724
725 sb.WriteString(string(runes[length-shift:]))
726 sb.WriteString(string(runes[:length-shift]))
727
728 return sb.String()
729}
730
731// TemplateReplace replaces the placeholders in the template string with the corresponding values in the data map.
732// The placeholders are enclosed in curly braces, e.g. {key}.

Callers 2

TestRotateFunction · 0.85
ExampleRotateFunction · 0.85

Calls 1

StringMethod · 0.65

Tested by 2

TestRotateFunction · 0.68
ExampleRotateFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…