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