Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / tools / helpers.hxx
blob05ee59960f50519f651ac4ae7aae3ffd1ab91f21
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 */
9 #pragma once
11 #include <sal/config.h>
12 #include <sal/types.h>
13 #include <tools/long.hxx>
14 #include <cassert>
15 #include <limits>
16 #include <type_traits>
18 inline sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
20 if (nWidthBits > SAL_MAX_UINT32 - 31)
21 nWidthBits = SAL_MAX_UINT32;
22 else
23 nWidthBits += 31;
24 return (nWidthBits >> 5) << 2;
27 inline tools::Long FRound( double fVal )
29 return fVal > 0.0
30 ? fVal == double(std::numeric_limits<tools::Long>::max())
31 ? std::numeric_limits<tools::Long>::max() : static_cast<tools::Long>( fVal + 0.5 )
32 : static_cast<tools::Long>( fVal - 0.5 );
35 //valid range: (-180,180]
36 template <typename T>
37 [[nodiscard]] inline typename std::enable_if<std::is_signed<T>::value, T>::type
38 NormAngle180(T angle)
40 while (angle <= -180)
41 angle += 360;
42 while (angle > 180)
43 angle -= 360;
44 return angle;
47 //valid range: [0,360)
48 template <typename T> [[nodiscard]] inline T NormAngle360(T angle)
50 while (angle < 0)
51 angle += 360;
52 while (angle >= 360)
53 angle -= 360;
54 return angle;
57 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */