Path: blob/master/thirdparty/pcre2/src/pcre2_match_data.c
21635 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041#include "pcre2_internal.h"42434445/*************************************************46* Create a match data block given ovector size *47*************************************************/4849/* A minimum of 1 is imposed on the number of ovector pairs. A maximum is also50imposed because the oveccount field in a match data block is uintt6_t. */5152PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION53pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)54{55pcre2_match_data *yield;56if (oveccount < 1) oveccount = 1;57if (oveccount > UINT16_MAX) oveccount = UINT16_MAX;58yield = PRIV(memctl_malloc)(59offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),60(pcre2_memctl *)gcontext);61if (yield == NULL) return NULL;62yield->oveccount = oveccount;63yield->flags = 0;64yield->heapframes = NULL;65yield->heapframes_size = 0;66return yield;67}68697071/*************************************************72* Create a match data block using pattern data *73*************************************************/7475/* If no context is supplied, use the memory allocator from the code. This code76assumes that a general context contains nothing other than a memory allocator.77If that ever changes, this code will need fixing. */7879PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION80pcre2_match_data_create_from_pattern(const pcre2_code *code,81pcre2_general_context *gcontext)82{83if (code == NULL) return NULL;84if (gcontext == NULL) gcontext = (pcre2_general_context *)code;85return pcre2_match_data_create(((const pcre2_real_code *)code)->top_bracket + 1,86gcontext);87}88899091/*************************************************92* Free a match data block *93*************************************************/9495PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION96pcre2_match_data_free(pcre2_match_data *match_data)97{98if (match_data != NULL)99{100if (match_data->heapframes != NULL)101match_data->memctl.free(match_data->heapframes,102match_data->memctl.memory_data);103if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)104match_data->memctl.free((void *)match_data->subject,105match_data->memctl.memory_data);106match_data->memctl.free(match_data, match_data->memctl.memory_data);107}108}109110111112/*************************************************113* Get last mark in match *114*************************************************/115116PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION117pcre2_get_mark(pcre2_match_data *match_data)118{119return match_data->mark;120}121122123124/*************************************************125* Get pointer to ovector *126*************************************************/127128PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION129pcre2_get_ovector_pointer(pcre2_match_data *match_data)130{131return match_data->ovector;132}133134135136/*************************************************137* Get number of ovector slots *138*************************************************/139140PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION141pcre2_get_ovector_count(pcre2_match_data *match_data)142{143return match_data->oveccount;144}145146147148/*************************************************149* Get starting code unit in match *150*************************************************/151152PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION153pcre2_get_startchar(pcre2_match_data *match_data)154{155return match_data->startchar;156}157158159160/*************************************************161* Get size of match data block *162*************************************************/163164PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION165pcre2_get_match_data_size(pcre2_match_data *match_data)166{167return offsetof(pcre2_match_data, ovector) +1682 * (match_data->oveccount) * sizeof(PCRE2_SIZE);169}170171172173/*************************************************174* Get heapframes size *175*************************************************/176177PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION178pcre2_get_match_data_heapframes_size(pcre2_match_data *match_data)179{180return match_data->heapframes_size;181}182183/* End of pcre2_match_data.c */184185186