]> Cypherpunks repositories - gostls13.git/commitdiff
os: reimplement HasPrefix and LastIndex to not depend on strings
authorDavid du Colombier <0intro@gmail.com>
Sat, 21 Dec 2013 00:22:10 +0000 (01:22 +0100)
committerDavid du Colombier <0intro@gmail.com>
Sat, 21 Dec 2013 00:22:10 +0000 (01:22 +0100)
R=golang-codereviews, rsc
CC=golang-codereviews, jas
https://golang.org/cl/44790043

src/pkg/os/file_plan9.go

index 102ad5f8929ae315e1e1a6a89bed11fa4eff25a9..e6496558ca5ed95253e9e5912f92f77627eb5e1c 100644 (file)
@@ -6,7 +6,6 @@ package os
 
 import (
        "runtime"
-       "strings"
        "syscall"
        "time"
 )
@@ -314,9 +313,24 @@ func Remove(name string) error {
        return nil
 }
 
+// HasPrefix from the strings package.
+func hasPrefix(s, prefix string) bool {
+       return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
+}
+
+// Variant of LastIndex from the strings package.
+func lastIndex(s string, sep byte) int {
+       for i := len(s) - 1; i >= 0; i-- {
+               if s[i] == sep {
+                       return i
+               }
+       }
+       return -1
+}
+
 func rename(oldname, newname string) error {
-       dirname := oldname[:strings.LastIndex(oldname, "/")+1]
-       if strings.HasPrefix(newname, dirname) {
+       dirname := oldname[:lastIndex(oldname, '/')+1]
+       if hasPrefix(newname, dirname) {
                newname = newname[len(dirname):]
        }