Skip to content

Commit a9114be

Browse files
MaxWipfliawesomekling
authored andcommitted
AK: Use correct constness in URL class methods
This changes the URL class to use the correct constness for getters, setters and other methods. It also changes the entire class to use east const style.
1 parent f3fda59 commit a9114be

File tree

2 files changed

+65
-77
lines changed

2 files changed

+65
-77
lines changed

AK/URL.cpp

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ constexpr bool is_ascii_hex_digit(u32 code_point)
3535
}
3636

3737
// FIXME: It could make sense to force users of URL to use URLParser::parse() explicitly instead of using a constructor.
38-
URL::URL(const StringView& string)
38+
URL::URL(StringView const& string)
3939
: URL(URLParser::parse({}, string))
4040
{
4141
if constexpr (URL_PARSER_DEBUG) {
@@ -58,39 +58,39 @@ String URL::path() const
5858
return builder.to_string();
5959
}
6060

61-
URL URL::complete_url(const String& string) const
61+
URL URL::complete_url(String const& string) const
6262
{
6363
if (!is_valid())
6464
return {};
6565

6666
return URLParser::parse({}, string, this);
6767
}
6868

69-
void URL::set_scheme(const String& scheme)
69+
void URL::set_scheme(String scheme)
7070
{
71-
m_scheme = scheme;
71+
m_scheme = move(scheme);
7272
m_valid = compute_validity();
7373
}
7474

75-
void URL::set_username(const String& username)
75+
void URL::set_username(String username)
7676
{
77-
m_username = username;
77+
m_username = move(username);
7878
m_valid = compute_validity();
7979
}
8080

81-
void URL::set_password(const String& password)
81+
void URL::set_password(String password)
8282
{
83-
m_password = password;
83+
m_password = move(password);
8484
m_valid = compute_validity();
8585
}
8686

87-
void URL::set_host(const String& host)
87+
void URL::set_host(String host)
8888
{
89-
m_host = host;
89+
m_host = move(host);
9090
m_valid = compute_validity();
9191
}
9292

93-
void URL::set_port(const u16 port)
93+
void URL::set_port(u16 port)
9494
{
9595
if (port == default_port_for_scheme(m_scheme)) {
9696
m_port = 0;
@@ -100,20 +100,20 @@ void URL::set_port(const u16 port)
100100
m_valid = compute_validity();
101101
}
102102

103-
void URL::set_paths(const Vector<String>& paths)
103+
void URL::set_paths(Vector<String> paths)
104104
{
105-
m_paths = paths;
105+
m_paths = move(paths);
106106
m_valid = compute_validity();
107107
}
108108

109-
void URL::set_query(const String& query)
109+
void URL::set_query(String query)
110110
{
111-
m_query = query;
111+
m_query = move(query);
112112
}
113113

114-
void URL::set_fragment(const String& fragment)
114+
void URL::set_fragment(String fragment)
115115
{
116-
m_fragment = fragment;
116+
m_fragment = move(fragment);
117117
}
118118

119119
// FIXME: This is by no means complete.
@@ -154,12 +154,12 @@ bool URL::compute_validity() const
154154
return true;
155155
}
156156

157-
bool URL::scheme_requires_port(const StringView& scheme)
157+
bool URL::scheme_requires_port(StringView const& scheme)
158158
{
159159
return (default_port_for_scheme(scheme) != 0);
160160
}
161161

162-
u16 URL::default_port_for_scheme(const StringView& scheme)
162+
u16 URL::default_port_for_scheme(StringView const& scheme)
163163
{
164164
if (scheme == "http")
165165
return 80;
@@ -178,7 +178,7 @@ u16 URL::default_port_for_scheme(const StringView& scheme)
178178
return 0;
179179
}
180180

181-
URL URL::create_with_file_scheme(const String& path, const String& fragment, const String& hostname)
181+
URL URL::create_with_file_scheme(String const& path, String const& fragment, String const& hostname)
182182
{
183183
LexicalPath lexical_path(path);
184184
if (!lexical_path.is_valid() || !lexical_path.is_absolute())
@@ -197,7 +197,7 @@ URL URL::create_with_file_scheme(const String& path, const String& fragment, con
197197
return url;
198198
}
199199

200-
URL URL::create_with_url_or_path(const String& url_or_path)
200+
URL URL::create_with_url_or_path(String const& url_or_path)
201201
{
202202
URL url = url_or_path;
203203
if (url.is_valid())
@@ -207,20 +207,8 @@ URL URL::create_with_url_or_path(const String& url_or_path)
207207
return URL::create_with_file_scheme(path);
208208
}
209209

210-
URL URL::create_with_data(const StringView& mime_type, const StringView& payload, bool is_base64)
211-
{
212-
URL url;
213-
url.set_scheme("data");
214-
url.m_valid = true;
215-
url.m_data_payload = payload;
216-
url.m_data_mime_type = mime_type;
217-
url.m_data_payload_is_base64 = is_base64;
218-
219-
return url;
220-
}
221-
222210
// https://url.spec.whatwg.org/#special-scheme
223-
bool URL::is_special_scheme(const StringView& scheme)
211+
bool URL::is_special_scheme(StringView const& scheme)
224212
{
225213
return scheme.is_one_of("ftp", "file", "http", "https", "ws", "wss");
226214
}
@@ -337,7 +325,7 @@ String URL::serialize_for_display() const
337325
return builder.to_string();
338326
}
339327

340-
bool URL::equals(const URL& other, ExcludeFragment exclude_fragments) const
328+
bool URL::equals(URL const& other, ExcludeFragment exclude_fragments) const
341329
{
342330
if (!m_valid || !other.m_valid)
343331
return false;
@@ -404,7 +392,7 @@ void URL::append_percent_encoded_if_necessary(StringBuilder& builder, u32 code_p
404392
builder.append_code_point(code_point);
405393
}
406394

407-
String URL::percent_encode(const StringView& input, URL::PercentEncodeSet set)
395+
String URL::percent_encode(StringView const& input, URL::PercentEncodeSet set)
408396
{
409397
StringBuilder builder;
410398
for (auto code_point : Utf8View(input)) {
@@ -424,7 +412,7 @@ constexpr u8 parse_hex_digit(u8 digit)
424412
VERIFY_NOT_REACHED();
425413
}
426414

427-
String URL::percent_decode(const StringView& input)
415+
String URL::percent_decode(StringView const& input)
428416
{
429417
if (!input.contains('%'))
430418
return input;

AK/URL.h

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,43 @@ class URL {
3636
};
3737

3838
URL() = default;
39-
URL(const StringView&);
40-
URL(const char* string)
39+
URL(StringView const&);
40+
URL(char const* string)
4141
: URL(StringView(string))
4242
{
4343
}
44-
URL(const String& string)
44+
URL(String const& string)
4545
: URL(string.view())
4646
{
4747
}
4848

49-
bool is_valid() const { return m_valid; }
49+
bool const& is_valid() const { return m_valid; }
5050

51-
String scheme() const { return m_scheme; }
52-
String protocol() const { return m_scheme; }
53-
String username() const { return m_username; }
54-
String password() const { return m_password; }
55-
String host() const { return m_host; }
56-
const Vector<String>& paths() const { return m_paths; }
57-
String query() const { return m_query; }
58-
String fragment() const { return m_fragment; }
51+
String const& scheme() const { return m_scheme; }
52+
String const& protocol() const { return m_scheme; }
53+
String const& username() const { return m_username; }
54+
String const& password() const { return m_password; }
55+
String const& host() const { return m_host; }
56+
Vector<String> const& paths() const { return m_paths; }
57+
String const& query() const { return m_query; }
58+
String const& fragment() const { return m_fragment; }
5959
u16 port() const { return m_port ? m_port : default_port_for_scheme(m_scheme); }
60-
bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; }
60+
bool const& cannot_be_a_base_url() const { return m_cannot_be_a_base_url; }
6161

6262
bool includes_credentials() const { return !m_username.is_empty() || !m_password.is_empty(); }
6363
bool is_special() const { return is_special_scheme(m_scheme); }
6464

65-
void set_scheme(const String&);
66-
void set_protocol(const String& protocol) { set_scheme(protocol); }
67-
void set_username(const String&);
68-
void set_password(const String&);
69-
void set_host(const String&);
70-
void set_port(const u16);
71-
void set_paths(const Vector<String>&);
72-
void set_query(const String&);
73-
void set_fragment(const String&);
74-
void set_cannot_be_a_base_url(const bool value) { m_cannot_be_a_base_url = value; }
75-
void append_path(const String& path) { m_paths.append(path); }
65+
void set_scheme(String);
66+
void set_protocol(String protocol) { set_scheme(move(protocol)); }
67+
void set_username(String);
68+
void set_password(String);
69+
void set_host(String);
70+
void set_port(u16);
71+
void set_paths(Vector<String>);
72+
void set_query(String);
73+
void set_fragment(String);
74+
void set_cannot_be_a_base_url(bool value) { m_cannot_be_a_base_url = value; }
75+
void append_path(String path) { m_paths.append(path); }
7676

7777
String path() const;
7878
String basename() const;
@@ -83,27 +83,27 @@ class URL {
8383
String to_string() const { return serialize(); }
8484
String to_string_encoded() const { return serialize(); }
8585

86-
bool equals(const URL& other, ExcludeFragment = ExcludeFragment::No) const;
86+
bool equals(URL const& other, ExcludeFragment = ExcludeFragment::No) const;
8787

88-
URL complete_url(const String&) const;
88+
URL complete_url(String const&) const;
8989

9090
bool data_payload_is_base64() const { return m_data_payload_is_base64; }
91-
const String& data_mime_type() const { return m_data_mime_type; }
92-
const String& data_payload() const { return m_data_payload; }
91+
String const& data_mime_type() const { return m_data_mime_type; }
92+
String const& data_payload() const { return m_data_payload; }
9393

94-
static URL create_with_url_or_path(const String&);
95-
static URL create_with_file_scheme(const String& path, const String& fragment = {}, const String& hostname = {});
96-
static URL create_with_file_protocol(const String& path, const String& fragment = {}) { return create_with_file_scheme(path, fragment); }
97-
static URL create_with_data(const StringView& mime_type, const StringView& payload, bool is_base64 = false);
94+
static URL create_with_url_or_path(String const&);
95+
static URL create_with_file_scheme(String const& path, String const& fragment = {}, String const& hostname = {});
96+
static URL create_with_file_protocol(String const& path, String const& fragment = {}) { return create_with_file_scheme(path, fragment); }
97+
static URL create_with_data(String mime_type, String payload, bool is_base64 = false) { return URL(move(mime_type), move(payload), is_base64); };
9898

99-
static bool scheme_requires_port(const StringView&);
100-
static u16 default_port_for_scheme(const StringView&);
101-
static bool is_special_scheme(const StringView&);
99+
static bool scheme_requires_port(StringView const&);
100+
static u16 default_port_for_scheme(StringView const&);
101+
static bool is_special_scheme(StringView const&);
102102

103-
static String percent_encode(const StringView& input, PercentEncodeSet set = PercentEncodeSet::Userinfo);
104-
static String percent_decode(const StringView& input);
103+
static String percent_encode(StringView const& input, PercentEncodeSet set = PercentEncodeSet::Userinfo);
104+
static String percent_decode(StringView const& input);
105105

106-
bool operator==(const URL& other) const
106+
bool operator==(URL const& other) const
107107
{
108108
if (this == &other)
109109
return true;
@@ -148,15 +148,15 @@ class URL {
148148

149149
template<>
150150
struct Formatter<URL> : Formatter<StringView> {
151-
void format(FormatBuilder& builder, const URL& value)
151+
void format(FormatBuilder& builder, URL const& value)
152152
{
153153
Formatter<StringView>::format(builder, value.serialize());
154154
}
155155
};
156156

157157
template<>
158158
struct Traits<URL> : public GenericTraits<URL> {
159-
static unsigned hash(const URL& url) { return url.to_string().hash(); }
159+
static unsigned hash(URL const& url) { return url.to_string().hash(); }
160160
};
161161

162162
}

0 commit comments

Comments
 (0)