mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-09 13:45:26 +02:00
Start work on porting hints kitten to Go
This commit is contained in:
@@ -25,24 +25,23 @@ func MustCompile(pat string) *regexp.Regexp {
|
||||
return pat_cache.MustGetOrCreate(pat, regexp.MustCompile)
|
||||
}
|
||||
|
||||
func ReplaceAll(pat, str string, repl func(full_match string, groupdict map[string]SubMatch) string) string {
|
||||
cpat := MustCompile(pat)
|
||||
func ReplaceAll(cpat *regexp.Regexp, str string, repl func(full_match string, groupdict map[string]SubMatch) string) string {
|
||||
result := strings.Builder{}
|
||||
result.Grow(len(str) + 256)
|
||||
last_index := 0
|
||||
matches := cpat.FindAllStringSubmatchIndex(str, -1)
|
||||
names := cpat.SubexpNames()
|
||||
groupdict := make(map[string]SubMatch, len(names))
|
||||
for _, v := range matches {
|
||||
match_start, match_end := v[0], v[1]
|
||||
full_match := str[match_start:match_end]
|
||||
groupdict := make(map[string]SubMatch, len(names))
|
||||
for k := range groupdict {
|
||||
delete(groupdict, k)
|
||||
}
|
||||
for i, name := range names {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
idx := 2 * i
|
||||
if v[idx] > -1 && v[idx+1] > -1 {
|
||||
groupdict[name] = SubMatch{Text: str[v[idx]:v[idx+1]], Start: v[idx] - match_start, End: v[idx+1] - match_start}
|
||||
groupdict[name] = SubMatch{Text: str[v[idx]:v[idx+1]], Start: v[idx], End: v[idx+1]}
|
||||
}
|
||||
}
|
||||
result.WriteString(str[last_index:match_start])
|
||||
|
||||
@@ -55,6 +55,10 @@ func (self *Set[T]) Iterable() map[T]struct{} {
|
||||
return self.items
|
||||
}
|
||||
|
||||
func (self *Set[T]) AsSlice() []T {
|
||||
return maps.Keys(self.items)
|
||||
}
|
||||
|
||||
func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {
|
||||
if self.Len() < other.Len() {
|
||||
ans = NewSet[T](self.Len())
|
||||
|
||||
Reference in New Issue
Block a user