Rotate rotates the string by the specified number of characters. Play: https://go.dev/play/p/Kf03iOeT5bd
(str string, shift int)
| 703 | // Rotate rotates the string by the specified number of characters. |
| 704 | // Play: https://go.dev/play/p/Kf03iOeT5bd |
| 705 | func 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}. |
searching dependent graphs…