Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / basegfx / source / tools / stringconversiontools.cxx
blob79b6d604662ea19ef803ed691891c7be3c7c915b
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 <stringconversiontools.hxx>
21 #include <rtl/math.hxx>
23 namespace basegfx::internal
25 void skipSpaces(sal_Int32& io_rPos,
26 std::u16string_view rStr,
27 const sal_Int32 nLen)
29 while( io_rPos < nLen &&
30 rStr[io_rPos] == ' ' )
32 ++io_rPos;
36 static void skipSpacesAndCommas(sal_Int32& io_rPos,
37 std::u16string_view rStr,
38 const sal_Int32 nLen)
40 while(io_rPos < nLen
41 && (rStr[io_rPos] == ' ' || rStr[io_rPos] == ','))
43 ++io_rPos;
47 static bool getDoubleChar(double& o_fRetval,
48 sal_Int32& io_rPos,
49 std::u16string_view rStr)
51 const sal_Int64 nStrSize = rStr.size();
52 sal_Unicode aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
53 const sal_Int32 nStartPos = io_rPos;
55 // sign
56 if(aChar == '+' || aChar == '-')
58 aChar = rStr[++io_rPos];
61 // numbers before point
62 while('0' <= aChar && '9' >= aChar)
64 io_rPos++;
65 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
68 // point
69 if(aChar == '.')
71 io_rPos++;
72 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
75 // numbers after point
76 while ('0' <= aChar && '9' >= aChar)
78 io_rPos++;
79 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
82 // 'e'
83 if(aChar == 'e' || aChar == 'E')
85 io_rPos++;
86 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
88 // sign for 'e'
89 if(aChar == '+' || aChar == '-')
91 io_rPos++;
92 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
95 // number for 'e'
96 while('0' <= aChar && '9' >= aChar)
98 io_rPos++;
99 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
103 const sal_Int32 nLen = io_rPos - nStartPos;
104 if(nLen)
106 rStr = rStr.substr(nStartPos, nLen);
107 rtl_math_ConversionStatus eStatus;
108 o_fRetval = ::rtl::math::stringToDouble( rStr,
109 '.',
110 ',',
111 &eStatus );
112 return ( eStatus == rtl_math_ConversionStatus_Ok );
115 return false;
118 bool importDoubleAndSpaces(double& o_fRetval,
119 sal_Int32& io_rPos,
120 std::u16string_view rStr,
121 const sal_Int32 nLen )
123 if( !getDoubleChar(o_fRetval, io_rPos, rStr) )
124 return false;
126 skipSpacesAndCommas(io_rPos, rStr, nLen);
128 return true;
131 bool importFlagAndSpaces(sal_Int32& o_nRetval,
132 sal_Int32& io_rPos,
133 std::u16string_view rStr,
134 const sal_Int32 nLen)
136 sal_Unicode aChar( rStr[io_rPos] );
138 if(aChar == '0')
140 o_nRetval = 0;
141 ++io_rPos;
143 else if (aChar == '1')
145 o_nRetval = 1;
146 ++io_rPos;
148 else
149 return false;
151 skipSpacesAndCommas(io_rPos, rStr, nLen);
153 return true;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */