github.com/google/osv-scalibr@v0.4.1/semantic/version-semver-like.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  	"strings"
    21  )
    22  
    23  // semverLikeVersion is a version that is _like_ a version as defined by the
    24  // Semantic Version specification, except with potentially unlimited numeric
    25  // components and a leading "v"
    26  type semverLikeVersion struct {
    27  	LeadingV   bool
    28  	Components components
    29  	Build      string
    30  	Original   string
    31  }
    32  
    33  func (v *semverLikeVersion) fetchComponentsAndBuild(maxComponents int) (components, string) {
    34  	if maxComponents == -1 || len(v.Components) <= maxComponents {
    35  		return v.Components, v.Build
    36  	}
    37  
    38  	comps := v.Components[:maxComponents]
    39  	extra := v.Components[maxComponents:]
    40  
    41  	var build strings.Builder
    42  
    43  	build.WriteString(v.Build)
    44  
    45  	for _, c := range extra {
    46  		build.WriteString(fmt.Sprintf(".%d", c))
    47  	}
    48  
    49  	return comps, build.String()
    50  }
    51  
    52  func parseSemverLikeVersion(line string, maxComponents int) semverLikeVersion {
    53  	v := parseSemverLike(line)
    54  
    55  	comps, build := v.fetchComponentsAndBuild(maxComponents)
    56  
    57  	return semverLikeVersion{
    58  		LeadingV:   v.LeadingV,
    59  		Components: comps,
    60  		Build:      build,
    61  		Original:   v.Original,
    62  	}
    63  }
    64  
    65  func parseSemverLike(line string) semverLikeVersion {
    66  	var comps []*big.Int
    67  	originStr := line
    68  
    69  	currentCom := ""
    70  	foundBuild := false
    71  
    72  	leadingV := strings.HasPrefix(line, "v")
    73  	line = strings.TrimPrefix(line, "v")
    74  
    75  	for _, c := range line {
    76  		if foundBuild {
    77  			currentCom += string(c)
    78  
    79  			continue
    80  		}
    81  
    82  		// this is part of a component version
    83  		if isASCIIDigit(c) {
    84  			currentCom += string(c)
    85  
    86  			continue
    87  		}
    88  
    89  		// at this point, we:
    90  		//   1. might be parsing a component (as foundBuild != true)
    91  		//   2. we're not looking at a part of a component (as c != number)
    92  		//
    93  		// so c must be either:
    94  		//   1. a component terminator (.), or
    95  		//   2. the start of the build string
    96  		//
    97  		// either way, we will be terminating the current component being
    98  		// parsed (if any), so let's do that first
    99  		if currentCom != "" {
   100  			v, _ := new(big.Int).SetString(currentCom, 10)
   101  
   102  			comps = append(comps, v)
   103  			currentCom = ""
   104  		}
   105  
   106  		// a component terminator means there might be another component
   107  		// afterwards, so don't start parsing the build string just yet
   108  		if c == '.' {
   109  			continue
   110  		}
   111  
   112  		// anything else is part of the build string
   113  		foundBuild = true
   114  		currentCom = string(c)
   115  	}
   116  
   117  	// if we looped over everything without finding a build string,
   118  	// then what we were currently parsing is actually a component
   119  	if !foundBuild && currentCom != "" {
   120  		v, _ := new(big.Int).SetString(currentCom, 10)
   121  
   122  		comps = append(comps, v)
   123  		currentCom = ""
   124  	}
   125  
   126  	return semverLikeVersion{
   127  		LeadingV:   leadingV,
   128  		Components: comps,
   129  		Build:      currentCom,
   130  		Original:   originStr,
   131  	}
   132  }