ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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, &error, &offset, NULL);
20}

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)
Definition common.h:377
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:73

Definition at line 73 of file pcre2.c.

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

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:104

Definition at line 104 of file pcre2.c.

105 {
106 if (!match_data || !subject || !out_len || group_num < 0) {
107 return NULL;
108 }
109
110 /* Get ovector (output vector with match offsets) */
111 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
112 PCRE2_SIZE start = ovector[2 * group_num];
113 PCRE2_SIZE end = ovector[2 * group_num + 1];
114
115 if (start == PCRE2_UNSET || end == PCRE2_UNSET) {
116 return NULL; /* Group not matched */
117 }
118
119 *out_len = end - start;
120 return subject + start;
121}

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
Definition common.h:62
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:126

Definition at line 126 of file pcre2.c.

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

References errno, and 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:34

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 34 of file pcre2.c.

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

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 164 of file pcre2.c.

165 {
166 int result = pcre2_jit_match(code, subject, length, startoffset, options, match_data, context);
167 if (result == PCRE2_ERROR_JIT_BADOPTION || result == PCRE2_ERROR_JIT_STACKLIMIT)
168 return pcre2_match(code, subject, length, startoffset, options | PCRE2_NO_JIT, match_data, context);
169 return result;
170}

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