go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/isolate/utils.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 isolate
    16  
    17  import (
    18  	"log"
    19  
    20  	"go.chromium.org/luci/common/errors"
    21  )
    22  
    23  func assert(condition bool, info ...any) {
    24  	if condition {
    25  		return
    26  	}
    27  	if len(info) == 0 {
    28  		log.Panic(errors.RenderStack(errors.New("assertion failed")))
    29  	} else if format, ok := info[0].(string); ok {
    30  		log.Panic(errors.RenderStack(errors.Reason("assertion failed: "+format, info[1:]...).Err()))
    31  	}
    32  }
    33  
    34  // uniqueMergeSortedStrings merges two sorted sets of string (as slices) and removes duplicates.
    35  func uniqueMergeSortedStrings(ls, rs []string) []string {
    36  	varSet := make([]string, len(ls)+len(rs))
    37  	for i := 0; ; i++ {
    38  		if len(ls) == 0 {
    39  			rs, ls = ls, rs
    40  		}
    41  		if len(rs) == 0 {
    42  			i += copy(varSet[i:], ls)
    43  			return varSet[:i]
    44  		}
    45  		assert(i < len(varSet))
    46  		assert(len(rs) > 0 && len(ls) > 0)
    47  		if ls[0] > rs[0] {
    48  			ls, rs = rs, ls
    49  		}
    50  		if ls[0] < rs[0] {
    51  			varSet[i] = ls[0]
    52  			ls = ls[1:]
    53  		} else {
    54  			varSet[i] = ls[0]
    55  			ls, rs = ls[1:], rs[1:]
    56  		}
    57  	}
    58  }