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 .
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <rtl/uri.hxx>
27 namespace ucb_impl::urihelper
{
29 inline OUString
encodeSegment( const OUString
& rSegment
)
31 return rtl::Uri::encode( rSegment
,
32 rtl_UriCharClassPchar
,
33 rtl_UriEncodeIgnoreEscapes
,
34 RTL_TEXTENCODING_UTF8
);
37 inline OUString
decodeSegment( const OUString
& rSegment
)
39 return rtl::Uri::decode( rSegment
,
40 rtl_UriDecodeWithCharset
,
41 RTL_TEXTENCODING_UTF8
);
44 inline OUString
encodeURI( const OUString
& rURI
)
50 sal_Int32 nFragment
= rURI
.lastIndexOf( u
'#' );
51 if ( nFragment
!= -1 )
52 aFragment
= rURI
.copy( nFragment
+ 1 );
54 sal_Int32 nParams
= ( nFragment
== -1 )
55 ? rURI
.lastIndexOf( u
'?' )
56 : rURI
.lastIndexOf( u
'?', nFragment
);
58 aParams
= ( nFragment
== -1 )
59 ? rURI
.copy( nParams
+ 1 )
60 : rURI
.copy( nParams
+ 1, nFragment
- nParams
- 1 );
62 aURI
= ( nParams
!= -1 )
63 ? rURI
.copy( 0, nParams
)
65 ? rURI
.copy( 0, nFragment
)
68 if ( aFragment
.getLength() > 1 )
70 rtl::Uri::encode( aFragment
,
72 rtl_UriEncodeKeepEscapes
, /* #i81690# */
73 RTL_TEXTENCODING_UTF8
);
75 if ( aParams
.getLength() > 1 )
77 rtl::Uri::encode( aParams
,
79 rtl_UriEncodeKeepEscapes
, /* #i81690# */
80 RTL_TEXTENCODING_UTF8
);
82 OUStringBuffer
aResult(256);
87 rtl::Uri::encode( aURI
.getToken( 0, '/', nIndex
),
88 rtl_UriCharClassPchar
,
89 rtl_UriEncodeKeepEscapes
, /* #i81690# */
90 RTL_TEXTENCODING_UTF8
) );
92 aResult
.append( u
'/' );
94 while ( nIndex
>= 0 );
96 if ( !aParams
.isEmpty() )
97 aResult
.append( u
"?" + aParams
);
99 if ( !aFragment
.isEmpty() )
100 aResult
.append( u
"#" + aFragment
);
102 return aResult
.makeStringAndClear();
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */