1 /* 2 * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * @author Aeneas Rekkas <aeneas+oss@aeneas.io> 17 * @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 18 * @license Apache-2.0 19 * 20 */ 21 22 package fosite 23 24 import ( 25 "fmt" 26 "strings" 27 ) 28 29 // StringInSlice returns true if needle exists in haystack 30 func StringInSlice(needle string, haystack []string) bool { 31 for _, b := range haystack { 32 if strings.ToLower(b) == strings.ToLower(needle) { 33 return true 34 } 35 } 36 return false 37 } 38 39 func RemoveEmpty(args []string) (ret []string) { 40 for _, v := range args { 41 v = strings.TrimSpace(v) 42 if v != "" { 43 ret = append(ret, v) 44 } 45 } 46 return 47 } 48 49 // EscapeJSONString does a poor man's JSON encoding. Useful when we do not want to use full JSON encoding 50 // because we just had an error doing the JSON encoding. The characters that MUST be escaped: quotation mark, 51 // reverse solidus, and the control characters (U+0000 through U+001F). 52 // See: https://tools.ietf.org/html/std90#section-7 53 func EscapeJSONString(str string) string { 54 // Escape reverse solidus. 55 str = strings.ReplaceAll(str, `\`, `\\`) 56 // Escape control characters. 57 for r := rune(0); r < ' '; r++ { 58 str = strings.ReplaceAll(str, string(r), fmt.Sprintf(`\u%04x`, r)) 59 } 60 // Escape quotation mark. 61 str = strings.ReplaceAll(str, `"`, `\"`) 62 return str 63 } 64