cid#1607171 Data race condition
[LibreOffice.git] / ucb / source / ucp / inc / urihelper.hxx
blobfa9c7c91815109d8bacb356be6ce5bbd9032c5bc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #pragma once
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 )
46 OUString aFragment;
47 OUString aParams;
48 OUString aURI;
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 );
57 if ( nParams != -1 )
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 )
64 : ( nFragment != -1 )
65 ? rURI.copy( 0, nFragment )
66 : rURI;
68 if ( aFragment.getLength() > 1 )
69 aFragment =
70 rtl::Uri::encode( aFragment,
71 rtl_UriCharClassUric,
72 rtl_UriEncodeKeepEscapes, /* #i81690# */
73 RTL_TEXTENCODING_UTF8 );
75 if ( aParams.getLength() > 1 )
76 aParams =
77 rtl::Uri::encode( aParams,
78 rtl_UriCharClassUric,
79 rtl_UriEncodeKeepEscapes, /* #i81690# */
80 RTL_TEXTENCODING_UTF8 );
82 OUStringBuffer aResult(256);
83 sal_Int32 nIndex = 0;
86 aResult.append(
87 rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
88 rtl_UriCharClassPchar,
89 rtl_UriEncodeKeepEscapes, /* #i81690# */
90 RTL_TEXTENCODING_UTF8 ) );
91 if ( nIndex >= 0 )
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();
105 } // namespace
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */