...

Source file src/github.com/gobwas/glob/util/runes/runes.go

Documentation: github.com/gobwas/glob/util/runes

     1  package runes
     2  
     3  func Index(s, needle []rune) int {
     4  	ls, ln := len(s), len(needle)
     5  
     6  	switch {
     7  	case ln == 0:
     8  		return 0
     9  	case ln == 1:
    10  		return IndexRune(s, needle[0])
    11  	case ln == ls:
    12  		if Equal(s, needle) {
    13  			return 0
    14  		}
    15  		return -1
    16  	case ln > ls:
    17  		return -1
    18  	}
    19  
    20  head:
    21  	for i := 0; i < ls && ls-i >= ln; i++ {
    22  		for y := 0; y < ln; y++ {
    23  			if s[i+y] != needle[y] {
    24  				continue head
    25  			}
    26  		}
    27  
    28  		return i
    29  	}
    30  
    31  	return -1
    32  }
    33  
    34  func LastIndex(s, needle []rune) int {
    35  	ls, ln := len(s), len(needle)
    36  
    37  	switch {
    38  	case ln == 0:
    39  		if ls == 0 {
    40  			return 0
    41  		}
    42  		return ls
    43  	case ln == 1:
    44  		return IndexLastRune(s, needle[0])
    45  	case ln == ls:
    46  		if Equal(s, needle) {
    47  			return 0
    48  		}
    49  		return -1
    50  	case ln > ls:
    51  		return -1
    52  	}
    53  
    54  head:
    55  	for i := ls - 1; i >= 0 && i >= ln; i-- {
    56  		for y := ln - 1; y >= 0; y-- {
    57  			if s[i-(ln-y-1)] != needle[y] {
    58  				continue head
    59  			}
    60  		}
    61  
    62  		return i - ln + 1
    63  	}
    64  
    65  	return -1
    66  }
    67  
    68  // IndexAny returns the index of the first instance of any Unicode code point
    69  // from chars in s, or -1 if no Unicode code point from chars is present in s.
    70  func IndexAny(s, chars []rune) int {
    71  	if len(chars) > 0 {
    72  		for i, c := range s {
    73  			for _, m := range chars {
    74  				if c == m {
    75  					return i
    76  				}
    77  			}
    78  		}
    79  	}
    80  	return -1
    81  }
    82  
    83  func Contains(s, needle []rune) bool {
    84  	return Index(s, needle) >= 0
    85  }
    86  
    87  func Max(s []rune) (max rune) {
    88  	for _, r := range s {
    89  		if r > max {
    90  			max = r
    91  		}
    92  	}
    93  
    94  	return
    95  }
    96  
    97  func Min(s []rune) rune {
    98  	min := rune(-1)
    99  	for _, r := range s {
   100  		if min == -1 {
   101  			min = r
   102  			continue
   103  		}
   104  
   105  		if r < min {
   106  			min = r
   107  		}
   108  	}
   109  
   110  	return min
   111  }
   112  
   113  func IndexRune(s []rune, r rune) int {
   114  	for i, c := range s {
   115  		if c == r {
   116  			return i
   117  		}
   118  	}
   119  	return -1
   120  }
   121  
   122  func IndexLastRune(s []rune, r rune) int {
   123  	for i := len(s) - 1; i >= 0; i-- {
   124  		if s[i] == r {
   125  			return i
   126  		}
   127  	}
   128  
   129  	return -1
   130  }
   131  
   132  func Equal(a, b []rune) bool {
   133  	if len(a) == len(b) {
   134  		for i := 0; i < len(a); i++ {
   135  			if a[i] != b[i] {
   136  				return false
   137  			}
   138  		}
   139  
   140  		return true
   141  	}
   142  
   143  	return false
   144  }
   145  
   146  // HasPrefix tests whether the string s begins with prefix.
   147  func HasPrefix(s, prefix []rune) bool {
   148  	return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
   149  }
   150  
   151  // HasSuffix tests whether the string s ends with suffix.
   152  func HasSuffix(s, suffix []rune) bool {
   153  	return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):], suffix)
   154  }
   155  

View as plain text