github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/values/string_list.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package values
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/zero"
     7  	"strings"
     8  )
     9  
    10  type StringList struct {
    11  	ValueMap        map[string]zero.Zero
    12  	CaseInsensitive bool
    13  }
    14  
    15  func NewStringList(caseInsensitive bool) *StringList {
    16  	return &StringList{
    17  		ValueMap:        map[string]zero.Zero{},
    18  		CaseInsensitive: caseInsensitive,
    19  	}
    20  }
    21  
    22  func ParseStringList(v string, caseInsensitive bool) *StringList {
    23  	var list = NewStringList(caseInsensitive)
    24  	if len(v) == 0 {
    25  		return list
    26  	}
    27  
    28  	var lines = strings.Split(v, "\n")
    29  	for _, line := range lines {
    30  		line = strings.TrimSpace(line)
    31  		if len(line) == 0 {
    32  			continue
    33  		}
    34  
    35  		var values = strings.Split(line, ",")
    36  		for _, value := range values {
    37  			value = strings.TrimSpace(value)
    38  			if len(value) > 0 {
    39  				if caseInsensitive {
    40  					value = strings.ToLower(value)
    41  				}
    42  				list.ValueMap[value] = zero.Zero{}
    43  			}
    44  		}
    45  	}
    46  	return list
    47  }
    48  
    49  func (this *StringList) Contains(f string) bool {
    50  	if this.CaseInsensitive {
    51  		f = strings.ToLower(f)
    52  	}
    53  	_, ok := this.ValueMap[f]
    54  	return ok
    55  }