8000 Merge branch 'ada-url:main' into main · plusiv/ada-python@f8f7613 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8f7613

Browse files
authored
Merge branch 'ada-url:main' into main
2 parents b181414 + 0dda1fd commit f8f7613

File tree

5 files changed

+103
-56
lines changed

5 files changed

+103
-56
lines changed

README.rst

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
ada-url
22
========
33

4+
This is ``ada_url``, a Python library for working with URLs based on the ``Ada`` URL
5+
parser.
46

5-
This is ``ada_url``, a Python library for parsing and joining URLs.
7+
* `Documentation <https://ada-url.readthedocs.io>`__
8+
* `Development <https://github.com/ada-url/ada-python/>`__
9+
* `Ada <https://www.ada-url.com/>`__
610

711
Installation
812
------------
@@ -16,30 +20,20 @@ Install from `PyPI <https://pypi.org/project/ada-url/>`__:
1620
Usage examples
1721
--------------
1822

19-
This package exposes a ``URL`` class that is intended to match the one described in the
20-
`WHATWG URL spec <https://url.spec.whatwg.org/#url-class>`__.
23+
Parsing URLs
24+
^^^^^^^^^^^^
2125

22-
.. code-block:: python
23-
24-
>>> from ada_url import URL
25-
>>> URL('https://example.org/path/../file.txt') as urlobj:
26-
>>> urlobj.host = 'example.com'
27-
>>> new_url = urlobj.href
28-
>>> new_url
29-
'https://example.com/file.txt'
30-
31-
It also provides high level functions for parsing and manipulating URLs. Validating
32-
a URL:
26+
The ``URL`` class is intended to match the one described in the
27+
`WHATWG URL spec <https://url.spec.whatwg.org/#url-class>`_:.
3328

3429
.. code-block:: python
3530
36-
>>> from ada_url import check_url
37-
>>> check_url('https://example.org')
38-
True
39-
>>> check_url('http://example:bougus')
40-
False
31+
>>> from ada_url import URL
32+
>>> urlobj = URL('https://example.org/path/../file.txt')
33+
>>> urlobj.href
34+
'https://example.org/path/file.txt'
4135
42-
Parsing a URL:
36+
The ``parse_url`` function returns a dictionary of all URL elements:
4337

4438
.. code-block:: python
4539
@@ -61,19 +55,59 @@ Parsing a URL:
6155
'scheme_type': <SchemeType.HTTPS: 2>
6256
}
6357
64-
Replacing URL components:
58+
Altering URLs
59+
^^^^^^^^^^^^^
60+
61+
Replacing URL components with the ``URL`` class:
6562

6663
.. code-block:: python
6764
65+
>>> from ada_url import URL
66+
>>> urlobj = URL('https://example.org/path/../file.txt')
67+
>>> urlobj.host = 'example.com'
68+
>>> urlobj.href
69+
'https://example.com/path/file.txt'
70+
71+
Replacing URL components with the ``replace_url`` function:
72+
6873
>>> from ada_url import replace_url
69-
>>> ada_url.replace_url('http://example.org:80', protocol='https:')
70-
'https://example.org/'
74+
>>> replace_url('https://example.org/path/../file.txt', host='example.com')
75+
'https://example.com/file.txt'
7176

72-
Joining a URL with a relative fragment:
77+
Search parameters
78+
^^^^^^^^^^^^^^^^^
7379

