Commits

Anonymous committed db01780

be more careful about c strings and do it the 'right' way

  • Participants
  • Parent commits 0d7cbd3
  • Tags release.2011-02-27

Comments (0)

Files changed (3)

 
 func (kc *KCDB) Load(filename string) (err os.Error)
 
-func (kc *KCDB) MatchPrefix(prefix []byte, max int) (matches [][]byte, err os.Error)
+func (kc *KCDB) MatchPrefix(prefix string, max int) (matches [][]byte, err os.Error)
 
 func (kc *KCDB) MatchRegex(regex string, max int) (matches [][]byte, err os.Error)
 
 }
 
 func (kc *KCDB) Open(filename string, mode int) (err os.Error) {
-	name := (*C.char)(unsafe.Pointer(&[]byte(filename)[0]))
+	name := C.CString(filename)
+	defer C.free(unsafe.Pointer(name))
 	if C.kcdbopen(kc.db, name, C.uint32_t(mode)) == 0 {
 		err = kc.error()
 	}
 }
 
 func (kc *KCDB) Copy(filename string) (err os.Error) {
-	name := (*C.char)(unsafe.Pointer(&[]byte(filename)[0]))
+	name := C.CString(filename)
+	defer C.free(unsafe.Pointer(name))
 	if C.kcdbcopy(kc.db, name) == 0 {
 		err = kc.error()
 	}
 }
 
 func (kc *KCDB) Dump(filename string) (err os.Error) {
-	name := (*C.char)(unsafe.Pointer(&[]byte(filename)[0]))
+	name := C.CString(filename)
+	defer C.free(unsafe.Pointer(name))
 	if C.kcdbdumpsnap(kc.db, name) == 0 {
 		err = kc.error()
 	}
 	return
 }
 func (kc *KCDB) Load(filename string) (err os.Error) {
-	name := (*C.char)(unsafe.Pointer(&[]byte(filename)[0]))
+	name := C.CString(filename)
+	defer C.free(unsafe.Pointer(name))
 	if C.kcdbcopy(kc.db, name) == 0 {
 		err = kc.error()
 	}
 	return
 }
 
-func (kc *KCDB) MatchPrefix(prefix []byte, max int) (matches [][]byte, err os.Error) {
-	cprefix := (*C.char)(unsafe.Pointer(&prefix[0]))
+func (kc *KCDB) MatchPrefix(prefix string, max int) (matches [][]byte, err os.Error) {
+	cprefix := C.CString(prefix)
+	defer C.free(unsafe.Pointer(cprefix))
 	strarray := make([]*C.char, max)
 	count := C.kcdbmatchprefix(kc.db, cprefix, &strarray[0], C.size_t(max))
 	if count == -1 {
 }
 
 func (kc *KCDB) MatchRegex(regex string, max int) (matches [][]byte, err os.Error) {
-	cregex := (*C.char)(unsafe.Pointer(&[]byte(regex)[0]))
+	cregex := C.CString(regex)
+	defer C.free(unsafe.Pointer(cregex))
 	strarray := make([]*C.char, max)
 	count := C.kcdbmatchregex(kc.db, cregex, &strarray[0], C.size_t(max))
 	if count == -1 {
 	addKVP(t, kc, "hell", "world")
 	addKVP(t, kc, "1hello", "world")
 
-	matches, err := kc.MatchPrefix([]byte("he"), 10)
+	matches, err := kc.MatchPrefix("he", 10)
 	if err != nil {
 		t.Fatalf("MatchPrefix(he): %s", err)
 	}