xref: /aoo42x/main/sc/source/core/tool/stringutil.cxx (revision b3f79822)
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 
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 
49     for (sal_Int32 i = 0; i < n; ++i)
50     {
51         sal_Unicode c = p[i];
52         if (c == 0x00A0)
53             // unicode space to ascii space
54             c = 0x0020;
55 
56         if (sal_Unicode('0') <= c && c <= sal_Unicode('9'))
57         {
58             // this is a digit.
59             aBuf.append(c);
60             ++nDigitCount;
61         }
62         else if (c == dsep)
63         {
64             // this is a decimal separator.
65 
66             if (nPosDSep >= 0)
67                 // a second decimal separator -> not a valid number.
68                 return false;
69 
70             if (nPosGSep >= 0 && i - nPosGSep != 4)
71                 // the number has a group separator and the decimal sep is not
72                 // positioned correctly.
73                 return false;
74 
75             nPosDSep = i;
76             nPosGSep = -1;
77             aBuf.append(c);
78             nDigitCount = 0;
79         }
80         else if (c == gsep)
81         {
82             // this is a group (thousand) separator.
83 
84             if (i == 0)
85                 // not allowed as the first character.
86                 return false;
87 
88             if (nPosDSep >= 0)
89                 // not allowed after the decimal separator.
90                 return false;
91 
92             if (nPosGSep >= 0 && nDigitCount != 3)
93                 // must be exactly 3 digits since the last group separator.
94                 return false;
95 
96             nPosGSep = i;
97             nDigitCount = 0;
98         }
99         else if (c == sal_Unicode('-') || c == sal_Unicode('+'))
100         {
101             // A sign must be the first character if it's given.
102             if (i == 0)
103                 aBuf.append(c);
104             else
105                 return false;
106         }
107         else
108             return false;
109     }
110 
111     // finished parsing the number.
112 
113     if (nPosGSep >= 0 && nDigitCount != 3)
114         // must be exactly 3 digits since the last group separator.
115         return false;
116 
117     rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
118     sal_Int32 nParseEnd = 0;
119     rVal = ::rtl::math::stringToDouble(aBuf.makeStringAndClear(), dsep, gsep, &eStatus, &nParseEnd);
120     if (eStatus != rtl_math_ConversionStatus_Ok)
121         return false;
122 
123     return true;
124 }
125