github.com/google/osv-scalibr@v0.4.1/semantic/utilities.go (about)

     1  // Copyright 2025 Google LLC
     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  
    15  package semantic
    16  
    17  import (
    18  	"fmt"
    19  	"math/big"
    20  )
    21  
    22  // convertToBigInt attempts to convert the given str to a big.Int,
    23  // returning an error if the conversion fails
    24  func convertToBigInt(str string) (*big.Int, error) {
    25  	i, ok := new(big.Int).SetString(str, 10)
    26  
    27  	if !ok {
    28  		return nil, fmt.Errorf("%w: failed to convert %s to a number", ErrInvalidVersion, str)
    29  	}
    30  
    31  	return i, nil
    32  }
    33  
    34  func fetch(slice []string, i int, def string) string {
    35  	if len(slice) <= i {
    36  		return def
    37  	}
    38  
    39  	return slice[i]
    40  }
    41  
    42  // isASCIIDigit returns true if the given rune is an ASCII digit.
    43  //
    44  // Unicode digits are not considered ASCII digits by this function.
    45  func isASCIIDigit(c rune) bool {
    46  	return c >= 48 && c <= 57
    47  }
    48  
    49  // isASCIILetter returns true if the given rune is an ASCII letter.
    50  //
    51  // Unicode letters are not considered ASCII letters by this function.
    52  func isASCIILetter(c rune) bool {
    53  	return (c >= 65 && c <= 90) || (c >= 97 && c <= 122)
    54  }