github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/ranges/read_array.go (about) 1 package ranges 2 3 import ( 4 "bytes" 5 6 "github.com/lmorg/murex/lang" 7 "github.com/lmorg/murex/utils/rmbs" 8 ) 9 10 type rangeParameters struct { 11 Exclude bool 12 RmBS bool 13 StripBlank bool 14 TrimSpace bool 15 Buffer bool 16 Start string 17 End string 18 Match rangeFuncs 19 } 20 21 type rangeFuncs interface { 22 Start([]byte) bool 23 End([]byte) bool 24 SetLength(int) 25 } 26 27 func readArray(p *lang.Process, r *rangeParameters, dt string) error { 28 var ( 29 nestedErr error 30 started bool 31 stdin = p.Stdin 32 length int 33 ) 34 35 if r.Start == "" { 36 started = true 37 } 38 39 array, err := p.Stdout.WriteArray(dt) 40 if err != nil { 41 return err 42 } 43 44 if r.Buffer { 45 stdin, length, err = buffer(p, dt) 46 if err != nil { 47 return err 48 } 49 r.Match.SetLength(length) 50 } 51 52 //noBang := !p.IsNot 53 54 write := func(b []byte) { 55 nestedErr = array.Write(b) 56 if nestedErr != nil { 57 p.Done() 58 } 59 } 60 61 err = stdin.ReadArray(p.Context, func(b []byte) { 62 if r.RmBS { 63 b = []byte(rmbs.Remove(string(b))) 64 } 65 66 if r.TrimSpace { 67 b = bytes.TrimSpace(b) 68 } 69 70 if r.StripBlank && len(b) == 0 { 71 return 72 } 73 74 if !started { 75 if r.Match.Start(b) { 76 started = true 77 if r.Exclude { 78 return 79 } 80 81 } else { 82 return 83 } 84 } 85 86 if r.End != "" && r.Match.End(b) { 87 if !r.Exclude { 88 write(b) 89 } 90 p.Done() 91 return 92 } 93 94 if p.IsNot { 95 return 96 } 97 write(b) 98 }) 99 100 if nestedErr != nil { 101 return nestedErr 102 } 103 104 if err != nil { 105 return err 106 } 107 108 return array.Close() 109 }