74-
>>> from ada_url import join_url
75-
>>> join_url('https://example.org/dir/child.txt', '../parent.txt')
76-
'https://example.org/parent.txt'
80+
The ``URLSearchParams`` class is intended to match the one described in the
81+
`WHATWG URL spec <https://url.spec.whatwg.org/#interface-urlsearchparams>`__.
82+
83+
.. code-bl A92E ock:: python
84+
85+
>>> from ada_url import URLSearchParams
86+
>>> obj = URLSearchParams('key1=value1&key2=value2')
87+
>>> list(obj.items())
88+
[('key1', 'value1'), ('key2', 'value2')]
89+
90+
The ``parse_search_params`` function returns a dictionary of search keys mapped to
91+
value lists:
92+
93+
.. code-block:: python
94+
95+
>>> from ada_url import parse_search_params
96+
>>> parse_search_params('key1=value1&key2=value2')
97+
{'key1': ['value1'], 'key2': ['value2']}
98+
99+
Internationalized domain names
100+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101+
102+
The ``idna`` class can encode and decode IDNs:
103+
104+
.. code-block:: python
105+
106+
>>> from ada_url import idna
107+
>>> idna.encode('Bücher.example')
108+
b'xn--bcher-kva.example'
109+
>>> idna.decode(b'xn--bcher-kva.example')
110+
'bücher.example'
77111
78112
WHATWG URL compliance
79113
---------------------
@@ -100,10 +134,3 @@ Contrast that with the Python standard library's ``urlib.parse`` module:
100134
'www.googlé.com'
101135
>>> parsed_url.path
102136
'/./path/../path2/'
103-
104-
More information
105-
----------------
106-
107-
* ``ada-url`` is based on the `Ada <https://www.ada-url.com/>`__ project.
108-
* A full API reference is available at `Read the Docs <https://ada-url.readthedocs.io>`__.
109-
* Source code is available at `GitHub <https://github.com/ada-url/ada-python>`__.

ada_url/ada.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-10-10 12:43:54 -0400. Do not edit! */
1+
/* auto-generated on 2023-10-22 19:50:50 -0400. Do not edit! */
22
/* begin file src/ada.cpp */
33
#include "ada.h"
44
/* begin file src/checkers.cpp */

ada_url/ada.h

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-10-10 12:43:54 -0400. Do not edit! */
1+
/* auto-generated on 2023-10-22 19:50:50 -0400. Do not edit! */
22
/* begin file include/ada.h */
33
/**
44
* @file ada.h
@@ -1505,8 +1505,8 @@ struct url_base {
15051505
* @return On failure, it returns zero.
15061506
* @see https://url.spec.whatwg.org/#host-parsing
15071507
*/
1508-
virtual ada_really_inline size_t
1509-
parse_port(std::string_view view, bool check_trailing_content) noexcept = 0;
1508+
virtual size_t parse_port(std::string_view view,
1509+
bool check_trailing_content) noexcept = 0;
15101510

