github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/string.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package types 23 24 import ( 25 "context" 26 "errors" 27 "fmt" 28 "strconv" 29 30 "github.com/dolthub/dolt/go/store/hash" 31 ) 32 33 // String is a Noms Value wrapper around the primitive string type. 34 type String string 35 36 // Value interface 37 func (s String) Value(ctx context.Context) (Value, error) { 38 return s, nil 39 } 40 41 func (s String) Equals(other Value) bool { 42 return s == other 43 } 44 45 func (s String) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { 46 if s2, ok := other.(String); ok { 47 return s < s2, nil 48 } 49 return StringKind < other.Kind(), nil 50 } 51 52 func (s String) Hash(nbf *NomsBinFormat) (hash.Hash, error) { 53 return getHash(s, nbf) 54 } 55 56 func (s String) isPrimitive() bool { 57 return true 58 } 59 60 func (s String) WalkValues(ctx context.Context, cb ValueCallback) error { 61 return nil 62 } 63 64 func (s String) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { 65 return nil 66 } 67 68 func (s String) typeOf() (*Type, error) { 69 return PrimitiveTypeMap[StringKind], nil 70 } 71 72 func (s String) Kind() NomsKind { 73 return StringKind 74 } 75 76 func (s String) valueReadWriter() ValueReadWriter { 77 return nil 78 } 79 80 func (s String) writeTo(w nomsWriter, nbf *NomsBinFormat) error { 81 err := StringKind.writeTo(w, nbf) 82 83 if err != nil { 84 return err 85 } 86 87 w.writeString(string(s)) 88 89 return nil 90 } 91 92 func (s String) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) { 93 return String(b.ReadString()), nil 94 } 95 96 func (s String) skip(nbf *NomsBinFormat, b *binaryNomsReader) { 97 b.skipString() 98 } 99 100 func parseNumber(s String) (isNegative bool, decPos int, err error) { 101 decPos = -1 102 for i, c := range s { 103 if i == 0 && c == '-' { 104 isNegative = true 105 } else if c == '.' { 106 if decPos != -1 { 107 return false, -1, errors.New("not a valid number. multiple decimal points found.") 108 } 109 110 decPos = i 111 } else if c > '9' || c < '0' { 112 return false, -1, fmt.Errorf("for the string '%s' found invalid character '%s' at pos %d", s, string(c), i) 113 } 114 } 115 116 return isNegative, decPos, nil 117 } 118 119 func (s String) HumanReadableString() string { 120 return strconv.Quote(string(s)) 121 }