gitlab.com/evatix-go/core@v1.3.55/regexnew/lazyRegexMap.go (about)

     1  package regexnew
     2  
     3  import "regexp"
     4  
     5  type lazyRegexMap struct {
     6  	items map[string]*LazyRegex
     7  }
     8  
     9  func (it *lazyRegexMap) IsEmpty() bool {
    10  	return it == nil || len(it.items) == 0
    11  }
    12  
    13  func (it *lazyRegexMap) IsEmptyLock() bool {
    14  	lazyRegexLock.Lock()
    15  	defer lazyRegexLock.Unlock()
    16  
    17  	return it == nil || len(it.items) == 0
    18  }
    19  
    20  func (it *lazyRegexMap) HasAnyItem() bool {
    21  	return it == nil || len(it.items) > 0
    22  }
    23  
    24  func (it *lazyRegexMap) HasAnyItemLock() bool {
    25  	lazyRegexLock.Lock()
    26  	defer lazyRegexLock.Unlock()
    27  
    28  	return it == nil || len(it.items) > 0
    29  }
    30  
    31  func (it *lazyRegexMap) Length() int {
    32  	if it == nil {
    33  		return 0
    34  	}
    35  
    36  	return len(it.items)
    37  }
    38  
    39  func (it *lazyRegexMap) LengthLock() int {
    40  	lazyRegexLock.Lock()
    41  	defer lazyRegexLock.Unlock()
    42  
    43  	return it.Length()
    44  }
    45  
    46  func (it *lazyRegexMap) Has(keyName string) bool {
    47  	_, has := it.items[keyName]
    48  
    49  	return has
    50  }
    51  
    52  func (it *lazyRegexMap) HasLock(keyName string) bool {
    53  	lazyRegexLock.Lock()
    54  	defer lazyRegexLock.Unlock()
    55  
    56  	_, has := it.items[keyName]
    57  
    58  	return has
    59  }
    60  
    61  func (it *lazyRegexMap) CreateOrExisting(
    62  	patternName string,
    63  ) (lazyRegex *LazyRegex, isExisting bool) {
    64  	lazyRegEx, has := it.items[patternName]
    65  
    66  	if has {
    67  		return lazyRegEx, has
    68  	}
    69  
    70  	// create
    71  	lazyRegex = it.createDefaultLazyRegex(
    72  		patternName,
    73  	)
    74  
    75  	it.items[patternName] = lazyRegex
    76  
    77  	return lazyRegex, false
    78  }
    79  
    80  func (it *lazyRegexMap) CreateOrExistingLock(
    81  	patternName string,
    82  ) (lazyRegex *LazyRegex, isExisting bool) {
    83  	lazyRegexLock.Lock()
    84  	defer lazyRegexLock.Unlock()
    85  
    86  	return it.CreateOrExisting(patternName)
    87  }
    88  
    89  func (it *lazyRegexMap) CreateOrExistingLockIf(
    90  	isLock bool,
    91  	patternName string,
    92  ) (lazyRegex *LazyRegex, isExisting bool) {
    93  	if isLock {
    94  		lazyRegexLock.Lock()
    95  		defer lazyRegexLock.Unlock()
    96  
    97  	}
    98  
    99  	return it.CreateOrExisting(patternName)
   100  }
   101  
   102  func (it *lazyRegexMap) createDefaultLazyRegex(
   103  	patternName string,
   104  ) (lazyRegex *LazyRegex) {
   105  	return &LazyRegex{
   106  		pattern:  patternName,
   107  		compiler: CreateLock, // must use lock func
   108  	}
   109  }
   110  
   111  func (it *lazyRegexMap) createLazyRegex(
   112  	patternName string,
   113  	creatorFunc func(pattern string) (*regexp.Regexp, error),
   114  ) (lazyRegex *LazyRegex) {
   115  	return &LazyRegex{
   116  		pattern:  patternName,
   117  		compiler: creatorFunc,
   118  	}
   119  }