github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/position.go (about)

     1  // Copyright 2016 Denormal Limited
     2  // Copyright 2023 Google LLC
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package gitignore
    17  
    18  import (
    19  	"fmt"
    20  )
    21  
    22  // Position represents the position of the .gitignore parser, and the position
    23  // of a .gitignore pattern within the parsed stream.
    24  type Position struct {
    25  	File   string
    26  	Line   int
    27  	Column int
    28  	Offset int
    29  }
    30  
    31  // String returns a string representation of the current position.
    32  func (p Position) String() string {
    33  	_prefix := ""
    34  	if p.File != "" {
    35  		_prefix = p.File + ": "
    36  	}
    37  
    38  	if p.Line == 0 {
    39  		return fmt.Sprintf("%s+%d", _prefix, p.Offset)
    40  	} else if p.Column == 0 {
    41  		return fmt.Sprintf("%s%d", _prefix, p.Line)
    42  	} else {
    43  		return fmt.Sprintf("%s%d:%d", _prefix, p.Line, p.Column)
    44  	}
    45  } // String()
    46  
    47  // Zero returns true if the Position represents the zero Position
    48  func (p Position) Zero() bool {
    49  	return p.Line+p.Column+p.Offset == 0
    50  } // Zero()