LibURL/Pattern: Add a URL Pattern 'Part' representation

This commit is contained in:
Shannon Booth 2025-03-17 15:49:52 +13:00 committed by Tim Flynn
parent 8a33c57c1e
commit 88bea4a717
Notes: github-actions[bot] 2025-04-06 12:28:04 +00:00
3 changed files with 144 additions and 0 deletions

View file

@ -10,6 +10,7 @@ set(SOURCES
Pattern/Canonicalization.cpp
Pattern/ConstructorStringParser.cpp
Pattern/Init.cpp
Pattern/Part.cpp
Pattern/Pattern.cpp
Pattern/String.cpp
Pattern/Tokenizer.cpp

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Pattern/Component.h>
#include <LibURL/Pattern/Part.h>
namespace URL::Pattern {
Part::Part(Type type, String value, Modifier modifier)
: type(type)
, value(move(value))
, modifier(modifier)
{
}
Part::Part(Type type, String value, Modifier modifier, String name, String prefix, String suffix)
: type(type)
, value(move(value))
, modifier(modifier)
, name(move(name))
, prefix(move(prefix))
, suffix(move(suffix))
{
}
StringView Part::type_to_string(Part::Type type)
{
switch (type) {
case Type::FixedText:
return "FixedText"sv;
case Type::Regexp:
return "Regexp"sv;
case Type::SegmentWildcard:
return "SegmentWildcard"sv;
case Type::FullWildcard:
return "FullWildcard"sv;
}
VERIFY_NOT_REACHED();
}
// https://urlpattern.spec.whatwg.org/#convert-a-modifier-to-a-string
StringView Part::convert_modifier_to_string(Part::Modifier modifier)
{
// 1. If modifier is "zero-or-more", then return "*".
if (modifier == Modifier::ZeroOrMore)
return "*"sv;
// 2. If modifier is "optional", then return "?".
if (modifier == Modifier::Optional)
return "?"sv;
// 3. If modifier is "one-or-more", then return "+".
if (modifier == Modifier::OneOrMore)
return "+"sv;
// 4. Return the empty string.
return ""sv;
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/String.h>
namespace URL::Pattern {
// https://urlpattern.spec.whatwg.org/#part
struct Part {
// https://urlpattern.spec.whatwg.org/#part-type
enum class Type {
// The part represents a simple fixed text string.
FixedText,
// The part represents a matching group with a custom regular expression.
Regexp,
// The part represents a matching group that matches code points up to the next separator code point. This is
// typically used for a named group like ":foo" that does not have a custom regular expression.
SegmentWildcard,
// The part represents a matching group that greedily matches all code points. This is typically used for
// the "*" wildcard matching group.
FullWildcard,
};
// https://urlpattern.spec.whatwg.org/#part-modifier
enum class Modifier {
// The part does not have a modifier.
None,
// The part has an optional modifier indicated by the U+003F (?) code point.
Optional,
// The part has a "zero or more" modifier indicated by the U+002A (*) code point.
ZeroOrMore,
// The part has a "one or more" modifier indicated by the U+002B (+) code point.
OneOrMore,
};
static StringView convert_modifier_to_string(Modifier);
static StringView type_to_string(Type);
Part(Type, String value, Modifier);
Part(Type, String value, Modifier, String name, String prefix, String suffix);
// https://urlpattern.spec.whatwg.org/#part-type
// A part has an associated type, a string, which must be set upon creation.
Type type {};
// https://urlpattern.spec.whatwg.org/#part-value
// A part has an associated value, a string, which must be set upon creation.
String value;
// https://urlpattern.spec.whatwg.org/#part-modifier
// A part has an associated modifier a string, which must be set upon creation.
Modifier modifier;
// https://urlpattern.spec.whatwg.org/#part-name
// A part has an associated name, a string, initially the empty string.
String name;
// https://urlpattern.spec.whatwg.org/#part-prefix
// A part has an associated prefix, a string, initially the empty string.
String prefix;
// https://urlpattern.spec.whatwg.org/#part-suffix
// A part has an associated suffix, a string, initially the empty string.
String suffix;
};
}