Path: blob/master/thirdparty/pcre2/src/pcre2_match_data.c
9903 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#ifdef HAVE_CONFIG_H42#include "config.h"43#endif4445#include "pcre2_internal.h"46474849/*************************************************50* Create a match data block given ovector size *51*************************************************/5253/* A minimum of 1 is imposed on the number of ovector pairs. A maximum is also54imposed because the oveccount field in a match data block is uintt6_t. */5556PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION57pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)58{59pcre2_match_data *yield;60if (oveccount < 1) oveccount = 1;61if (oveccount > UINT16_MAX) oveccount = UINT16_MAX;62yield = PRIV(memctl_malloc)(63offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),64(pcre2_memctl *)gcontext);65if (yield == NULL) return NULL;66yield->oveccount = oveccount;67yield->flags = 0;68yield->heapframes = NULL;69yield->heapframes_size = 0;70return yield;71}72737475/*************************************************76* Create a match data block using pattern data *77*************************************************/7879/* If no context is supplied, use the memory allocator from the code. This code80assumes that a general context contains nothing other than a memory allocator.81If that ever changes, this code will need fixing. */8283PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION84pcre2_match_data_create_from_pattern(const pcre2_code *code,85pcre2_general_context *gcontext)86{87if (gcontext == NULL) gcontext = (pcre2_general_context *)code;88return pcre2_match_data_create(((const pcre2_real_code *)code)->top_bracket + 1,89gcontext);90}91929394/*************************************************95* Free a match data block *96*************************************************/9798PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION99pcre2_match_data_free(pcre2_match_data *match_data)100{101if (match_data != NULL)102{103if (match_data->heapframes != NULL)104match_data->memctl.free(match_data->heapframes,105match_data->memctl.memory_data);106if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)107match_data->memctl.free((void *)match_data->subject,108match_data->memctl.memory_data);109match_data->memctl.free(match_data, match_data->memctl.memory_data);110}111}112113114115/*************************************************116* Get last mark in match *117*************************************************/118119PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION120pcre2_get_mark(pcre2_match_data *match_data)121{122return match_data->mark;123}124125126127/*************************************************128* Get pointer to ovector *129*************************************************/130131PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION132pcre2_get_ovector_pointer(pcre2_match_data *match_data)133{134return match_data->ovector;135}136137138139/*************************************************140* Get number of ovector slots *141*************************************************/142143PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION144pcre2_get_ovector_count(pcre2_match_data *match_data)145{146return match_data->oveccount;147}148149150151/*************************************************152* Get starting code unit in match *153*************************************************/154155PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION156pcre2_get_startchar(pcre2_match_data *match_data)157{158return match_data->startchar;159}160161162163/*************************************************164* Get size of match data block *165*************************************************/166167PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION168pcre2_get_match_data_size(pcre2_match_data *match_data)169{170return offsetof(pcre2_match_data, ovector) +1712 * (match_data->oveccount) * sizeof(PCRE2_SIZE);172}173174175176/*************************************************177* Get heapframes size *178*************************************************/179180PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION181pcre2_get_match_data_heapframes_size(pcre2_match_data *match_data)182{183return match_data->heapframes_size;184}185186/* End of pcre2_match_data.c */187188189