OInterfaceContainerHelper3 needs to be thread-safe
[LibreOffice.git] / svl / source / misc / sharedstring.cxx
blob9aff3eb0eab58e2037783cecfc725c6ebce57b90
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/.
8 */
10 #include <svl/sharedstring.hxx>
12 namespace svl {
14 SharedString SharedString::getEmptyString()
16 // unicode string array for empty string is globally shared in OUString.
17 // Let's take advantage of that.
18 rtl_uString* pData = nullptr;
19 rtl_uString_new(&pData);
20 return SharedString(pData, pData);
23 SharedString::SharedString() : mpData(nullptr), mpDataIgnoreCase(nullptr) {}
25 SharedString::SharedString( rtl_uString* pData, rtl_uString* pDataIgnoreCase ) :
26 mpData(pData), mpDataIgnoreCase(pDataIgnoreCase)
28 if (mpData)
29 rtl_uString_acquire(mpData);
30 if (mpDataIgnoreCase)
31 rtl_uString_acquire(mpDataIgnoreCase);
34 SharedString::SharedString( const OUString& rStr ) : mpData(rStr.pData), mpDataIgnoreCase(nullptr)
36 rtl_uString_acquire(mpData);
39 SharedString::SharedString( const SharedString& r ) : mpData(r.mpData), mpDataIgnoreCase(r.mpDataIgnoreCase)
41 if (mpData)
42 rtl_uString_acquire(mpData);
43 if (mpDataIgnoreCase)
44 rtl_uString_acquire(mpDataIgnoreCase);
47 SharedString::SharedString(SharedString&& r) noexcept : mpData(r.mpData), mpDataIgnoreCase(r.mpDataIgnoreCase)
49 r.mpData = nullptr;
50 r.mpDataIgnoreCase = nullptr;
53 SharedString::~SharedString()
55 if (mpData)
56 rtl_uString_release(mpData);
57 if (mpDataIgnoreCase)
58 rtl_uString_release(mpDataIgnoreCase);
61 SharedString& SharedString::operator= ( const SharedString& r )
63 if(this == &r)
64 return *this;
66 if (mpData)
67 rtl_uString_release(mpData);
68 if (mpDataIgnoreCase)
69 rtl_uString_release(mpDataIgnoreCase);
71 mpData = r.mpData;
72 mpDataIgnoreCase = r.mpDataIgnoreCase;
74 if (mpData)
75 rtl_uString_acquire(mpData);
76 if (mpDataIgnoreCase)
77 rtl_uString_acquire(mpDataIgnoreCase);
79 return *this;
82 SharedString& SharedString::operator=(SharedString&& r) noexcept
84 if (mpData)
85 rtl_uString_release(mpData);
86 if (mpDataIgnoreCase)
87 rtl_uString_release(mpDataIgnoreCase);
89 mpData = r.mpData;
90 mpDataIgnoreCase = r.mpDataIgnoreCase;
92 r.mpData = nullptr;
93 r.mpDataIgnoreCase = nullptr;
95 return *this;
98 bool SharedString::operator== ( const SharedString& r ) const
100 // Compare only the original (not case-folded) string.
102 if (mpData == r.mpData)
103 return true;
105 if (mpData)
107 if (!r.mpData)
108 return false;
110 if (mpData->length != r.mpData->length)
111 return false;
113 return rtl_ustr_reverseCompare_WithLength(mpData->buffer, mpData->length, r.mpData->buffer, r.mpData->length) == 0;
116 return !r.mpData;
119 bool SharedString::operator!= ( const SharedString& r ) const
121 return !operator== (r);
124 OUString SharedString::getString() const
126 return mpData ? OUString(mpData) : OUString();
129 rtl_uString* SharedString::getData()
131 return mpData;
134 const rtl_uString* SharedString::getData() const
136 return mpData;
139 rtl_uString* SharedString::getDataIgnoreCase()
141 return mpDataIgnoreCase;
144 const rtl_uString* SharedString::getDataIgnoreCase() const
146 return mpDataIgnoreCase;
149 bool SharedString::isValid() const
151 return mpData != nullptr;
154 bool SharedString::isEmpty() const
156 return mpData == nullptr || mpData->length == 0;
159 sal_Int32 SharedString::getLength() const
161 return mpData ? mpData->length : 0;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */