ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
pcre2.h File Reference

PCRE2 compilation and match helpers. More...

Go to the source code of this file.

Macros

#define PCRE2_CODE_UNIT_WIDTH   8

Functions

pcre2_code * asciichat_pcre2_compile (const char *pattern, uint32_t flags)
char * asciichat_pcre2_extract_named_group (pcre2_code *regex, pcre2_match_data *match_data, const char *group_name, const char *subject)
 Extract named substring from PCRE2 match data.
char * asciichat_pcre2_extract_group (pcre2_match_data *match_data, int group_num, const char *subject)
 Extract numbered capture group as allocated string.
const char * asciichat_pcre2_extract_group_ptr (pcre2_match_data *match_data, int group_num, const char *subject, size_t *out_len)
 Extract numbered capture group as pointer into subject (non-allocating).
bool asciichat_pcre2_extract_group_ulong (pcre2_match_data *match_data, int group_num, const char *subject, unsigned long *out_value)
 Extract numbered capture group and convert to unsigned long.
int asciichat_pcre2_match (const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, uint32_t options, pcre2_match_data *match_data, pcre2_match_context *context)

Detailed Description

PCRE2 compilation and match helpers.

Compiled patterns are owned and freed by their callers.

Definition in file pcre2.h.

Macro Definition Documentation

◆ PCRE2_CODE_UNIT_WIDTH

#define PCRE2_CODE_UNIT_WIDTH   8

Definition at line 11 of file pcre2.h.

Function Documentation

◆ asciichat_pcre2_compile()

pcre2_code * asciichat_pcre2_compile ( const char * pattern,
uint32_t flags )

Compile a pattern owned by the caller, who must call pcre2_code_free().

Definition at line 16 of file pcre2.c.

16 {
17 int error;
18 PCRE2_SIZE offset;
19 return pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, flags,
20 &error, &offset, NULL);
21}

Referenced by grep_init(), log_search_enter_mode(), log_search_exit_mode(), and log_search_init().

◆ asciichat_pcre2_extract_group()

char * asciichat_pcre2_extract_group ( pcre2_match_data * match_data,
int group_num,
const char * subject )

Extract numbered capture group as allocated string.

Extracts a numbered capture group from match data and returns an allocated string. The caller is responsible for freeing the returned string with SAFE_FREE().

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
Returns
Allocated string containing the matched substring, or NULL if group not matched

Example:

char *foundation = asciichat_pcre2_extract_group(match_data, 1, line);
if (foundation) {
// Use foundation...
SAFE_FREE(foundation);
}
#define SAFE_FREE(ptr)
char * asciichat_pcre2_extract_group(pcre2_match_data *match_data, int group_num, const char *subject)
Extract numbered capture group as allocated string.
Definition pcre2.c:79

Definition at line 79 of file pcre2.c.

80 {
81 if (!match_data || !subject || group_num < 0) {
82 return NULL;
83 }
84
85 /* Get ovector (output vector with match offsets) */
86 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
87 PCRE2_SIZE start = ovector[2 * group_num];
88 PCRE2_SIZE end = ovector[2 * group_num + 1];
89
90 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
91 return NULL; /* Group not matched */
92 }
93
94 /* Allocate and copy substring */
95 size_t len = end - start;
96 char *result = SAFE_MALLOC(len + 1, char *);
97 if (!result) {
98 log_error("failed to allocate %zu bytes", len + 1);
99 return NULL;
100 }
101
102 memcpy(result, subject + start, len);
103 result[len] = '\0';
104
105 return result;
106}
#define SAFE_MALLOC(size, cast)
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604

References log_error, and SAFE_MALLOC.

◆ asciichat_pcre2_extract_group_ptr()

const char * asciichat_pcre2_extract_group_ptr ( pcre2_match_data * match_data,
int group_num,
const char * subject,
size_t * out_len )

Extract numbered capture group as pointer into subject (non-allocating).

Returns a pointer to the matched substring within the original subject string. No memory allocation occurs - the returned pointer is only valid as long as the subject string remains valid.

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
out_lenOutput parameter for substring length
Returns
Pointer to matched substring within subject, or NULL if group not matched

Example:

size_t len;
const char *video_id = asciichat_pcre2_extract_group_ptr(match_data, 1, url,
&len); if (video_id) { printf("Video ID: %.*s\n", (int)len, video_id);
}
const char * asciichat_pcre2_extract_group_ptr(pcre2_match_data *match_data, int group_num, const char *subject, size_t *out_len)
Extract numbered capture group as pointer into subject (non-allocating).
Definition pcre2.c:112

Definition at line 112 of file pcre2.c.

115 {
116 if (!match_data || !subject || !out_len || group_num < 0) {
117 return NULL;
118 }
119
120 /* Get ovector (output vector with match offsets) */
121 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
122 PCRE2_SIZE start = ovector[2 * group_num];
123 PCRE2_SIZE end = ovector[2 * group_num + 1];
124
125 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
126 return NULL; /* Group not matched */
127 }
128
129 *out_len = end - start;
130 return subject + start;
131}

Referenced by stun_servers_parse().

◆ asciichat_pcre2_extract_group_ulong()

bool asciichat_pcre2_extract_group_ulong ( pcre2_match_data * match_data,
int group_num,
const char * subject,
unsigned long * out_value )

Extract numbered capture group and convert to unsigned long.

Extracts a numbered capture group and converts it to an unsigned long integer. Handles extraction and parsing in one step.

