/// Will find the first non escaped quote in a JSON String
/// @access private
/// @param {String} $string - to search in
/// @return {Number} - position of the first non escaped quote
@function _find-ending-quote($string) {
  $backslash: str-slice('\\', 1, 1); // Dirty hack to have a single backslash
  $escaped-chars: '"\/bfnrtu'; // Characters that can be escaped in JSON
  $hexadecimal-chars: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' 'a' 'b' 'c' 'd' 'e' 'f';

  $pos: 1;
  $length: str-length($string);
  $backslash-found: false;

  @while $pos <= $length {
    $char: to-lower-case(str-slice($string, $pos, $pos));

    // Previous char was a backslash
    @if $backslash-found {

      // Special case, the 'u' character
      @if $char == 'u' {
        // Next 4 characters must be hexadecimal
        @if not index($hexadecimal-chars, str-slice($string, $pos + 1, $pos + 1)) or
            not index($hexadecimal-chars, str-slice($string, $pos + 2, $pos + 2)) or
            not index($hexadecimal-chars, str-slice($string, $pos + 3, $pos + 3)) or
            not index($hexadecimal-chars, str-slice($string, $pos + 4, $pos + 4)) {
          @return 0;
        }

        $pos: $pos + 4;
      } @else if not str-index($escaped-chars, $char) {
        // Invalid character escaped
        @return 0;
      }

      $backslash-found: false;
    } @else if $char == $backslash {
      $backslash-found: true;
    } @else if $char == '"' {
      // Unescaped quote found
      @return $pos;
    }

    $pos: $pos + 1;
  }

  // No end of string found
  @return 0;
}
