1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_URI_HXX
25 #define INCLUDED_RTL_URI_HXX
27 #include "rtl/malformeduriexception.hxx"
29 #include "rtl/textenc.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
33 #if defined LIBO_INTERNAL_ONLY
37 #include <string_view>
38 #include "config_global.h"
43 /** A wrapper around the C functions from <rtl/uri.h>.
48 /** A wrapper around rtl_uriEncode() from <rtl/uri.h> (see there), using
49 an array of 128 booleans as char class.
51 static inline rtl::OUString
encode(rtl::OUString
const & rText
,
52 sal_Bool
const * pCharClass
,
53 rtl_UriEncodeMechanism eMechanism
,
54 rtl_TextEncoding eCharset
);
56 /** A wrapper around rtl_uriEncode() from <rtl/uri.h> (see there), using
57 a predefined rtl_UriCharClass enumeration member.
59 static inline rtl::OUString
encode(rtl::OUString
const & rText
,
60 rtl_UriCharClass eCharClass
,
61 rtl_UriEncodeMechanism eMechanism
,
62 rtl_TextEncoding eCharset
);
64 /** A wrapper around rtl_uriDecode() from <rtl/uri.h> (see there).
66 static inline rtl::OUString
decode(rtl::OUString
const & rText
,
67 rtl_UriDecodeMechanism eMechanism
,
68 rtl_TextEncoding eCharset
);
70 /** A wrapper around rtl_uriConvertRelToAbs() from <rtl/uri.h> (see there).
72 @exception MalformedUriException
73 Thrown in case rtl_uriConvertRelToAbs() signals an exception due to a
76 static inline rtl::OUString
convertRelToAbs(
77 rtl::OUString
const & rBaseUriRef
, rtl::OUString
const & rRelUriRef
);
80 Uri() SAL_DELETED_FUNCTION
;
82 Uri(Uri
&) SAL_DELETED_FUNCTION
;
84 ~Uri() SAL_DELETED_FUNCTION
;
86 void operator =(Uri
) SAL_DELETED_FUNCTION
;
89 inline rtl::OUString
Uri::encode(rtl::OUString
const & rText
,
90 sal_Bool
const * pCharClass
,
91 rtl_UriEncodeMechanism eMechanism
,
92 rtl_TextEncoding eCharset
)
94 rtl::OUString aResult
;
95 rtl_uriEncode(rText
.pData
,
103 inline rtl::OUString
Uri::encode(rtl::OUString
const & rText
,
104 rtl_UriCharClass eCharClass
,
105 rtl_UriEncodeMechanism eMechanism
,
106 rtl_TextEncoding eCharset
)
108 rtl::OUString aResult
;
109 rtl_uriEncode(rText
.pData
,
110 rtl_getUriCharClass(eCharClass
),
117 inline rtl::OUString
Uri::decode(rtl::OUString
const & rText
,
118 rtl_UriDecodeMechanism eMechanism
,
119 rtl_TextEncoding eCharset
)
121 rtl::OUString aResult
;
122 rtl_uriDecode(rText
.pData
,
129 inline rtl::OUString
Uri::convertRelToAbs(rtl::OUString
const & rBaseUriRef
,
130 rtl::OUString
const & rRelUriRef
)
132 rtl::OUString aResult
;
133 rtl::OUString aException
;
134 if (!rtl_uriConvertRelToAbs(
136 rRelUriRef
.pData
, &aResult
.pData
,
138 throw MalformedUriException(aException
);
142 #if defined LIBO_INTERNAL_ONLY
144 constexpr std::size_t UriCharClassSize
= 128;
146 // Create a char class (for use with rtl_uriEncode and rtl::Uri::encode), represented as a
147 // compile-time std::array, from an UTF-8 string literal.
149 // The given `unencoded` lists each ASCII character once that shall not be encoded. (It uses an
150 // UTF-8 string type to emphasize that its characters' values are always interpreted as ASCII
152 #if HAVE_CPP_CONSTEVAL
157 auto createUriCharClass(
158 #if defined __cpp_lib_char8_t
165 std::array
<sal_Bool
, UriCharClassSize
> a
= {};
166 for (auto c
: unencoded
) {
167 assert(!a
[c
]); // would presumably indicate a typo in the `unencoded` argument
177 #endif // INCLUDED_RTL_URI_HXX
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */