github.com/m3db/m3@v1.5.0/src/query/models/strconv/checker.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package strconv
    22  
    23  // NB: for nicer table formatting.
    24  const (
    25  	// Indicates this rune does not need escaping.
    26  	ff = false
    27  	// Indicates this rune needs escaping.
    28  	tt = true
    29  )
    30  
    31  // Determines valid characters that do not require escaping.
    32  //
    33  // NB: escape all control characters, `"`, and any character above `~`. This
    34  // table loosely based on constants used in utf8.DecodeRune.
    35  var escape = [256]bool{
    36  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    37  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x00-0x0F
    38  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x10-0x1F
    39  	ff, ff, tt, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x20-0x2F
    40  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x30-0x3F
    41  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x40-0x4F
    42  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x50-0x5F
    43  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x60-0x6F
    44  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, tt, // 0x70-0x7F
    45  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    46  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x80-0x8F
    47  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x90-0x9F
    48  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xA0-0xAF
    49  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xB0-0xBF
    50  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xC0-0xCF
    51  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xD0-0xDF
    52  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xE0-0xEF
    53  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0xF0-0xFF
    54  }
    55  
    56  // NeedToEscape returns true if the byte slice contains characters that will
    57  // need to be escaped when quoting the slice.
    58  func NeedToEscape(bb []byte) bool {
    59  	for _, b := range bb {
    60  		if escape[b] {
    61  			return true
    62  		}
    63  	}
    64  
    65  	return false
    66  }
    67  
    68  // Determines valid alphanumeric characters.
    69  var alphaNumeric = [256]bool{
    70  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    71  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x00-0x0F
    72  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x10-0x1F
    73  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x20-0x2F
    74  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, ff, ff, ff, ff, ff, ff, // 0x30-0x3F
    75  	ff, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x40-0x4F
    76  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, ff, ff, ff, ff, ff, // 0x50-0x5F
    77  	ff, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, // 0x60-0x6F
    78  	tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, tt, ff, ff, ff, ff, ff, // 0x70-0x7F
    79  	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    80  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x80-0x8F
    81  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0x90-0x9F
    82  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xA0-0xAF
    83  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xB0-0xBF
    84  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xC0-0xCF
    85  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xD0-0xDF
    86  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xE0-0xEF
    87  	ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, ff, // 0xF0-0xFF
    88  }
    89  
    90  // IsAlphaNumeric returns true if the given string is alpha numeric.
    91  //
    92  // NB: here this means that it contains only characters in [0-9A-Za-z])
    93  func IsAlphaNumeric(str string) bool {
    94  	for _, c := range str {
    95  		if !alphaNumeric[c] {
    96  			return false
    97  		}
    98  	}
    99  
   100  	return true
   101  }
   102  
   103  // IsRuneAlphaNumeric returns true if the given rune is alpha numeric.
   104  //
   105  // NB: here this means that it contains only characters in [0-9A-Za-z])
   106  func IsRuneAlphaNumeric(r rune) bool {
   107  	return alphaNumeric[r]
   108  }