From 88b94b686d6ce32fded93be7d30a20a23e34fcc3 Mon Sep 17 00:00:00 2001 From: Brian Gitonga Marete Date: Sat, 15 Mar 2014 07:39:08 +0300 Subject: [PATCH] pkg/schema: Move two non-utf8-handling utility funcs to schema.go. As per review comments. Change-Id: I0240f697d0cb180a9fb2e9b1ddda7ac18869b4ad --- pkg/schema/blob.go | 35 ----------------------------------- pkg/schema/schema.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/pkg/schema/blob.go b/pkg/schema/blob.go index b9dfb6eeb..3946f371b 100644 --- a/pkg/schema/blob.go +++ b/pkg/schema/blob.go @@ -438,41 +438,6 @@ func (bb *Builder) SetSymlinkTarget(target string) *Builder { return bb } -func mixedArrayFromString(s string) []interface{} { - buf := []byte(s) - var name []interface{} - n := 0 - for n < len(buf) { - part, offset := nextStringOrByte(buf[n:]) - name = append(name, part) - n += offset - } - - return name -} - -func nextStringOrByte(b []byte) (interface{}, int) { - n := 0 - var s []byte - for n < len(b) { - r, size := utf8.DecodeRune(b[n:]) - if r == utf8.RuneError { - // If we already have a UTF8 string segment, return it - if len(s) > 0 { - return string(s), n - } - // Return the single byte and an offset of 1 - return b[n], 1 - } - n += size // We have consumed size bytes - c := make([]byte, utf8.RuneLen(r)) - _ = utf8.EncodeRune(c, r) - s = append(s, c...) - } - - return string(s), n -} - // IsClaimType returns whether this blob builder is for a type // which should be signed. (a "claim" or "permanode") func (bb *Builder) IsClaimType() bool { diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 7f121b48e..4dc938762 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -36,6 +36,7 @@ import ( "strings" "sync" "time" + "unicode/utf8" "camlistore.org/pkg/blob" "camlistore.org/pkg/types" @@ -348,6 +349,41 @@ func stringFromMixedArray(parts []interface{}) string { return buf.String() } +func mixedArrayFromString(s string) []interface{} { + buf := []byte(s) + var name []interface{} + n := 0 + for n < len(buf) { + part, offset := nextStringOrByte(buf[n:]) + name = append(name, part) + n += offset + } + + return name +} + +func nextStringOrByte(b []byte) (interface{}, int) { + n := 0 + var s []byte + for n < len(b) { + r, size := utf8.DecodeRune(b[n:]) + if r == utf8.RuneError { + // If we already have a UTF8 string segment, return it + if len(s) > 0 { + return string(s), n + } + // Return the single byte and an offset of 1 + return b[n], 1 + } + n += size // We have consumed size bytes + c := make([]byte, utf8.RuneLen(r)) + _ = utf8.EncodeRune(c, r) + s = append(s, c...) + } + + return string(s), n +} + func (ss *superset) SumPartsSize() (size uint64) { for _, part := range ss.Parts { size += uint64(part.Size)