github.com/richardwilkes/toolbox@v1.121.0/txt/slices.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 "strings" 14 15 "github.com/richardwilkes/toolbox/collection/dict" 16 ) 17 18 // StringSliceToMap returns a map created from the strings of a slice. 19 func StringSliceToMap(slice []string) map[string]bool { 20 m := make(map[string]bool, len(slice)) 21 for _, str := range slice { 22 m[str] = true 23 } 24 return m 25 } 26 27 // MapToStringSlice returns a slice created from the keys of a map. 28 // 29 // Deprecated: Use dict.Keys instead. This function was deprecated on May 3, 2024 and will be removed on or after 30 // January 1, 2025. 31 func MapToStringSlice(m map[string]bool) []string { 32 return dict.Keys(m) 33 } 34 35 // CloneStringSlice returns a copy of the slice of strings. 36 func CloneStringSlice(in []string) []string { 37 if len(in) == 0 { 38 return nil 39 } 40 out := make([]string, len(in)) 41 copy(out, in) 42 return out 43 } 44 45 // RunesEqual returns true if the two slices of runes are equal. 46 func RunesEqual(left, right []rune) bool { 47 if len(left) != len(right) { 48 return false 49 } 50 for i := range left { 51 if left[i] != right[i] { 52 return false 53 } 54 } 55 return true 56 } 57 58 // CaselessSliceContains returns true if the target is within the slice, regardless of case. 59 func CaselessSliceContains(slice []string, target string) bool { 60 for _, one := range slice { 61 if strings.EqualFold(one, target) { 62 return true 63 } 64 } 65 return false 66 }