ext/iodine/fio.c
/** A binary glob matching helper. Returns 1 on match, otherwise returns 0. */
A binary glob matching helper. Returns 1 on match, otherwise returns 0.
A binary glob matching helper . Returns 1 on match otherwise returns 0 .
static int fio_glob_match(fio_str_info_s pat, fio_str_info_s ch) {
  uint8_t *back_pat = NULL, *back_str = (uint8_t *)ch.data;
  size_t back_pat_len = 0, back_str_len = ch.len;
  while (ch.len) {
    uint8_t c = *(uint8_t *)ch.data++;
    uint8_t d = *(uint8_t *)pat.data++;
    ch.len--;
    pat.len--;
    switch (d) {
    case '?': 
      break;
    case '*':       
      if (!pat.len) 
        return 1;
      back_pat = (uint8_t *)pat.data;
      back_pat_len = pat.len;
      back_str = (uint8_t *)--ch.data; 
      back_str_len = ++ch.len;
      break;
    case '[': { 
      uint8_t match = 0, inverted = (*(uint8_t *)pat.data == '^');
      uint8_t *cls = (uint8_t *)pat.data + inverted;
      uint8_t a = *cls++;
      do {
        uint8_t b = a;
        if (cls[0] == '-' && cls[1] != ']') {
          b = cls[1];
          cls += 2;
          if (a > b) {
            uint8_t tmp = a;
            a = b;
            b = tmp;
          }
        }
        match |= (a <= c && c <= b);
      } while ((a = *cls++) != ']');
      if (match == inverted)
        goto backtrack;
      pat.len -= cls - (uint8_t *)pat.data;
      pat.data = (char *)cls;
    } break;
    case '\\':
      d = *(uint8_t *)pat.data++;
      pat.len--;
    default: 
      if (c == d)
        break;
    backtrack:
      if (!back_pat)
        return 0; 
      pat.data = (char *)back_pat;
      ch.data = (char *)++back_str;
      ch.len = --back_str_len;
      pat.len = back_pat_len;
    }
  }
  return !ch.len && !pat.len;
}
static int fio_glob_match ( fio_str_info_s pat , fio_str_info_s ch ) { uint8_t * back_pat = NULL , * back_str = ( uint8_t * ) ch . data ; size_t back_pat_len = 0 , back_str_len = ch . len ; while ( ch . len ) { uint8_t c = * ( uint8_t * ) ch . data ++ ; uint8_t d = * ( uint8_t * ) pat . data ++ ; ch . len -- ; pat . len -- ; switch ( d ) { case ' ' : break ; case ' ' : if ( ! pat . len ) return 1 ; back_pat = ( uint8_t * ) pat . data ; back_pat_len = pat . len ; back_str = ( uint8_t * ) -- ch . data ; back_str_len = ++ ch . len ; break ; case ' ' : { uint8_t match = 0 , inverted = ( * ( uint8_t * ) pat . data == ' ' ) ; uint8_t * cls = ( uint8_t * ) pat . data + inverted ; uint8_t a = * cls ++ ; do { uint8_t b = a ; if ( cls [ 0 ] == ' ' && cls [ 1 ] != ' ' ) { b = cls [ 1 ] ; cls += 2 ; if ( a > b ) { uint8_t tmp = a ; a = b ; b = tmp ; } } match |= ( a <= c && c <= b ) ; } while ( ( a = * cls ++ ) != ' ' ) ; if ( match == inverted ) goto backtrack ; pat . len -= cls - ( uint8_t * ) pat . data ; pat . data = ( char * ) cls ; } break ; case ' \\ ' : d = * ( uint8_t * ) pat . data ++ ; pat . len -- ; default : if ( c == d ) break ; backtrack : if ( ! back_pat ) return 0 ; pat . data = ( char * ) back_pat ; ch . data = ( char * ) ++ back_str ; ch . len = -- back_str_len ; pat . len = back_pat_len ; } } return ! ch . len && ! pat . len ; }
A binary glob matching helper.
A binary glob matching helper .
/* adapted and rewritten, with thankfulness, from the code at:
   * https://github.com/opnfv/kvmfornfv/blob/master/kernel/lib/glob.c
   *
   * Original version's copyright:
   * Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
   * Under the MIT license.
   */ /*
   * Backtrack to previous * on mismatch and retry starting one
   * character later in the string.  Because * matches all characters,
   * there's never a need to backtrack multiple levels.
   */ /*
   * Loop over each token (character or class) in pat, matching
   * it against the remaining unmatched tail of str.  Return false
   * on mismatch, or true after matching the trailing nul bytes.
   */ /* Wildcard: anything goes */ /* Any-length wildcard */ /* Optimize trailing * case */ /* Allow zero-length match */ /* Character class */ /*
       * Iterate over each span in the character class.
       * A span is either a single character a, or a
       * range a-b.  The first span may begin with ']'.
       */ /* fallthrough */ /* Literal character */ /* No point continuing */ /* Try again from last *, one character later in str. */