github.com/blend/go-sdk@v1.20220411.3/diff/text.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package diff
     9  
    10  import "bytes"
    11  
    12  // Text converts a []Diff into a text report.
    13  func Text(diffs []Diff) string {
    14  	var buff bytes.Buffer
    15  	for _, diff := range diffs {
    16  		text := diff.Text
    17  
    18  		switch diff.Type {
    19  		case DiffInsert:
    20  			_, _ = buff.WriteString("+")
    21  			_, _ = buff.WriteString(text)
    22  		case DiffDelete:
    23  			_, _ = buff.WriteString("-")
    24  			_, _ = buff.WriteString(text)
    25  		}
    26  	}
    27  	return buff.String()
    28  }
    29  
    30  // Text1 computes and returns the source text (all equalities and deletions).
    31  func Text1(diffs []Diff) string {
    32  	//StringBuilder text = new StringBuilder()
    33  	var text bytes.Buffer
    34  
    35  	for _, aDiff := range diffs {
    36  		if aDiff.Type != DiffInsert {
    37  			_, _ = text.WriteString(aDiff.Text)
    38  		}
    39  	}
    40  	return text.String()
    41  }
    42  
    43  // Text2 computes and returns the destination text (all equalities and insertions).
    44  func Text2(diffs []Diff) string {
    45  	var text bytes.Buffer
    46  
    47  	for _, aDiff := range diffs {
    48  		if aDiff.Type != DiffDelete {
    49  			_, _ = text.WriteString(aDiff.Text)
    50  		}
    51  	}
    52  	return text.String()
    53  }