Parameters
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_numCapture group number (1-based, 0 = entire match)
subjectOriginal subject string that was matched
out_valueOutput parameter for parsed integer value
Returns
true if extraction and parsing succeeded, false otherwise

Example:

unsigned long component_id;
if (asciichat_pcre2_extract_group_ulong(match_data, 2, line, &component_id))
{ candidate->component_id = (uint32_t)component_id;
}
unsigned int uint32_t
bool asciichat_pcre2_extract_group_ulong(pcre2_match_data *match_data, int group_num, const char *subject, unsigned long *out_value)
Extract numbered capture group and convert to unsigned long.
Definition pcre2.c:136

Definition at line 136 of file pcre2.c.

138 {
139 if (!match_data || !subject || !out_value || group_num < 0) {
140 return false;
141 }
142
143 /* Get ovector (output vector with match offsets) */
144 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
145 PCRE2_SIZE start = ovector[2 * group_num];
146 PCRE2_SIZE end = ovector[2 * group_num + 1];
147
148 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
149 return false; /* Group not matched */
150 }
151
152 /* Extract substring into temporary buffer */
153 size_t len = end - start;
154 if (len == 0 || len > 63) { /* Sanity check for reasonable number length */
155 return false;
156 }
157
159 memcpy(temp, subject + start, len);
160 temp[len] = '\0';
161
162 /* Parse as unsigned long */
163 char *endptr;
164 errno = 0;
165 unsigned long value = strtoul(temp, &endptr, 10);
166
167 if (errno != 0 || endptr == temp || *endptr != '\0') {
168 return false; /* Parse failed */
169 }
170
171 *out_value = value;
172 return true;
173}
#define UTIL_PCRE2_TEMP_BUFFER_SIZE
Definition pcre2.c:8

References UTIL_PCRE2_TEMP_BUFFER_SIZE.

◆ asciichat_pcre2_extract_named_group()

char * asciichat_pcre2_extract_named_group ( pcre2_code * regex,
pcre2_match_data * match_data,
const char * group_name,
const char * subject )

Extract named substring from PCRE2 match data.

Extracts a named capture group from match data and returns an allocated string. The caller is responsible for freeing the returned string with SAFE_FREE().

Parameters
regexCompiled PCRE2 regex with named groups
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_nameName of the capture group to extract
subjectOriginal subject string that was matched
Returns
Allocated string containing the matched substring, or NULL if group not matched

Example:

pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex,
NULL); int rc = pcre2_jit_match(regex, (PCRE2_SPTR)subject, strlen(subject),
0, 0, match_data, NULL); if (rc >= 0) { char *value =
asciichat_pcre2_extract_named_group(regex, match_data, "fieldname", subject);
if (value) {
// Use value...
SAFE_FREE(value);
}
}
pcre2_match_data_free(match_data);
char * asciichat_pcre2_extract_named_group(pcre2_code *regex, pcre2_match_data *match_data, const char *group_name, const char *subject)
Extract named substring from PCRE2 match data.
Definition pcre2.c:37

Extracts a named capture group from match data and returns an allocated string. The caller is responsible for freeing the returned string with SAFE_FREE().

Parameters
regexCompiled PCRE2 regex with named groups
match_dataMatch data from pcre2_match() or pcre2_jit_match()
group_nameName of the capture group to extract
subjectOriginal subject string that was matched
Returns
Allocated string containing the matched substring, or NULL if group not matched

Definition at line 37 of file pcre2.c.

40 {
41 if (!regex || !match_data || !group_name || !subject) {
42 log_error("invalid parameters");
43 return NULL;
44 }
45
46 /* Get group number from name */
47 int group_number =
48 pcre2_substring_number_from_name(regex, (PCRE2_SPTR)group_name);
49 if (group_number < 0) {
50 return NULL; /* Group doesn't exist */
51 }
52
53 /* Get ovector (output vector with match offsets) */
54 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
55 PCRE2_SIZE start = ovector[2 * group_number];
56 PCRE2_SIZE end = ovector[2 * group_number + 1];
57
58 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
59 return NULL; /* Group not matched */
60 }
61
62 /* Allocate and copy substring */
63 size_t len = end - start;
64 char *result = SAFE_MALLOC(len + 1, char *);
65 if (!result) {
66 log_error("failed to allocate %zu bytes", len + 1);
67 return NULL;
68 }
69
70 memcpy(result, subject + start, len);
71 result[len] = '\0';
72
73 return result;
74}

References log_error, and SAFE_MALLOC.

Referenced by crypto_regex_extract_gpg_keygrip(), crypto_regex_extract_pem_base64(), crypto_regex_match_known_hosts(), crypto_regex_match_public_key(), and url_parse().

◆ asciichat_pcre2_match()

int asciichat_pcre2_match ( const pcre2_code * code,
PCRE2_SPTR subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
pcre2_match_data * match_data,
pcre2_match_context * context )

Match with the JIT fast path, using the interpreter when JIT is unavailable. Returns the native PCRE2 capture count or negative PCRE2 error code.

Definition at line 175 of file pcre2.c.

178 {
179 int result = pcre2_jit_match(code, subject, length, startoffset, options,
180 match_data, context);
181 if (result == PCRE2_ERROR_JIT_BADOPTION ||
182 result == PCRE2_ERROR_JIT_STACKLIMIT)
183 return pcre2_match(code, subject, length, startoffset,
184 options | PCRE2_NO_JIT, match_data, context);
185 return result;
186}

Referenced by grep_highlight_colored(), grep_should_output(), stun_servers_parse(), and url_parse().