Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Transfer and use isPathPrefixOrEqual for checking if string is matchi…
Browse files Browse the repository at this point in the history
…ng path tree
  • Loading branch information
Rhymond committed Mar 20, 2017
1 parent 20fcfef commit 5427779
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cmd/dep/graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package main

import (
"hash/fnv"
"strings"
"bytes"
"fmt"
"hash/fnv"
"strings"
)

type graphviz struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (g graphviz) output() bytes.Buffer {
for _, dp := range g.ps {
for _, bsc := range dp.children {
for pr, hsh := range g.h {
if strings.HasPrefix(bsc, pr) {
if strings.HasPrefix(bsc, pr) && isPathPrefixOrEqual(pr, bsc) {
r := fmt.Sprintf("%d -> %d", g.h[dp.project], hsh)

if _, ex := rels[r]; !ex {
Expand Down Expand Up @@ -91,3 +91,24 @@ func (dp gvnode) label() string {

return strings.Join(label, "\n")
}

// Ensure that the literal string prefix is a path tree match and
// guard against possibilities like this:
//
// github.com/sdboyer/foo
// github.com/sdboyer/foobar/baz
//
// Verify that either the input is the same length as the match (in which
// case we know they're equal), or that the next character is a "/". (Import
// paths are defined to always use "/", not the OS-specific path separator.)
func isPathPrefixOrEqual(pre, path string) bool {
prflen, pathlen := len(pre), len(path)
if pathlen == prflen+1 {
// this can never be the case
return false
}

// we assume something else (a trie) has done equality check up to the point
// of the prefix, so we just check len
return prflen == pathlen || strings.Index(path[prflen:], "/") == 0
}

0 comments on commit 5427779

Please sign in to comment.