| // Copyright 2021 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package codifier |
| |
| import ( |
| "log" |
| "strings" |
| ) |
| |
| // Change replaces all copies of old with new in p.replacement. |
| func (p *Proc) Change(old, new string) *Proc { |
| if p == nil { |
| log.Println(" Change() error: nil receiver") |
| return nil |
| } |
| p.replacement = strings.Replace(p.replacement, old, new, -1) |
| return p |
| } |
| |
| // Delete removes the current scope by erasing p.replacement. applyChange() must |
| // be called to effect the change in the parent. |
| func (p *Proc) Delete() *Proc { |
| if p == nil { |
| log.Println(" Delete() error: nil receiver") |
| return nil |
| } |
| p.replacement = "" |
| return p |
| } |
| |
| // Append adds the given string to the end of p.replacement. |
| func (p *Proc) Append(addition string) *Proc { |
| if p == nil { |
| log.Println(" Append() error: nil receiver") |
| return nil |
| } |
| p.replacement += addition |
| return p |
| } |
| |
| // AddIfNeeded adds the given string if the key is present. |
| func (p *Proc) AddIfNeeded(key, location, addition string) *Proc { |
| if p == nil { |
| log.Println(" AddIfNeeded() error: nil receiver") |
| return nil |
| } |
| rep, err := addIfNeeded(p.replacement, key, location, addition+"\n") |
| if err != nil { |
| log.Printf(" AddIfNeeded() error: %v", err) |
| return nil |
| } |
| p.replacement = rep |
| return p |
| } |
| |
| // RemoveIfNotNeeded removes the given string if the key is not present. |
| func (p *Proc) RemoveIfNotNeeded(key, removeStr string) *Proc { |
| if p == nil { |
| log.Println(" RemoveIfNotNeeded() error: nil receiver") |
| return nil |
| } |
| rep, err := removeIfNotNeeded(p.replacement, key, removeStr+"\n") |
| if err != nil { |
| log.Printf(" RemoveIfNotNeeded() error: %v", err) |
| return nil |
| } |
| p.replacement = rep |
| return p |
| } |
| |
| // Replace sets p.replacement to the given string. |
| func (p *Proc) Replace(replacement string) *Proc { |
| if p == nil { |
| log.Println(" Replace() error: nil receiver") |
| return nil |
| } |
| p.replacement = replacement |
| return p |
| } |