/src/libreoffice/basegfx/source/tools/stringconversiontools.cxx
Line | Count | Source |
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 | | |
20 | | #include <stringconversiontools.hxx> |
21 | | #include <rtl/math.hxx> |
22 | | |
23 | | namespace basegfx::internal |
24 | | { |
25 | | void skipSpaces(sal_Int32& io_rPos, |
26 | | std::u16string_view rStr, |
27 | | const sal_Int32 nLen) |
28 | 145k | { |
29 | 145k | while( io_rPos < nLen && |
30 | 125k | rStr[io_rPos] == ' ' ) |
31 | 175 | { |
32 | 175 | ++io_rPos; |
33 | 175 | } |
34 | 145k | } |
35 | | |
36 | | static void skipSpacesAndCommas(sal_Int32& io_rPos, |
37 | | std::u16string_view rStr, |
38 | | const sal_Int32 nLen) |
39 | 98.9k | { |
40 | 141k | while(io_rPos < nLen |
41 | 141k | && (rStr[io_rPos] == ' ' || rStr[io_rPos] == ',')) |
42 | 42.8k | { |
43 | 42.8k | ++io_rPos; |
44 | 42.8k | } |
45 | 98.9k | } |
46 | | |
47 | | static bool getDoubleChar(double& o_fRetval, |
48 | | sal_Int32& io_rPos, |
49 | | std::u16string_view rStr) |
50 | 99.9k | { |
51 | 99.9k | const sal_Int64 nStrSize = rStr.size(); |
52 | 99.9k | sal_Unicode aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
53 | 99.9k | const sal_Int32 nStartPos = io_rPos; |
54 | | |
55 | | // sign |
56 | 99.9k | if(aChar == '+' || aChar == '-') |
57 | 21.5k | { |
58 | 21.5k | aChar = rStr[++io_rPos]; |
59 | 21.5k | } |
60 | | |
61 | | // numbers before point |
62 | 475k | while('0' <= aChar && '9' >= aChar) |
63 | 375k | { |
64 | 375k | io_rPos++; |
65 | 375k | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
66 | 375k | } |
67 | | |
68 | | // point |
69 | 99.9k | if(aChar == '.') |
70 | 277 | { |
71 | 277 | io_rPos++; |
72 | 277 | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
73 | 277 | } |
74 | | |
75 | | // numbers after point |
76 | 100k | while ('0' <= aChar && '9' >= aChar) |
77 | 734 | { |
78 | 734 | io_rPos++; |
79 | 734 | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
80 | 734 | } |
81 | | |
82 | | // 'e' |
83 | 99.9k | if(aChar == 'e' || aChar == 'E') |
84 | 18 | { |
85 | 18 | io_rPos++; |
86 | 18 | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
87 | | |
88 | | // sign for 'e' |
89 | 18 | if(aChar == '+' || aChar == '-') |
90 | 0 | { |
91 | 0 | io_rPos++; |
92 | 0 | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
93 | 0 | } |
94 | | |
95 | | // number for 'e' |
96 | 41 | while('0' <= aChar && '9' >= aChar) |
97 | 23 | { |
98 | 23 | io_rPos++; |
99 | 23 | aChar = io_rPos < nStrSize ? rStr[io_rPos] : 0; |
100 | 23 | } |
101 | 18 | } |
102 | | |
103 | 99.9k | const sal_Int32 nLen = io_rPos - nStartPos; |
104 | 99.9k | if(nLen) |
105 | 98.9k | { |
106 | 98.9k | rStr = rStr.substr(nStartPos, nLen); |
107 | 98.9k | rtl_math_ConversionStatus eStatus; |
108 | 98.9k | o_fRetval = ::rtl::math::stringToDouble( rStr, |
109 | 98.9k | '.', |
110 | 98.9k | ',', |
111 | 98.9k | &eStatus ); |
112 | 98.9k | return ( eStatus == rtl_math_ConversionStatus_Ok ); |
113 | 98.9k | } |
114 | | |
115 | 954 | return false; |
116 | 99.9k | } |
117 | | |
118 | | bool importDoubleAndSpaces(double& o_fRetval, |
119 | | sal_Int32& io_rPos, |
120 | | std::u16string_view rStr, |
121 | | const sal_Int32 nLen ) |
122 | 99.9k | { |
123 | 99.9k | if( !getDoubleChar(o_fRetval, io_rPos, rStr) ) |
124 | 956 | return false; |
125 | | |
126 | 98.9k | skipSpacesAndCommas(io_rPos, rStr, nLen); |
127 | | |
128 | 98.9k | return true; |
129 | 99.9k | } |
130 | | |
131 | | bool importFlagAndSpaces(sal_Int32& o_nRetval, |
132 | | sal_Int32& io_rPos, |
133 | | std::u16string_view rStr, |
134 | | const sal_Int32 nLen) |
135 | 2 | { |
136 | 2 | sal_Unicode aChar( rStr[io_rPos] ); |
137 | | |
138 | 2 | if(aChar == '0') |
139 | 0 | { |
140 | 0 | o_nRetval = 0; |
141 | 0 | ++io_rPos; |
142 | 0 | } |
143 | 2 | else if (aChar == '1') |
144 | 1 | { |
145 | 1 | o_nRetval = 1; |
146 | 1 | ++io_rPos; |
147 | 1 | } |
148 | 1 | else |
149 | 1 | return false; |
150 | | |
151 | 1 | skipSpacesAndCommas(io_rPos, rStr, nLen); |
152 | | |
153 | 1 | return true; |
154 | 2 | } |
155 | | |
156 | | } |
157 | | |
158 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |