1b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5b3f79822SAndrew Rist * distributed with this work for additional information
6b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10b3f79822SAndrew Rist *
11b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12b3f79822SAndrew Rist *
13b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14b3f79822SAndrew Rist * software distributed under the License is distributed on an
15b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17b3f79822SAndrew Rist * specific language governing permissions and limitations
18b3f79822SAndrew Rist * under the License.
19b3f79822SAndrew Rist *
20b3f79822SAndrew Rist *************************************************************/
21b3f79822SAndrew Rist
22b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir // System - Includes -----------------------------------------------------
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "stringutil.hxx"
30cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
31cdf0e10cSrcweir #include "rtl/math.hxx"
32cdf0e10cSrcweir
33cdf0e10cSrcweir using ::rtl::OUString;
34cdf0e10cSrcweir using ::rtl::OUStringBuffer;
35cdf0e10cSrcweir
parseSimpleNumber(const OUString & rStr,sal_Unicode dsep,sal_Unicode gsep,double & rVal)36cdf0e10cSrcweir bool ScStringUtil::parseSimpleNumber(
37cdf0e10cSrcweir const OUString& rStr, sal_Unicode dsep, sal_Unicode gsep, double& rVal)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir if (gsep == 0x00A0)
40cdf0e10cSrcweir // unicode space to ascii space
41cdf0e10cSrcweir gsep = 0x0020;
42cdf0e10cSrcweir
43cdf0e10cSrcweir OUStringBuffer aBuf;
44cdf0e10cSrcweir sal_Int32 n = rStr.getLength();
45cdf0e10cSrcweir const sal_Unicode* p = rStr.getStr();
46cdf0e10cSrcweir sal_Int32 nPosDSep = -1, nPosGSep = -1;
47cdf0e10cSrcweir sal_uInt32 nDigitCount = 0;
48*c32d42b5Sdamjan bool haveSeenDigit = false;
49cdf0e10cSrcweir
50cdf0e10cSrcweir for (sal_Int32 i = 0; i < n; ++i)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir sal_Unicode c = p[i];
53cdf0e10cSrcweir if (c == 0x00A0)
54cdf0e10cSrcweir // unicode space to ascii space
55cdf0e10cSrcweir c = 0x0020;
56cdf0e10cSrcweir
57cdf0e10cSrcweir if (sal_Unicode('0') <= c && c <= sal_Unicode('9'))
58cdf0e10cSrcweir {
59cdf0e10cSrcweir // this is a digit.
60cdf0e10cSrcweir aBuf.append(c);
61*c32d42b5Sdamjan haveSeenDigit = true;
62cdf0e10cSrcweir ++nDigitCount;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir else if (c == dsep)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir // this is a decimal separator.
67cdf0e10cSrcweir
68cdf0e10cSrcweir if (nPosDSep >= 0)
69cdf0e10cSrcweir // a second decimal separator -> not a valid number.
70cdf0e10cSrcweir return false;
71cdf0e10cSrcweir
72cdf0e10cSrcweir if (nPosGSep >= 0 && i - nPosGSep != 4)
73cdf0e10cSrcweir // the number has a group separator and the decimal sep is not
74cdf0e10cSrcweir // positioned correctly.
75cdf0e10cSrcweir return false;
76cdf0e10cSrcweir
77cdf0e10cSrcweir nPosDSep = i;
78cdf0e10cSrcweir nPosGSep = -1;
79cdf0e10cSrcweir aBuf.append(c);
80cdf0e10cSrcweir nDigitCount = 0;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir else if (c == gsep)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir // this is a group (thousand) separator.
85cdf0e10cSrcweir
86*c32d42b5Sdamjan if (!haveSeenDigit)
87*c32d42b5Sdamjan // not allowed before digits.
88cdf0e10cSrcweir return false;
89cdf0e10cSrcweir
90cdf0e10cSrcweir if (nPosDSep >= 0)
91cdf0e10cSrcweir // not allowed after the decimal separator.
92cdf0e10cSrcweir return false;
93cdf0e10cSrcweir
94cdf0e10cSrcweir if (nPosGSep >= 0 && nDigitCount != 3)
95cdf0e10cSrcweir // must be exactly 3 digits since the last group separator.
96cdf0e10cSrcweir return false;
97cdf0e10cSrcweir
98cdf0e10cSrcweir nPosGSep = i;
99cdf0e10cSrcweir nDigitCount = 0;
100cdf0e10cSrcweir }
101cdf0e10cSrcweir else if (c == sal_Unicode('-') || c == sal_Unicode('+'))
102cdf0e10cSrcweir {
103cdf0e10cSrcweir // A sign must be the first character if it's given.
104cdf0e10cSrcweir if (i == 0)
105cdf0e10cSrcweir aBuf.append(c);
106cdf0e10cSrcweir else
107cdf0e10cSrcweir return false;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir else
110cdf0e10cSrcweir return false;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
113cdf0e10cSrcweir // finished parsing the number.
114cdf0e10cSrcweir
115cdf0e10cSrcweir if (nPosGSep >= 0 && nDigitCount != 3)
116cdf0e10cSrcweir // must be exactly 3 digits since the last group separator.
117cdf0e10cSrcweir return false;
118cdf0e10cSrcweir
119cdf0e10cSrcweir rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
120cdf0e10cSrcweir sal_Int32 nParseEnd = 0;
121cdf0e10cSrcweir rVal = ::rtl::math::stringToDouble(aBuf.makeStringAndClear(), dsep, gsep, &eStatus, &nParseEnd);
122cdf0e10cSrcweir if (eStatus != rtl_math_ConversionStatus_Ok)
123cdf0e10cSrcweir return false;
124cdf0e10cSrcweir
125cdf0e10cSrcweir return true;
126cdf0e10cSrcweir }
127