Path: blob/main/vendor/github.com/chzyer/readline/runes.go
2875 views
package readline12import (3"bytes"4"unicode"5"unicode/utf8"6)78var runes = Runes{}9var TabWidth = 41011type Runes struct{}1213func (Runes) EqualRune(a, b rune, fold bool) bool {14if a == b {15return true16}17if !fold {18return false19}20if a > b {21a, b = b, a22}23if b < utf8.RuneSelf && 'A' <= a && a <= 'Z' {24if b == a+'a'-'A' {25return true26}27}28return false29}3031func (r Runes) EqualRuneFold(a, b rune) bool {32return r.EqualRune(a, b, true)33}3435func (r Runes) EqualFold(a, b []rune) bool {36if len(a) != len(b) {37return false38}39for i := 0; i < len(a); i++ {40if r.EqualRuneFold(a[i], b[i]) {41continue42}43return false44}4546return true47}4849func (Runes) Equal(a, b []rune) bool {50if len(a) != len(b) {51return false52}53for i := 0; i < len(a); i++ {54if a[i] != b[i] {55return false56}57}58return true59}6061func (rs Runes) IndexAllBckEx(r, sub []rune, fold bool) int {62for i := len(r) - len(sub); i >= 0; i-- {63found := true64for j := 0; j < len(sub); j++ {65if !rs.EqualRune(r[i+j], sub[j], fold) {66found = false67break68}69}70if found {71return i72}73}74return -175}7677// Search in runes from end to front78func (rs Runes) IndexAllBck(r, sub []rune) int {79return rs.IndexAllBckEx(r, sub, false)80}8182// Search in runes from front to end83func (rs Runes) IndexAll(r, sub []rune) int {84return rs.IndexAllEx(r, sub, false)85}8687func (rs Runes) IndexAllEx(r, sub []rune, fold bool) int {88for i := 0; i < len(r); i++ {89found := true90if len(r[i:]) < len(sub) {91return -192}93for j := 0; j < len(sub); j++ {94if !rs.EqualRune(r[i+j], sub[j], fold) {95found = false96break97}98}99if found {100return i101}102}103return -1104}105106func (Runes) Index(r rune, rs []rune) int {107for i := 0; i < len(rs); i++ {108if rs[i] == r {109return i110}111}112return -1113}114115func (Runes) ColorFilter(r []rune) []rune {116newr := make([]rune, 0, len(r))117for pos := 0; pos < len(r); pos++ {118if r[pos] == '\033' && r[pos+1] == '[' {119idx := runes.Index('m', r[pos+2:])120if idx == -1 {121continue122}123pos += idx + 2124continue125}126newr = append(newr, r[pos])127}128return newr129}130131var zeroWidth = []*unicode.RangeTable{132unicode.Mn,133unicode.Me,134unicode.Cc,135unicode.Cf,136}137138var doubleWidth = []*unicode.RangeTable{139unicode.Han,140unicode.Hangul,141unicode.Hiragana,142unicode.Katakana,143}144145func (Runes) Width(r rune) int {146if r == '\t' {147return TabWidth148}149if unicode.IsOneOf(zeroWidth, r) {150return 0151}152if unicode.IsOneOf(doubleWidth, r) {153return 2154}155return 1156}157158func (Runes) WidthAll(r []rune) (length int) {159for i := 0; i < len(r); i++ {160length += runes.Width(r[i])161}162return163}164165func (Runes) Backspace(r []rune) []byte {166return bytes.Repeat([]byte{'\b'}, runes.WidthAll(r))167}168169func (Runes) Copy(r []rune) []rune {170n := make([]rune, len(r))171copy(n, r)172return n173}174175func (Runes) HasPrefixFold(r, prefix []rune) bool {176if len(r) < len(prefix) {177return false178}179return runes.EqualFold(r[:len(prefix)], prefix)180}181182func (Runes) HasPrefix(r, prefix []rune) bool {183if len(r) < len(prefix) {184return false185}186return runes.Equal(r[:len(prefix)], prefix)187}188189func (Runes) Aggregate(candicate [][]rune) (same []rune, size int) {190for i := 0; i < len(candicate[0]); i++ {191for j := 0; j < len(candicate)-1; j++ {192if i >= len(candicate[j]) || i >= len(candicate[j+1]) {193goto aggregate194}195if candicate[j][i] != candicate[j+1][i] {196goto aggregate197}198}199size = i + 1200}201aggregate:202if size > 0 {203same = runes.Copy(candicate[0][:size])204for i := 0; i < len(candicate); i++ {205n := runes.Copy(candicate[i])206copy(n, n[size:])207candicate[i] = n[:len(n)-size]208}209}210return211}212213func (Runes) TrimSpaceLeft(in []rune) []rune {214firstIndex := len(in)215for i, r := range in {216if unicode.IsSpace(r) == false {217firstIndex = i218break219}220}221return in[firstIndex:]222}223224225