MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / RegExCreateMatchArray

Function RegExCreateMatchArray

source/script2.cpp:11693–11733  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11691
11692
11693ResultType RegExCreateMatchArray(LPCTSTR haystack, pcret *re, pcret_extra *extra, int *offset, int pattern_count, int captured_pattern_count, IObject *&match_object)
11694{
11695 // For lookup performance, create a table of subpattern names indexed by subpattern number.
11696 LPCTSTR *subpat_name = NULL; // Set default as "no subpattern names present or available".
11697 bool allow_dupe_subpat_names = false; // Set default.
11698 LPCTSTR name_table;
11699 int name_count, name_entry_size;
11700 if ( !pcret_fullinfo(re, extra, PCRE_INFO_NAMECOUNT, &name_count) // Success. Fix for v1.0.45.01: Don't check captured_pattern_count>=0 because PCRE_ERROR_NOMATCH can still have named patterns!
11701 && name_count // There's at least one named subpattern. Relies on short-circuit boolean order.
11702 && !pcret_fullinfo(re, extra, PCRE_INFO_NAMETABLE, &name_table) // Success.
11703 && !pcret_fullinfo(re, extra, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size) ) // Success.
11704 {
11705 int pcre_options;
11706 if (!pcret_fullinfo(re, extra, PCRE_INFO_OPTIONS, &pcre_options)) // Success.
11707 allow_dupe_subpat_names = pcre_options & PCRE_DUPNAMES;
11708 // For indexing simplicity, also include an entry for the main/entire pattern at index 0 even though
11709 // it's never used because the entire pattern can't have a name without enclosing it in parentheses
11710 // (in which case it's not the entire pattern anymore, but in fact subpattern #1).
11711 size_t subpat_array_size = pattern_count * sizeof(LPCSTR);
11712 subpat_name = (LPCTSTR *)_alloca(subpat_array_size); // See other use of _alloca() above for reasons why it's used.
11713 ZeroMemory(subpat_name, subpat_array_size); // Set default for each index to be "no name corresponds to this subpattern number".
11714 for (int i = 0; i < name_count; ++i, name_table += name_entry_size)
11715 {
11716 // Below converts first two bytes of each name-table entry into the pattern number (it might be
11717 // possible to simplify this, but I'm not sure if big vs. little-endian will ever be a concern).
11718#ifdef UNICODE
11719 subpat_name[name_table[0]] = name_table + 1;
11720#else
11721 subpat_name[(name_table[0] << 8) + name_table[1]] = name_table + 2; // For indexing simplicity, subpat_name[0] is for the main/entire pattern though it is never actually used for that because it can't be named without being enclosed in parentheses (in which case it becomes a subpattern).
11722#endif
11723 // For simplicity and unlike PHP, IsNumeric() isn't called to forbid numeric subpattern names.
11724 // It seems the worst than could happen if it is numeric is that it would overlap/overwrite some of
11725 // the numerically-indexed elements in the output-array. Seems pretty harmless given the rarity.
11726 }
11727 }
11728 //else one of the pcre_fullinfo() calls may have failed. The PCRE docs indicate that this realistically never
11729 // happens unless bad inputs were given. So due to rarity, just leave subpat_name==NULL; i.e. "no named subpatterns".
11730
11731 LPTSTR mark = (extra->flags & PCRE_EXTRA_MARK) ? (LPTSTR)*extra->mark : NULL;
11732 return RegExMatchObject::Create(haystack, offset, subpat_name, pattern_count, captured_pattern_count, mark, match_object);
11733}
11734
11735
11736ResultType RegExMatchObject::Create(LPCTSTR aHaystack, int *aOffset, LPCTSTR *aPatternName

Callers 2

RegExCalloutFunction · 0.85
BIF_DECLFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected