github.com/richardwilkes/toolbox@v1.121.0/txt/unquote.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package txt 11 12 import ( 13 "unicode/utf8" 14 ) 15 16 // UnquoteBytes strips up to one set of surrounding double quotes from the bytes and returns them as a string. For a 17 // more capable version that supports different quoting types and unescaping, consider using strconv.Unquote(). 18 func UnquoteBytes(text []byte) []byte { 19 if len(text) > 1 { 20 if ch, _ := utf8.DecodeRune(text); ch == '"' { 21 if ch, _ = utf8.DecodeLastRune(text); ch == '"' { 22 text = text[1 : len(text)-1] 23 } 24 } 25 } 26 return text 27 } 28 29 // Unquote strips up to one set of surrounding double quotes from the bytes and returns them as a string. For a more 30 // capable version that supports different quoting types and unescaping, consider using strconv.Unquote(). 31 func Unquote(text string) string { 32 if len(text) > 1 { 33 if ch, _ := utf8.DecodeRuneInString(text); ch == '"' { 34 if ch, _ = utf8.DecodeLastRuneInString(text); ch == '"' { 35 text = text[1 : len(text)-1] 36 } 37 } 38 } 39 return text 40 }