update dev300-m57
[ooovba.git] / sc / source / core / tool / stringutil.cxx
blobae6247ffadef1733840783ed1148f317177c2bed
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: table.hxx,v $
10 * $Revision: 1.35 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 // System - Includes -----------------------------------------------------
36 #include "stringutil.hxx"
37 #include "rtl/ustrbuf.hxx"
38 #include "rtl/math.hxx"
40 using ::rtl::OUString;
41 using ::rtl::OUStringBuffer;
43 bool ScStringUtil::parseSimpleNumber(
44 const OUString& rStr, sal_Unicode dsep, sal_Unicode gsep, double& rVal)
46 if (gsep == 0x00A0)
47 // unicode space to ascii space
48 gsep = 0x0020;
50 OUStringBuffer aBuf;
51 sal_Int32 n = rStr.getLength();
52 const sal_Unicode* p = rStr.getStr();
53 sal_Int32 nPosDSep = -1, nPosGSep = -1;
54 sal_uInt32 nDigitCount = 0;
56 for (sal_Int32 i = 0; i < n; ++i)
58 sal_Unicode c = p[i];
59 if (c == 0x00A0)
60 // unicode space to ascii space
61 c = 0x0020;
63 if (sal_Unicode('0') <= c && c <= sal_Unicode('9'))
65 // this is a digit.
66 aBuf.append(c);
67 ++nDigitCount;
69 else if (c == dsep)
71 // this is a decimal separator.
73 if (nPosDSep >= 0)
74 // a second decimal separator -> not a valid number.
75 return false;
77 if (nPosGSep >= 0 && i - nPosGSep != 4)
78 // the number has a group separator and the decimal sep is not
79 // positioned correctly.
80 return false;
82 nPosDSep = i;
83 nPosGSep = -1;
84 aBuf.append(c);
85 nDigitCount = 0;
87 else if (c == gsep)
89 // this is a group (thousand) separator.
91 if (i == 0)
92 // not allowed as the first character.
93 return false;
95 if (nPosDSep >= 0)
96 // not allowed after the decimal separator.
97 return false;
99 if (nPosGSep >= 0 && nDigitCount != 3)
100 // must be exactly 3 digits since the last group separator.
101 return false;
103 nPosGSep = i;
104 nDigitCount = 0;
106 else if (c == sal_Unicode('-') || c == sal_Unicode('+'))
108 // A sign must be the first character if it's given.
109 if (i == 0)
110 aBuf.append(c);
111 else
112 return false;
114 else
115 return false;
118 // finished parsing the number.
120 if (nPosGSep >= 0 && nDigitCount != 3)
121 // must be exactly 3 digits since the last group separator.
122 return false;
124 rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
125 sal_Int32 nParseEnd = 0;
126 rVal = ::rtl::math::stringToDouble(aBuf.makeStringAndClear(), dsep, gsep, &eStatus, &nParseEnd);
127 if (eStatus != rtl_math_ConversionStatus_Ok)
128 return false;
130 return true;