Parses PostgreSQL encoded string

@param string $value
@param int $i parse starting position
@return string
433215
private function parseString($value, &$i)
    {
        $isQuoted = $value[$i] === '"';
        $endChars = $isQuoted ? ['"'] : [',', ')'];
        $result = '';
        $length = strlen($value);
        for ($i += $isQuoted ? 1 : 0; $i < $length; ++$i) {
            if (in_array($value[$i], ['\\', '"'], true) && in_array($value[$i + 1], [$value[$i], '"'], true)) {
                ++$i;
            } elseif (in_array($value[$i], $endChars, true)) {
                break;
            }

            $result .= $value[$i];
        }

        $i -= $isQuoted ? 0 : 1;

        return $result;
    }
548714
public function parse_value($value) {
        global $COURSE;

        if (strpos($value, '$LineItems.url') !== false) {
            $this->params['context_id'] = $COURSE->id;
            $value = str_replace('$LineItems.url', parent::get_endpoint(), $value);
        }

        return $value;

    }
233536
public static function convertFromSQLKeywords($value)
    {
        $pattern = array(
            '/(?:is\s+null)|(like\s+null)|' .
            '(?:(?:^|\W)in[+\s]*\([\s\d"]+[^()]*\))/ims'
        );
        $value   = preg_replace($pattern, '"=0', $value);

        $value   = preg_replace('/[^\w\)]+\s*like\s*[^\w\s]+/ims', '1" OR "1"', $value);
        $value   = preg_replace('/null([,"\s])/ims', '0$1', $value);
        $value   = preg_replace('/\d+\./ims', ' 1', $value);
        $value   = preg_replace('/,null/ims', ',0', $value);
        $value   = preg_replace('/(?:between)/ims', 'or', $value);
        $value   = preg_replace('/(?:and\s+\d+\.?\d*)/ims', '', $value);
        $value   = preg_replace('/(?:\s+and\s+)/ims', ' or ', $value);

        $pattern = array(
            '/(?:not\s+between)|(?:is\s+not)|(?:not\s+in)|' .
            '(?:xor|<>|rlike(?:\s+binary)?)|' .
            '(?:regexp\s+binary)|' .
            '(?:sounds\s+like)/ims'
        );
        $value   = preg_replace($pattern, '!', $value);
        $value   = preg_replace('/"\s+\d/', '"', $value);
        $value   = preg_replace('/(\W)div(\W)/ims', '$1 OR $2', $value);
        $value   = preg_replace('/\/(?:\d+|null)/', null, $value);

        return $value;
    }