15111511
virtual ada_really_inline size_t parse_port(std::string_view view) noexcept {
15121512
return this->parse_port(view, false);
@@ -6577,20 +6577,31 @@ inline bool url_aggregator::has_port() const noexcept {
65776577
// is greater than 1, and url's path[0] is the empty string, then append
65786578
// U+002F (/) followed by U+002E (.) to output.
65796579
ada_log("url_aggregator::has_dash_dot");
6580-
// Performance: instead of doing this potentially expensive check, we could
6581-
// just have a boolean value in the structure.
65826580
#if ADA_DEVELOPMENT_CHECKS
6583-
if (components.pathname_start + 1 < buffer.size() &&
6584-
components.pathname_start == components.host_end + 2) {
6585-
ADA_ASSERT_TRUE(buffer[components.host_end] == '/');
6586-
ADA_ASSERT_TRUE(buffer[components.host_end + 1] == '.');
6581+
// If pathname_start and host_end are exactly two characters apart, then we
6582+
// either have a one-digit port such as http://test.com:5?param=1 or else we
6583+
// have a /.: sequence such as "non-spec:/.//". We test that this is the case.
6584+
if (components.pathname_start == components.host_end + 2) {
6585+
ADA_ASSERT_TRUE((buffer[components.host_end] == '/' &&
6586+
buffer[components.host_end + 1] == '.') ||
6587+
(buffer[components.host_end] == ':' &&
6588+
checkers::is_digit(buffer[components.host_end + 1])));
6589+
}
6590+
if (components.pathname_start == components.host_end + 2 &&
6591+
buffer[components.host_end] == '/' &&
6592+
buffer[components.host_end + 1] == '.') {
6593+
ADA_ASSERT_TRUE(components.pathname_start + 1 < buffer.size());
65876594
ADA_ASSERT_TRUE(buffer[components.pathname_start] == '/');
65886595
ADA_ASSERT_TRUE(buffer[components.pathname_start + 1] == '/');
65896596
}
65906597
#endif
6591-
return !has_opaque_path &&
6592-
components.pathname_start == components.host_end + 2 &&
6593-
components.pathname_start + 1 < buffer.size();
6598+
// Performance: it should be uncommon for components.pathname_start ==
6599+
// components.host_end + 2 to be true. So we put this check first in the
6600+
// sequence. Most times, we do not have an opaque path. Checking for '/.' is
6601+
// more expensive, but should be uncommon.
6602+
return components.pathname_start == components.host_end + 2 &&
6603+
!has_opaque_path && buffer[components.host_end] == '/' &&
6604+
buffer[components.host_end + 1] == '.';
65946605
}
65956606

65966607
[[nodiscard]] inline std::string_view url_aggregator::get_href()
@@ -6798,11 +6809,11 @@ struct url_search_params {
67986809
* C++ style conventional iterator support. const only because we
67996810
* do not really want the params to be modified via the iterator.
68006811
*/
6801-
inline const auto begin() const { return params.begin(); }
6802-
inline const auto end() const { return params.end(); }
6803-
inline const auto front() const { return params.front(); }
6804-
inline const auto back() const { return params.back(); }
6805-
inline const auto operator[](size_t index) const { return params[index]; }
6812+
inline auto begin() const { return params.begin(); }
6813+
inline auto end() const { return params.end(); }
6814+
inline auto front() const { return params.front(); }
6815+
inline auto back() const { return params.back(); }
6816+
inline auto operator[](size_t index) const { return params[index]; }
68066817

68076818
private:
68086819
typedef std::pair<std::string, std::string> key_value_pair;
@@ -7077,14 +7088,14 @@ url_search_params_entries_iter::next() {
70777088
#ifndef ADA_ADA_VERSION_H
70787089
#define ADA_ADA_VERSION_H
70797090

7080-
#define ADA_VERSION "2.7.0"
7091+
#define ADA_VERSION "2.7.2"
70817092

70827093
namespace ada {
70837094

70847095
enum {
70857096
ADA_VERSION_MAJOR = 2,
70867097
ADA_VERSION_MINOR = 7,
7087-
ADA_VERSION_REVISION = 0,
7098+
ADA_VERSION_REVISION = 2,
70887099
};
70897100

70907101
} // namespace ada

docs/index.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,22 @@ API Documentation
5353
.. autoclass:: URL(url, base=None)
5454
.. autoclass:: HostType()
5555
.. autoclass:: SchemeType()
56+
57+
----
58+
5659
.. autofunction:: check_url(s)
5760
.. autofunction:: join_url(base_url, s)
5861
.. autofunction:: normalize_url(s)
5962
.. autofunction:: parse_url(s, [attributes])
6063
.. autofunction:: replace_url(s, **kwargs)
64+
65+
----
66+
6167
.. autoclass:: URLSearchParams(params)
6268
.. autoclass:: parse_search_params(s)
63-
.. autoclass:: replace_search_params(s)
69+
.. autoclass:: replace_search_params(s, *args)
70+
71+
----
72+
6473
.. autoclass:: idna
6574

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = ada-url
3-
version = 1.6.0
3+
version = 1.7.0
44
description = 'URL parser and manipulator based on the WHAT WG URL standard'
55
long_description = file: README.rst
66
long_description_content_type = text/x-rst

0 commit comments

Comments
 (0)
0