github.com/vanus-labs/vanus/lib@v0.0.0-20231221070800-1334a7b9605e/json/parse/string.go (about) 1 // Copyright 2023 Linkall 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 package parse 16 17 import ( 18 // standard libraries. 19 "errors" 20 "io" 21 ) 22 23 var errInvalidString = errors.New("invalid string") 24 25 const hicc = 0x1F // highest control characters. 26 27 func ConsumeDoubleQuotedString(r io.ByteReader, w io.ByteWriter) error { 28 for { 29 c, err := r.ReadByte() 30 if err != nil { 31 return errInvalidString 32 } 33 34 switch c { 35 case '"': // double quotes, end of string 36 return nil 37 case '\\': // backslash 38 if err = ConsumeEscapedWithDoubleQuote(r, w); err != nil { 39 return errInvalidString 40 } 41 default: 42 if c <= hicc { // control characters 43 return errInvalidString 44 } 45 if err = w.WriteByte(c); err != nil { 46 return errInvalidString 47 } 48 } 49 } 50 } 51 52 func ConsumeSingleQuotedString(r io.ByteReader, w io.ByteWriter) error { 53 for { 54 c, err := r.ReadByte() 55 if err != nil { 56 return errInvalidString 57 } 58 59 switch c { 60 case '\'': // single quote, end of string 61 return nil 62 case '\\': // backslash 63 if err = ConsumeEscapedWithSingleQuote(r, w); err != nil { 64 return errInvalidString 65 } 66 default: 67 if c <= hicc { // control characters 68 return errInvalidString 69 } 70 if err = w.WriteByte(c); err != nil { 71 return errInvalidString 72 } 73 } 74 } 75 }