Skip to content

Commit

Permalink
embed: simplify the code
Browse files Browse the repository at this point in the history
Use stringslite and bytealg to simplify the code and to remove redundent
helper functions.

Change-Id: I665a8313d9b91201b147b255290058f162cf0894
GitHub-Last-Rev: e9e3beb
GitHub-Pull-Request: #67515
Reviewed-on: https://go-review.googlesource.com/c/go/+/586715
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
  • Loading branch information
apocelipes authored and gopherbot committed May 20, 2024
1 parent f6653e6 commit 183b6b4
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ package embed

import (
"errors"
"internal/bytealg"
"internal/stringslite"
"io"
"io/fs"
"time"
Expand Down Expand Up @@ -185,29 +187,14 @@ type FS struct {
// comment in the FS struct above. isDir reports whether the
// final trailing slash was present, indicating that name is a directory.
func split(name string) (dir, elem string, isDir bool) {
if name[len(name)-1] == '/' {
isDir = true
name = name[:len(name)-1]
}
i := len(name) - 1
for i >= 0 && name[i] != '/' {
i--
}
name, isDir = stringslite.CutSuffix(name, "/")
i := bytealg.LastIndexByteString(name, '/')
if i < 0 {
return ".", name, isDir
}
return name[:i], name[i+1:], isDir
}

// trimSlash trims a trailing slash from name, if present,
// returning the possibly shortened name.
func trimSlash(name string) string {
if len(name) > 0 && name[len(name)-1] == '/' {
return name[:len(name)-1]
}
return name
}

var (
_ fs.ReadDirFS = FS{}
_ fs.ReadFileFS = FS{}
Expand Down Expand Up @@ -274,7 +261,7 @@ func (f FS) lookup(name string) *file {
idir, ielem, _ := split(files[i].name)
return idir > dir || idir == dir && ielem >= elem
})
if i < len(files) && trimSlash(files[i].name) == name {
if i < len(files) && stringslite.TrimSuffix(files[i].name, "/") == name {
return &files[i]
}
return nil
Expand Down

0 comments on commit 183b6b4

Please sign in to comment.