snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
This commit is contained in:
56
modules/util/trie.go
Normal file
56
modules/util/trie.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
type trieEdge struct {
|
||||
b byte
|
||||
node *TrieNode
|
||||
}
|
||||
|
||||
// TrieNode represents a node in a slice-based Trie for fast byte-sequence prefix matching.
|
||||
type TrieNode struct {
|
||||
children []trieEdge
|
||||
isEnd bool
|
||||
}
|
||||
|
||||
func (t *TrieNode) child(b byte) *TrieNode {
|
||||
for _, edge := range t.children {
|
||||
if edge.b == b {
|
||||
return edge.node
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert adds a string (byte sequence) to the Trie.
|
||||
func (t *TrieNode) Insert(val string) {
|
||||
curr := t
|
||||
for i := 0; i < len(val); i++ {
|
||||
next := curr.child(val[i])
|
||||
if next == nil {
|
||||
next = &TrieNode{}
|
||||
curr.children = append(curr.children, trieEdge{b: val[i], node: next})
|
||||
}
|
||||
curr = next
|
||||
}
|
||||
curr.isEnd = true
|
||||
}
|
||||
|
||||
// Match returns the length of the longest matching prefix starting at index `start` in string `s`.
|
||||
// It returns -1 if no prefix is matched.
|
||||
func (t *TrieNode) Match(s string, start int) int {
|
||||
curr := t
|
||||
matchLen := -1
|
||||
for j := start; j < len(s); j++ {
|
||||
next := curr.child(s[j])
|
||||
if next == nil {
|
||||
break
|
||||
}
|
||||
curr = next
|
||||
if curr.isEnd {
|
||||
matchLen = j - start + 1
|
||||
}
|
||||
}
|
||||
return matchLen
|
||||
}
|
||||
Reference in New Issue
Block a user