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