Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / core / xmltkmap.cxx
blob5e95afc44296e24375b01a709545d5cb98b5b885
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 #include <rtl/ustring.hxx>
21 #include <xmloff/xmltkmap.hxx>
22 #include <xmloff/xmltoken.hxx>
23 #include <o3tl/hash_combine.hxx>
25 #include <unordered_map>
26 #include <utility>
28 using namespace ::xmloff::token;
30 class SvXMLTokenMap_Impl
32 private:
33 struct PairHash
35 std::size_t operator()(const std::pair<sal_uInt16,OUString> &pair) const
37 std::size_t seed = 0;
38 o3tl::hash_combine(seed, pair.first);
39 o3tl::hash_combine(seed, pair.second.hashCode());
40 return seed;
43 std::unordered_map< std::pair<sal_uInt16, OUString>,
44 sal_uInt16, PairHash> m_aPrefixAndNameToTokenMap;
46 public:
47 void insert( const SvXMLTokenMapEntry& rEntry );
48 sal_uInt16 get( sal_uInt16 nKeyPrefix, const OUString& rLName ) const;
51 void SvXMLTokenMap_Impl::insert( const SvXMLTokenMapEntry& rEntry )
53 bool inserted = m_aPrefixAndNameToTokenMap.insert( std::make_pair( std::make_pair( rEntry.nPrefixKey,
54 GetXMLToken( rEntry.eLocalName ) ), rEntry.nToken ) ).second;
55 assert(inserted && "duplicate token");
56 (void)inserted;
59 sal_uInt16 SvXMLTokenMap_Impl::get( sal_uInt16 nKeyPrefix, const OUString& rLName ) const
61 auto aIter( m_aPrefixAndNameToTokenMap.find( std::make_pair( nKeyPrefix, rLName ) ) );
62 if ( aIter != m_aPrefixAndNameToTokenMap.end() )
63 return (*aIter).second;
64 else
65 return XML_TOK_UNKNOWN;
68 SvXMLTokenMap::SvXMLTokenMap( const SvXMLTokenMapEntry *pMap )
69 : m_pImpl( new SvXMLTokenMap_Impl )
71 while( pMap->eLocalName != XML_TOKEN_INVALID )
73 m_pImpl->insert( *pMap );
74 pMap++;
78 SvXMLTokenMap::~SvXMLTokenMap()
82 sal_uInt16 SvXMLTokenMap::Get( sal_uInt16 nKeyPrefix,
83 const OUString& rLName ) const
85 return m_pImpl->get( nKeyPrefix, rLName );
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */