Skip to content

Commit 5932440

Browse files
committed
uri: Add has_text_range() helper to uri_parser_rfc3986.c
1 parent f734e8a commit 5932440

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ext/uri/uri_parser_rfc3986.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ static inline size_t get_text_range_length(const UriTextRangeA *range)
6464
return range->afterLast - range->first;
6565
}
6666

67+
static inline bool has_text_range(const UriTextRangeA *range)
68+
{
69+
return range->first != NULL && range->afterLast != NULL;
70+
}
71+
6772
ZEND_ATTRIBUTE_NONNULL static void copy_uri(UriUriA *new_uriparser_uri, const UriUriA *uriparser_uri)
6873
{
6974
int result = uriCopyUriMmA(new_uriparser_uri, uriparser_uri, mm);
@@ -111,7 +116,7 @@ ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_rfc3986_userinfo_read(const ur
111116
{
112117
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
113118

114-
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
119+
if (has_text_range(&uriparser_uri->userInfo)) {
115120
ZVAL_STRINGL(retval, uriparser_uri->userInfo.first, get_text_range_length(&uriparser_uri->userInfo));
116121
} else {
117122
ZVAL_NULL(retval);
@@ -124,7 +129,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_username_read(c
124129
{
125130
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
126131

127-
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
132+
if (has_text_range(&uriparser_uri->userInfo)) {
128133
size_t length = get_text_range_length(&uriparser_uri->userInfo);
129134
const char *c = memchr(uriparser_uri->userInfo.first, ':', length);
130135

@@ -146,7 +151,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_password_read(c
146151
{
147152
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
148153

149-
if (uriparser_uri->userInfo.first != NULL && uriparser_uri->userInfo.afterLast != NULL) {
154+
if (has_text_range(&uriparser_uri->userInfo)) {
150155
const char *c = memchr(uriparser_uri->userInfo.first, ':', get_text_range_length(&uriparser_uri->userInfo));
151156

152157
if (c != NULL && uriparser_uri->userInfo.afterLast - c - 1 > 0) {
@@ -165,7 +170,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_host_read(const
165170
{
166171
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
167172

168-
if (uriparser_uri->hostText.first != NULL && uriparser_uri->hostText.afterLast != NULL) {
173+
if (has_text_range(&uriparser_uri->hostText)) {
169174
if (uriparser_uri->hostData.ip6 != NULL || uriparser_uri->hostData.ipFuture.first != NULL) {
170175
/* the textual representation of the host is always accessible in the .hostText field no matter what the host is */
171176
smart_str host_str = {0};
@@ -200,7 +205,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_port_read(const
200205
{
201206
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
202207

203-
if (uriparser_uri->portText.first != NULL && uriparser_uri->portText.afterLast != NULL) {
208+
if (has_text_range(&uriparser_uri->portText)) {
204209
ZVAL_LONG(retval, str_to_int(uriparser_uri->portText.first, get_text_range_length(&uriparser_uri->portText)));
205210
} else {
206211
ZVAL_NULL(retval);
@@ -241,7 +246,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_query_read(cons
241246
{
242247
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
243248

244-
if (uriparser_uri->query.first != NULL && uriparser_uri->query.afterLast != NULL) {
249+
if (has_text_range(&uriparser_uri->query)) {
245250
ZVAL_STRINGL(retval, uriparser_uri->query.first, get_text_range_length(&uriparser_uri->query));
246251
} else {
247252
ZVAL_NULL(retval);
@@ -254,7 +259,7 @@ ZEND_ATTRIBUTE_NONNULL static zend_result php_uri_parser_rfc3986_fragment_read(c
254259
{
255260
const UriUriA *uriparser_uri = get_uri_for_reading(internal_uri->uri, read_mode);
256261

257-
if (uriparser_uri->fragment.first != NULL && uriparser_uri->fragment.afterLast != NULL) {
262+
if (has_text_range(&uriparser_uri->fragment)) {
258263
ZVAL_STRINGL(retval, uriparser_uri->fragment.first, get_text_range_length(&uriparser_uri->fragment));
259264
} else {
260265
ZVAL_NULL(retval);

0 commit comments

Comments
 (0)