github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/appdef/utils.go (about)

     1  /*
     2   * Copyright (c) 2021-present Sigma-Soft, Ltd.
     3   * @author: Nikolay Nikitin
     4   */
     5  
     6  package appdef
     7  
     8  // Returns is string is valid identifier and error if not
     9  func ValidIdent(ident string) (bool, error) {
    10  	if len(ident) < 1 {
    11  		return false, ErrMissed("ident")
    12  	}
    13  
    14  	if l := len(ident); l > MaxIdentLen {
    15  		return false, ErrOutOfBounds("ident «%s» too long (%d runes, max is %d)", ident, l, MaxIdentLen)
    16  	}
    17  
    18  	const (
    19  		char_a    rune = 97
    20  		char_A    rune = 65
    21  		char_z    rune = 122
    22  		char_Z    rune = 90
    23  		char_0    rune = 48
    24  		char_9    rune = 57
    25  		char__    rune = 95
    26  		char_Buck rune = 36
    27  	)
    28  
    29  	digit := func(r rune) bool { return (char_0 <= r) && (r <= char_9) }
    30  
    31  	letter := func(r rune) bool { return ((char_a <= r) && (r <= char_z)) || ((char_A <= r) && (r <= char_Z)) }
    32  
    33  	underScore := func(r rune) bool { return r == char__ }
    34  
    35  	buck := func(r rune) bool { return r == char_Buck }
    36  
    37  	for p, c := range ident {
    38  		if !letter(c) && !underScore(c) && !buck(c) {
    39  			if (p == 0) || !digit(c) {
    40  				return false, ErrInvalid("ident «%s» has invalid char «%c» at pos %d", ident, c, p)
    41  			}
    42  		}
    43  	}
    44  
    45  	return true, nil
    46  }
    47  
    48  // Returns is string is valid field name and error if not
    49  func ValidFieldName(ident FieldName) (bool, error) {
    50  	return ValidIdent(ident)
    51  }
    52  
    53  // TODO: implement
    54  // Parsing a URI Reference with a Regular Expression [RFC3986, app B](https://datatracker.ietf.org/doc/html/rfc3986#appendix-B)
    55  func ValidPackagePath(path string) (bool, error) {
    56  	if len(path) < 1 {
    57  		return false, ErrMissed("package path")
    58  	}
    59  	return true, nil
    60  }