LibreOffice Module basegfx (master) 1
stringconversiontools.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <rtl/math.hxx>
22
23namespace basegfx::internal
24{
25 void skipSpaces(sal_Int32& io_rPos,
26 std::u16string_view rStr,
27 const sal_Int32 nLen)
28 {
29 while( io_rPos < nLen &&
30 rStr[io_rPos] == ' ' )
31 {
32 ++io_rPos;
33 }
34 }
35
36 static void skipSpacesAndCommas(sal_Int32& io_rPos,
37 std::u16string_view rStr,
38 const sal_Int32 nLen)
39 {
40 while(io_rPos < nLen
41 && (rStr[io_rPos] == ' ' || rStr[io_rPos] == ','))
42 {
43 ++io_rPos;
44 }
45 }
46
47 static bool getDoubleChar(double& o_fRetval,
48 sal_Int32& io_rPos,
49 std::u16string_view rStr)
50 {
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;
54
55 // sign
56 if(aChar == '+' || aChar == '-')
57 {
58 aChar = rStr[++io_rPos];
59 }
60
61 // numbers before point
62 while('0' <= aChar && '9' >= aChar)
63 {
64 io_rPos++;
65 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
66 }
67
68 // point
69 if(aChar == '.')
70 {
71 io_rPos++;
72 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
73 }
74
75 // numbers after point
76 while ('0' <= aChar && '9' >= aChar)
77 {
78 io_rPos++;
79 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
80 }
81
82 // 'e'
83 if(aChar == 'e' || aChar == 'E')
84 {
85 io_rPos++;
86 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
87
88 // sign for 'e'
89 if(aChar == '+' || aChar == '-')
90 {
91 io_rPos++;
92 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
93 }
94
95 // number for 'e'
96 while('0' <= aChar && '9' >= aChar)
97 {
98 io_rPos++;
99 aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0;
100 }
101 }
102
103 const sal_Int32 nLen = io_rPos - nStartPos;
104 if(nLen)
105 {
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 );
113 }
114
115 return false;
116 }
117
118 bool importDoubleAndSpaces(double& o_fRetval,
119 sal_Int32& io_rPos,
120 std::u16string_view rStr,
121 const sal_Int32 nLen )
122 {
123 if( !getDoubleChar(o_fRetval, io_rPos, rStr) )
124 return false;
125
126 skipSpacesAndCommas(io_rPos, rStr, nLen);
127
128 return true;
129 }
130
131 bool importFlagAndSpaces(sal_Int32& o_nRetval,
132 sal_Int32& io_rPos,
133 std::u16string_view rStr,
134 const sal_Int32 nLen)
135 {
136 sal_Unicode aChar( rStr[io_rPos] );
137
138 if(aChar == '0')
139 {
140 o_nRetval = 0;
141 ++io_rPos;
142 }
143 else if (aChar == '1')
144 {
145 o_nRetval = 1;
146 ++io_rPos;
147 }
148 else
149 return false;
150
151 skipSpacesAndCommas(io_rPos, rStr, nLen);
152
153 return true;
154 }
155
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void skipSpaces(sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
bool importFlagAndSpaces(sal_Int32 &o_nRetval, sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
static bool getDoubleChar(double &o_fRetval, sal_Int32 &io_rPos, std::u16string_view rStr)
bool importDoubleAndSpaces(double &o_fRetval, sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
static void skipSpacesAndCommas(sal_Int32 &io_rPos, std::u16string_view rStr, const sal_Int32 nLen)
sal_uInt16 sal_Unicode