1// Copyright 2014 Google Inc. All rights reserved. 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 15package parser 16 17func AddStringToList(value *Value, s string) (modified bool) { 18 if value.Type != List { 19 panic("expected list value, got " + value.Type.String()) 20 } 21 22 for _, v := range value.ListValue { 23 if v.Type != String { 24 panic("expected string in list, got " + value.Type.String()) 25 } 26 27 if v.StringValue == s { 28 // string already exists 29 return false 30 } 31 32 } 33 34 value.ListValue = append(value.ListValue, Value{ 35 Type: String, 36 Pos: value.EndPos, 37 StringValue: s, 38 }) 39 40 return true 41} 42 43func RemoveStringFromList(value *Value, s string) (modified bool) { 44 if value.Type != List { 45 panic("expected list value, got " + value.Type.String()) 46 } 47 48 for i, v := range value.ListValue { 49 if v.Type != String { 50 panic("expected string in list, got " + value.Type.String()) 51 } 52 53 if v.StringValue == s { 54 value.ListValue = append(value.ListValue[:i], value.ListValue[i+1:]...) 55 return true 56 } 57 58 } 59 60 return false 61} 62