1077640
private function parse(string $value): void
    {
        // Check for castling:
        if (preg_match('/^(O-O|O-O-O)(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->castling = $matches[1];
            $this->check = $matches[2] === '+';
            $this->checkmate = $matches[2] === '#';
            $this->annotation = isset($matches[3]) ? $matches[3] : null;
            return;
        }

        // Pawn movement:
        if (preg_match('/^([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->targetColumn = $matches[1];
            $this->targetRow = (int)$matches[2];
            $this->check = $matches[3] === '+';
            $this->checkmate = $matches[3] === '#';
            $this->movedPiece = self::PIECE_PAWN;
            $this->annotation = isset($matches[4]) ? $matches[4] : null;
            return;
        }

        // Pawn movement (long san):
        if (preg_match('/^([a-h])([1-8])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPieceDisambiguationColumn = $matches[1];
            $this->movedPieceDisambiguationRow = (int)$matches[2];
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->movedPiece = self::PIECE_PAWN;
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            $this->longSan = true;
            return;
        }

        // Piece movement:
        if (preg_match('/^([KQBNR])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->targetColumn = $matches[2];
            $this->targetRow = (int)$matches[3];
            $this->check = $matches[4] === '+';
            $this->checkmate = $matches[4] === '#';
            $this->annotation = isset($matches[5]) ? $matches[5] : null;
            return;
        }

        // Piece movement from a specific column:
        if (preg_match('/^([KQBNR])([a-h])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationColumn = $matches[2];
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            return;
        }

        // Piece movement from a specific row:
        if (preg_match('/^([KQBNR])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationRow = (int)$matches[2];
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            return;
        }

        // Piece movement from a specific column and row (long san):
        if (preg_match('/^([KQBNR])([a-h])([0-9])([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationColumn = $matches[2];
            $this->movedPieceDisambiguationRow = (int)$matches[3];
            $this->targetColumn = $matches[4];
            $this->targetRow = (int)$matches[5];
            $this->check = $matches[6] === '+';
            $this->checkmate = $matches[6] === '#';
            $this->annotation = isset($matches[7]) ? $matches[7] : null;
            $this->longSan = true;
            return;
        }

        // Pawn capture:
        if (preg_match('/^([a-h])x([a-h])([1-8])(?:=?([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->targetColumn = $matches[2];
            $this->targetRow = (int)$matches[3];
            $this->movedPiece = self::PIECE_PAWN;
            $this->movedPieceDisambiguationColumn = $matches[1];
            $this->capture = true;
            $this->promotedPiece = $matches[4] ?: null;
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            return;
        }

        // Pawn capture (long san):
        if (preg_match('/^([a-h])([1-8])x([a-h])([1-8])(?:=?([KQBNR]))?(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->movedPiece = self::PIECE_PAWN;
            $this->movedPieceDisambiguationColumn = $matches[1];
            $this->movedPieceDisambiguationRow = (int)$matches[2];
            $this->capture = true;
            $this->promotedPiece = $matches[5] ?: null;
            $this->check = $matches[6] === '+';
            $this->checkmate = $matches[6] === '#';
            $this->annotation = isset($matches[7]) ? $matches[7] : null;
            $this->longSan = true;
            return;
        }

        // Piece capture:
        if (preg_match('/^([KQBNR])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->targetColumn = $matches[2];
            $this->targetRow = (int)$matches[3];
            $this->check = $matches[4] === '+';
            $this->checkmate = $matches[4] === '#';
            $this->capture = true;
            $this->annotation = isset($matches[5]) ? $matches[5] : null;
            return;
        }

        // Piece capture from a specific column:
        if (preg_match('/^([KQBNR])([a-h])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationColumn = $matches[2];
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->capture = true;
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            return;
        }

        // Piece capture from a specific row:
        if (preg_match('/^([KQBNR])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationRow = (int)$matches[2];
            $this->targetColumn = $matches[3];
            $this->targetRow = (int)$matches[4];
            $this->check = $matches[5] === '+';
            $this->checkmate = $matches[5] === '#';
            $this->capture = true;
            $this->annotation = isset($matches[6]) ? $matches[6] : null;
            return;
        }

        // Piece capture from a specific column and row (long san):
        if (preg_match('/^([KQBNR])([a-h])([0-9])x([a-h])([1-8])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = $matches[1];
            $this->movedPieceDisambiguationColumn = $matches[2];
            $this->movedPieceDisambiguationRow = (int)$matches[3];
            $this->targetColumn = $matches[4];
            $this->targetRow = (int)$matches[5];
            $this->check = $matches[6] === '+';
            $this->checkmate = $matches[6] === '#';
            $this->capture = true;
            $this->annotation = isset($matches[7]) ? $matches[7] : null;
            $this->longSan = true;
            return;
        }

        // Check for pawn promotion:
        if (preg_match('/^([a-h])([1-8])=?([KQBNR])(\+|\#?)(\?\?|\?|\?\!|\!|\!\!)?$/', $value, $matches)) {
            $this->movedPiece = self::PIECE_PAWN;
            $this->targetColumn = $matches[1];
            $this->targetRow = (int)$matches[2];
            $this->promotedPiece = $matches[3];
            $this->check = $matches[4] === '+';
            $this->checkmate = $matches[4] === '#';
            $this->annotation = isset($matches[5]) ? $matches[5] : null;
            return;
        }

        throw new InvalidArgumentException(sprintf(
            'The value "%s" could not be parsed.',
            $value
        ));
    }
294941
protected function getBoolFromStringInt($value)
    {
        if (!($value == '0' || $value == '1')) {
            $this->throwException(sprintf('Expected "0" or "1" but got: %s', $value));
        }

        return (bool) $value;
    }
593756
public static function charAt(string $str, int $i): string
    {
        return (string)BaseStringy::create($str)->at($i);
    }
855428
public function parse_value($value) {
        global $COURSE, $DB;

        if (strpos($value, '$ToolProxyBinding.memberships.url') !== false) {
            if ($COURSE->id === SITEID) {
                $this->params['context_type'] = 'Group';
            } else {
                $this->params['context_type'] = 'CourseSection';
            }
            $this->params['context_id'] = $COURSE->id;

            $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
            if (!empty($id)) {
                $cm = get_coursemodule_from_id('lti', $id, 0, false, IGNORE_MISSING);
                $lti = $DB->get_record('lti', array('id' => $cm->instance), 'typeid', IGNORE_MISSING);
                if ($lti && !empty($lti->typeid)) {
                    $this->params['tool_code'] = $lti->typeid;
                }
            }
            $value = str_replace('$ToolProxyBinding.memberships.url', parent::get_endpoint(), $value);
        }
        return $value;

    }
571902
public function mysqliStmtType($value) {
        $value = trim($value);
        if (!preg_match('/[^0-9\.]/', $value) && substr($value, 0, 1) != 0) {
            // d or i?
            return ctype_digit($value) ? 'i' : 'd';
        }
        return 's';
    }
12379
protected function processValue(&$value) {
    if (!is_array($value)) {
      // decode encoded (%##) values
      if (preg_match ("/%/", $value)) {
        $value = urldecode($value);
      }
      // make arrays
      if(preg_match("/^{.*}$/", $value)) {
        $arrayValues = StringUtil::quotesplit(substr($value, 1, -1));
        $value = [];
        foreach ($arrayValues as $arrayValue) {
          $value[] = trim($arrayValue);
        }
      }
    }
  }
893588
public static void setPStmt(int type, PreparedStatement pstmt, Object value, int i) throws SQLException {
        switch (type) {
            case Types.BIT:
            case Types.BOOLEAN:
                if (value instanceof Boolean) {
                    pstmt.setBoolean(i, (Boolean) value);
                } else if (value instanceof String) {
                    boolean v = !value.equals("0");
                    pstmt.setBoolean(i, v);
                } else if (value instanceof Number) {
                    boolean v = ((Number) value).intValue() != 0;
                    pstmt.setBoolean(i, v);
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.CHAR:
            case Types.NCHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                if (value instanceof String) {
                    pstmt.setString(i, (String) value);
                } else if (value == null) {
                    pstmt.setNull(i, type);
                } else {
                    pstmt.setString(i, value.toString());
                }
                break;
            case Types.TINYINT:
                if (value instanceof Number) {
                    pstmt.setByte(i, ((Number) value).byteValue());
                } else if (value instanceof String) {
                    pstmt.setByte(i, Byte.parseByte((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.SMALLINT:
                if (value instanceof Number) {
                    pstmt.setShort(i, ((Number) value).shortValue());
                } else if (value instanceof String) {
                    pstmt.setShort(i, Short.parseShort((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.INTEGER:
                if (value instanceof Number) {
                    pstmt.setInt(i, ((Number) value).intValue());
                } else if (value instanceof String) {
                    pstmt.setInt(i, Integer.parseInt((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.BIGINT:
                if (value instanceof Number) {
                    pstmt.setLong(i, ((Number) value).longValue());
                } else if (value instanceof String) {
                    pstmt.setLong(i, Long.parseLong((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.DECIMAL:
            case Types.NUMERIC:
                if (value instanceof BigDecimal) {
                    pstmt.setBigDecimal(i, (BigDecimal) value);
                } else if (value instanceof Byte) {
                    pstmt.setInt(i, ((Byte) value).intValue());
                } else if (value instanceof Short) {
                    pstmt.setInt(i, ((Short) value).intValue());
                } else if (value instanceof Integer) {
                    pstmt.setInt(i, (Integer) value);
                } else if (value instanceof Long) {
                    pstmt.setLong(i, (Long) value);
                } else if (value instanceof Float) {
                    pstmt.setBigDecimal(i, new BigDecimal((float) value));
                } else if (value instanceof Double) {
                    pstmt.setBigDecimal(i, new BigDecimal((double) value));
                } else if (value != null) {
                    pstmt.setBigDecimal(i, new BigDecimal(value.toString()));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.REAL:
                if (value instanceof Number) {
                    pstmt.setFloat(i, ((Number) value).floatValue());
                } else if (value instanceof String) {
                    pstmt.setFloat(i, Float.parseFloat((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.FLOAT:
            case Types.DOUBLE:
                if (value instanceof Number) {
                    pstmt.setDouble(i, ((Number) value).doubleValue());
                } else if (value instanceof String) {
                    pstmt.setDouble(i, Double.parseDouble((String) value));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.BLOB:
                if (value instanceof Blob) {
                    pstmt.setBlob(i, (Blob) value);
                } else if (value instanceof byte[]) {
                    pstmt.setBytes(i, (byte[]) value);
                } else if (value instanceof String) {
                    pstmt.setBytes(i, ((String) value).getBytes(StandardCharsets.ISO_8859_1));
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.CLOB:
                if (value instanceof Clob) {
                    pstmt.setClob(i, (Clob) value);
                } else if (value instanceof byte[]) {
                    pstmt.setBytes(i, (byte[]) value);
                } else if (value instanceof String) {
                    Reader clobReader = new StringReader((String) value);
                    pstmt.setCharacterStream(i, clobReader);
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.DATE:
                if (value instanceof java.sql.Date) {
                    pstmt.setDate(i, (java.sql.Date) value);
                } else if (value instanceof java.util.Date) {
                    pstmt.setDate(i, new java.sql.Date(((java.util.Date) value).getTime()));
                } else if (value instanceof String) {
                    String v = (String) value;
                    if (!v.startsWith("0000-00-00")) {
                        java.util.Date date = Util.parseDate(v);
                        if (date != null) {
                            pstmt.setDate(i, new Date(date.getTime()));
                        } else {
                            pstmt.setNull(i, type);
                        }
                    } else {
                        pstmt.setObject(i, value);
                    }
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.TIME:
                if (value instanceof java.sql.Time) {
                    pstmt.setTime(i, (java.sql.Time) value);
                } else if (value instanceof java.util.Date) {
                    pstmt.setTime(i, new java.sql.Time(((java.util.Date) value).getTime()));
                } else if (value instanceof String) {
                    String v = (String) value;
                    java.util.Date date = Util.parseDate(v);
                    if (date != null) {
                        pstmt.setTime(i, new Time(date.getTime()));
                    } else {
                        pstmt.setNull(i, type);
                    }
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            case Types.TIMESTAMP:
                if (value instanceof java.sql.Timestamp) {
                    pstmt.setTimestamp(i, (java.sql.Timestamp) value);
                } else if (value instanceof java.util.Date) {
                    pstmt.setTimestamp(i, new java.sql.Timestamp(((java.util.Date) value).getTime()));
                } else if (value instanceof String) {
                    String v = (String) value;
                    if (!v.startsWith("0000-00-00")) {
                        java.util.Date date = Util.parseDate(v);
                        if (date != null) {
                            pstmt.setTimestamp(i, new Timestamp(date.getTime()));
                        } else {
                            pstmt.setNull(i, type);
                        }
                    } else {
                        pstmt.setObject(i, value);
                    }
                } else {
                    pstmt.setNull(i, type);
                }
                break;
            default:
                pstmt.setObject(i, value, type);
        }
946400
protected function parseDate($value)
    {
        // Skip empty values
        if (empty($value) && !is_numeric($value)) {
            return null;
        }

        // Determine value to parse
        if (is_array($value)) {
            $source = $value; // parse array
        } elseif (is_numeric($value)) {
            $source = $value; // parse timestamp
        } else {
            // Convert US date -> iso, fix y2k, etc
            $value = $this->fixInputDate($value);
            if (is_null($value)) {
                return null;
            }
            $source = strtotime($value); // convert string to timestamp
        }
        if ($value === false) {
            return false;
        }

        // Format as iso8601
        $formatter = $this->getInternalFormatter();
        return $formatter->format($source);
    }
355035
private function parsePre(array $lines, $value, $start)
    {
        foreach ($lines as &$line) {
            $line = htmlspecialchars(substr($line, 4));
        }
        $str = implode("\n", $this->markLines($lines, $start));
        return preg_match("/^\s*$/", $str) ? '' : '<pre><code>' . $str . '</code></pre>';
    }
27029
protected function parse_value($value){
        switch(true){
            case $value instanceof \Carbon\Carbon:
                $value = $value->format('d M y');
                break;
        }

        return (string) $value;
    }
881943
public function getDatabaseValue($value)
        {
            if (is_bool($value)) {
                if (Core::getDriver() == Core::DRIVER_MYSQL) {
                    return (int) $value;
                } elseif (Core::getDriver() == Core::DRIVER_POSTGRES) {
                    return ($value) ? 'true' : 'false';
                }
            }
            return $value;
        }
126584
private static function _parseDateString($value)
    {
        $result = @preg_match('/^\d\d\d\d-\d?\d-\d?\d$/', $value);
        if ($result !== 1)
            return null;
        $timestamp = strtotime($value);
        if ($timestamp === false)
            return null;

        return $timestamp;
    }
367422
public static function parse(string $value, array $references = [])
    {
        $value = \trim($value);

        if ('' === $value) {
            return '';
        }

        $i = 0;
        switch ($value[$i]) {
            case '[':
                $result = self::parseSequence($value, $i, $references);
                ++$i;
                break;
            case '{':
                $result = self::parseMapping($value, $i, $references);
                ++$i;
                break;
            default:
                $result = self::parseScalar($value, null, $i, true, $references);
        }

        // some comments are allowed at the end
        if (\preg_replace('/\s+#.*$/A', '', \substr($value, $i))) {
            throw new ParseException(\sprintf('Unexpected characters near "%s".', \substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
        }

        return $result;
    }
420473
private function parseComposite($value, &$i = 0)
    {
        $result = [];
        $length = strlen($value);
        for(++$i; $i < $length; ++$i) {
            switch ($value[$i]) {
                case ')':
                    break 2;
                case ',':
                    if (empty($result)) {
                        $result[] = null;
                    }
                    if (in_array($value[$i + 1], [',', ')'], true)) {
                        $result[] = null;
                    }
                    break;
                default:
                    $result[] = $this->parseString($value, $i);
            }
        }

        return $result;
    }