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 17import ( 18 "fmt" 19 "io" 20 "math" 21 "sort" 22) 23 24func AddStringToList(list *List, s string) (modified bool) { 25 for _, v := range list.Values { 26 if sv, ok := v.(*String); ok && sv.Value == s { 27 // string already exists 28 return false 29 } else if !ok { 30 panic(fmt.Errorf("expected string in list, got %s", v.Type())) 31 } 32 } 33 34 list.Values = append(list.Values, &String{ 35 LiteralPos: list.RBracePos, 36 Value: s, 37 }) 38 39 return true 40} 41 42func RemoveStringFromList(list *List, s string) (modified bool) { 43 for i, v := range list.Values { 44 if sv, ok := v.(*String); ok && sv.Value == s { 45 list.Values = append(list.Values[:i], list.Values[i+1:]...) 46 return true 47 } else if !ok { 48 panic(fmt.Errorf("expected string in list, got %s", v.Type())) 49 } 50 } 51 52 return false 53} 54 55func ReplaceStringsInList(list *List, replacements map[string]string) (replaced bool) { 56 modified := false 57 for i, v := range list.Values { 58 if sv, ok := v.(*String); ok && replacements[sv.Value] != "" { 59 pos := list.Values[i].Pos() 60 list.Values[i] = &String{ 61 LiteralPos: pos, 62 Value: replacements[sv.Value], 63 } 64 modified = true 65 } else if !ok { 66 panic(fmt.Errorf("expected string in list, got %s", v.Type())) 67 } 68 } 69 return modified 70} 71 72// A Patch represents a region of a text buffer to be replaced [Start, End) and its Replacement 73type Patch struct { 74 Start, End int 75 Replacement string 76} 77 78// A PatchList is a list of sorted, non-overlapping Patch objects 79type PatchList []Patch 80 81type PatchOverlapError error 82 83// Add adds a Patch to a PatchList. It returns a PatchOverlapError if the patch cannot be added. 84func (list *PatchList) Add(start, end int, replacement string) error { 85 patch := Patch{start, end, replacement} 86 if patch.Start > patch.End { 87 return fmt.Errorf("invalid patch, start %d is after end %d", patch.Start, patch.End) 88 } 89 for _, p := range *list { 90 if (patch.Start >= p.Start && patch.Start < p.End) || 91 (patch.End >= p.Start && patch.End < p.End) || 92 (p.Start >= patch.Start && p.Start < patch.End) || 93 (p.Start == patch.Start && p.End == patch.End) { 94 return PatchOverlapError(fmt.Errorf("new patch %d-%d overlaps with existing patch %d-%d", 95 patch.Start, patch.End, p.Start, p.End)) 96 } 97 } 98 *list = append(*list, patch) 99 list.sort() 100 return nil 101} 102 103func (list *PatchList) sort() { 104 sort.SliceStable(*list, 105 func(i, j int) bool { 106 return (*list)[i].Start < (*list)[j].Start 107 }) 108} 109 110// Apply applies all the Patch objects in PatchList to the data from an input ReaderAt to an output Writer. 111func (list *PatchList) Apply(in io.ReaderAt, out io.Writer) error { 112 var offset int64 113 for _, patch := range *list { 114 toWrite := int64(patch.Start) - offset 115 written, err := io.Copy(out, io.NewSectionReader(in, offset, toWrite)) 116 if err != nil { 117 return err 118 } 119 offset += toWrite 120 if written != toWrite { 121 return fmt.Errorf("unexpected EOF at %d", offset) 122 } 123 124 _, err = io.WriteString(out, patch.Replacement) 125 if err != nil { 126 return err 127 } 128 129 offset += int64(patch.End - patch.Start) 130 } 131 _, err := io.Copy(out, io.NewSectionReader(in, offset, math.MaxInt64-offset)) 132 return err 133} 134