dymokomi/luce-browser-foundation
Source of release 0.6.1
0.6.1 / src / web_url / pattern_component.lucb
#==============================================================================================
#
# pattern_component - URLPattern: a compiled component (pattern string, RegExp, group names)
#
# DESCRIPTION:
# Port of Libraries/LibURL/Pattern/Component.cpp (lines 1-340) and
# Libraries/LibURL/Pattern/Component.h (lines 1-56). The RegExp is regex::ECMAScriptRegex,
# here ecma_script_regex.lucb over luce-regex.
#
#==============================================================================================
## Ported from Libraries/LibURL/Pattern/Component.cpp:17 URL::Pattern::protocol_component_matches_a_special_scheme.
# https://urlpattern.spec.whatwg.org/#protocol-component-matches-a-special-scheme
pub func pattern_protocol_component_matches_a_special_scheme(protocol_component: Component) -> bool:
# 1. Let special scheme list be a list populated with all of the special schemes.
# 2. For each scheme of special scheme list:
for scheme in special_schemes():
# 1. Let test result be RegExpBuiltinExec(protocol component’s regular expression, scheme).
let test_result = component_matches(&protocol_component, scheme)
# 2. If test result is not null, then return true.
if test_result:
return true
# 3. Return false.
return false
## Ported from Libraries/LibURL/Pattern/Component.cpp:40 URL::Pattern::generate_a_regular_expression_and_name_list.
# https://urlpattern.spec.whatwg.org/#generate-a-regular-expression-and-name-list
func pattern_generate_a_regular_expression_and_name_list(part_list: const ak.Vector[Part]*, options: Options) -> RegularExpressionAndNameList:
# 1. Let result be "^".
var result: ak.StringBuilder
ak.string_builder_construct(&result)
ak.string_builder_append_u8(&result, '^')
# 2. Let name list be a new list.
var name_list: ak.Vector[ak.String]
# 3. For each part of part list:
for index in 0..<ak.vector_size(part_list):
let part = ak.vector_at_const(part_list, index)
# 1. If part’s type is "fixed-text":
if part.type_ == PartType.fixed_text:
# 1. If part’s modifier is "none", then append the result of running escape a regexp string given part’s
# value to the end of result.
if part.modifier == PartModifier.none_:
append_string(&result, pattern_escape_a_regexp_string(part.value))
# 2. Otherwise:
else:
# 1. Append "(?:" to the end of result.
append_literal(&result, "(?:")
# 2. Append the result of running escape a regexp string given part’s value to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.value))
# 3. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 4. Append the result of running convert a modifier to a string given part’s modifier to the end of result.
ak.string_builder_append_string_view(&result, part_convert_modifier_to_string(part.modifier))
# 3. Continue.
continue
# 2. Assert: part’s name is not the empty string.
assert(not ak.string_is_empty(&part.name))
# 3. Append part’s name to name list.
ak.vector_append_t(&name_list, part.name)
# 4. Let regexp value be part’s value.
var regexp_value = part.value
# 5. If part’s type is "segment-wildcard", then set regexp value to the result of running generate a segment wildcard regexp given options.
if part.type_ == PartType.segment_wildcard:
regexp_value = pattern_generate_a_segment_wildcard_regexp(options)
# 6. Otherwise if part’s type is "full-wildcard", then set regexp value to full wildcard regexp value.
elif part.type_ == PartType.full_wildcard:
regexp_value = ak.string_from_utf8(sv(pattern_full_wildcard_regexp_value)) else trap("MUST: String::from_utf8")
# 7. If part’s prefix is the empty string and part’s suffix is the empty string:
if ak.string_is_empty(&part.prefix) and ak.string_is_empty(&part.suffix):
# 1. If part’s modifier is "none" or "optional", then:
if part.modifier == PartModifier.none_ or part.modifier == PartModifier.optional:
# 1. Append "(" to the end of result.
ak.string_builder_append_u8(&result, '(')
# 2. Append regexp value to the end of result.
append_string(&result, regexp_value)
# 3. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 4. Append the result of running convert a modifier to a string given part’s modifier to the end of result.
ak.string_builder_append_string_view(&result, part_convert_modifier_to_string(part.modifier))
# 2. Otherwise:
else:
# 1. Append "((?:" to the end of result.
append_literal(&result, "((?:")
# 2. Append regexp value to the end of result.
append_string(&result, regexp_value)
# 3. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 4. Append the result of running convert a modifier to a string given part’s modifier to the end of result.
ak.string_builder_append_string_view(&result, part_convert_modifier_to_string(part.modifier))
# 5. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 3. Continue.
continue
# 8. If part’s modifier is "none" or "optional":
if part.modifier == PartModifier.none_ or part.modifier == PartModifier.optional:
# 1. Append "(?:" to the end of result.
append_literal(&result, "(?:")
# 2. Append the result of running escape a regexp string given part’s prefix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.prefix))
# 3. Append "(" to the end of result.
ak.string_builder_append_u8(&result, '(')
# 4. Append regexp value to the end of result.
append_string(&result, regexp_value)
# 5. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 6. Append the result of running escape a regexp string given part’s suffix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.suffix))
# 7. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 8. Append the result of running convert a modifier to a string given part’s modifier to the end of result.
ak.string_builder_append_string_view(&result, part_convert_modifier_to_string(part.modifier))
# 9. Continue.
continue
# 9. Assert: part’s modifier is "zero-or-more" or "one-or-more".
assert(part.modifier == PartModifier.zero_or_more or part.modifier == PartModifier.one_or_more)
# 10. Assert: part’s prefix is not the empty string or part’s suffix is not the empty string.
assert(not ak.string_is_empty(&part.prefix) or not ak.string_is_empty(&part.suffix))
# 11. Append "(?:" to the end of result.
append_literal(&result, "(?:")
# 12. Append the result of running escape a regexp string given part’s prefix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.prefix))
# 13. Append "((?:" to the end of result.
append_literal(&result, "((?:")
# 14. Append regexp value to the end of result.
append_string(&result, regexp_value)
# 15. Append ")(?:" to the end of result.
append_literal(&result, ")(?:")
# 16. Append the result of running escape a regexp string given part’s suffix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.suffix))
# 17. Append the result of running escape a regexp string given part’s prefix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.prefix))
# 18. Append "(?:" to the end of result.
append_literal(&result, "(?:")
# 19. Append regexp value to the end of result.
append_string(&result, regexp_value)
# 20. Append "))*)" to the end of result.
append_literal(&result, "))*)")
# 21. Append the result of running escape a regexp string given part’s suffix to the end of result.
append_string(&result, pattern_escape_a_regexp_string(part.suffix))
# 22. Append ")" to the end of result.
ak.string_builder_append_u8(&result, ')')
# 23. If part’s modifier is "zero-or-more" then append "?" to the end of result.
if part.modifier == PartModifier.zero_or_more:
ak.string_builder_append_u8(&result, '?')
# 4. Append "$" to the end of result.
ak.string_builder_append_u8(&result, '$')
# 5. Return (result, name list).
return RegularExpressionAndNameList(regular_expression = ak.string_builder_to_string_without_validation(&result),
name_list = name_list)
## Ported from Libraries/LibURL/Pattern/Component.cpp:218 URL::Pattern::Component::compile.
# https://urlpattern.spec.whatwg.org/#compile-a-component
pub func component_compile(input: ak.Utf8View, encoding_callback: EncodingCallback, options: Options) -> Component!:
# 1. Let part list be the result of running parse a pattern string given input, options, and encoding callback.
let part_list = try pattern_parser_parse(input, options, encoding_callback)
# 2. Let (regular expression string, name list) be the result of running generate a regular expression and name
# list given part list and options.
let generated = pattern_generate_a_regular_expression_and_name_list(&part_list, options)
let regular_expression_string = generated.regular_expression
let name_list = generated.name_list
# 3. Let flags be an empty string.
# NOTE: These flags match the flags for the empty string of the LibJS RegExp implementation.
var flags: RegexEcmaScriptCompileFlags
# 4. If options’s ignore case is true then set flags to "vi".
if options.ignore_case:
flags.unicode_sets = true
flags.ignore_case = true
# 5. Otherwise set flags to "v"
else:
flags.unicode_sets = true
# 6. Let regular expression be RegExpCreate(regular expression string, flags). If this throws an exception, catch
# it, and throw a TypeError.
let regex_or_error = regex_ecma_script_regex_compile(view_of(regular_expression_string), flags) catch failure:
var message: ak.StringBuilder
ak.string_builder_construct(&message)
append_literal(&message, "RegExp compile error: ")
append_literal(&message, failure.message)
try pattern_throw_builder(&message)
# 7. Let pattern string be the result of running generate a pattern string given part list and options.
let pattern_string = pattern_generate_a_pattern_string(&part_list, options)
# 8. Let has regexp groups be false.
var has_regexp_groups = false
# 9. For each part of part list:
for index in 0..<ak.vector_size(&part_list):
# 1. If part’s type is "regexp", then set has regexp groups to true.
if ak.vector_at_const(&part_list, index).type_ == PartType.regexp:
has_regexp_groups = true
break
# 10. Return a new component whose pattern string is pattern string, regular expression is regular expression,
# group name list is name list, and has regexp groups is has regexp groups.
return Component(
pattern_string = pattern_string,
regular_expression = regex_or_error,
group_name_list = name_list,
has_regexp_groups = has_regexp_groups,
)
## Ported from Libraries/LibURL/Pattern/Component.cpp:272 URL::Pattern::Component::execute.
pub func component_execute(this: const Component*, input: ak.String) -> ComponentExecutionResult:
let regular_expression = this.regular_expression else trap("VERIFY: Component without a regular expression")
let utf16_input = ak.utf16_string_from_utf8_string(&input)
let match_result = regex_ecma_script_regex_exec(regular_expression,
ak.utf16_string_base_utf16_view(&utf16_input.utf16_string_base), 0)
var result: ComponentExecutionResult
if match_result != RegexMatchResult.match_:
return result
result.success = true
ak.vector_ensure_capacity(&result.captures, ak.vector_size(&this.group_name_list))
for index in 1..=ak.vector_size(&this.group_name_list):
let start = regex_ecma_script_regex_capture_slot(regular_expression, u32(index * 2))
let end = regex_ecma_script_regex_capture_slot(regular_expression, u32(index * 2 + 1))
if start < 0 or end < 0:
let no_capture: ak.String? = none
ak.vector_append_t(&result.captures, no_capture)
continue
let capture = ak.utf16_string_base_substring_view_usize(&utf16_input.utf16_string_base, (usize)start,
(usize)(end - start))
let captured: ak.String? = ak.utf16_view_to_utf8(&capture) else trap("MUST: Utf16View::to_utf8")
ak.vector_append_t(&result.captures, captured)
return result
## Ported from Libraries/LibURL/Pattern/Component.cpp:297 URL::Pattern::Component::matches.
pub func component_matches(this: const Component*, input: ak.StringView) -> bool:
let regular_expression = this.regular_expression else trap("VERIFY: Component without a regular expression")
let utf16_input = ak.utf16_string_from_utf8_string_view(input)
return regex_ecma_script_regex_test(regular_expression, ak.utf16_string_base_utf16_view(&utf16_input.utf16_string_base),
0) == RegexMatchResult.match_
## Ported from Libraries/LibURL/Pattern/Component.cpp:304 URL::Pattern::Component::create_match_result.
# https://urlpattern.spec.whatwg.org/#create-a-component-match-result
pub func component_create_match_result(this: const Component*, input: ak.String, exec_result: ComponentExecutionResult) -> ComponentResult:
# 1. Let result be a new URLPatternComponentResult.
var result: ComponentResult
# 2. Set result["input"] to input.
result.input = input
# 3. Let groups be a record<USVString, (USVString or undefined)>.
var groups: ak.OrderedHashMap[ak.String, ak.StringOrEmpty, ak.StringTraits]
# 4. Let index be 1.
# 5. While index is less than or equal to component’s group name list’s size:
assert(ak.vector_size(&exec_result.captures) == ak.vector_size(&this.group_name_list))
for index in 1..=ak.vector_size(&this.group_name_list):
# 1. Let name be component’s group name list[index − 1].
let name = *ak.vector_at_const(&this.group_name_list, index - 1)
# 2. Let value be Get(execResult, ToString(index)).
# 3. Set groups[name] to value.
let capture = *ak.vector_at_const(&exec_result.captures, index - 1)
if let value = capture:
_ = ak.ordered_hash_map_set(&groups, name, ak.StringOrEmpty.string(value = value))
else:
_ = ak.ordered_hash_map_set(&groups, name, ak.StringOrEmpty.empty)
# 4. Increment index by 1.
# 6. Set result["groups"] to groups.
result.groups = groups
# 7. Return result.
return result