update dev300-m58
[ooovba.git] / sc / source / core / tool / stringutil.cxx
blobd5e09a66efd2270adb73ea1b5b66101601b02242
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 ScSetStringParam::ScSetStringParam() :
44 mpNumFormatter(NULL),
45 mbDetectNumberFormat(true),
46 mbSetTextCellFormat(false)
50 // ============================================================================-
52 bool ScStringUtil::parseSimpleNumber(
53 const OUString& rStr, sal_Unicode dsep, sal_Unicode gsep, double& rVal)
55 if (gsep == 0x00A0)
56 // unicode space to ascii space
57 gsep = 0x0020;
59 OUStringBuffer aBuf;
60 sal_Int32 n = rStr.getLength();
61 const sal_Unicode* p = rStr.getStr();
62 sal_Int32 nPosDSep = -1, nPosGSep = -1;
63 sal_uInt32 nDigitCount = 0;
65 for (sal_Int32 i = 0; i < n; ++i)
67 sal_Unicode c = p[i];
68 if (c == 0x00A0)
69 // unicode space to ascii space
70 c = 0x0020;
72 if (sal_Unicode('0') <= c && c <= sal_Unicode('9'))
74 // this is a digit.
75 aBuf.append(c);
76 ++nDigitCount;
78 else if (c == dsep)
80 // this is a decimal separator.
82 if (nPosDSep >= 0)
83 // a second decimal separator -> not a valid number.
84 return false;
86 if (nPosGSep >= 0 && i - nPosGSep != 4)
87 // the number has a group separator and the decimal sep is not
88 // positioned correctly.
89 return false;
91 nPosDSep = i;
92 nPosGSep = -1;
93 aBuf.append(c);
94 nDigitCount = 0;
96 else if (c == gsep)
98 // this is a group (thousand) separator.
100 if (i == 0)
101 // not allowed as the first character.
102 return false;
104 if (nPosDSep >= 0)
105 // not allowed after the decimal separator.
106 return false;
108 if (nPosGSep >= 0 && nDigitCount != 3)
109 // must be exactly 3 digits since the last group separator.
110 return false;
112 nPosGSep = i;
113 nDigitCount = 0;
115 else if (c == sal_Unicode('-') || c == sal_Unicode('+'))
117 // A sign must be the first character if it's given.
118 if (i == 0)
119 aBuf.append(c);
120 else
121 return false;
123 else
124 return false;
127 // finished parsing the number.
129 if (nPosGSep >= 0 && nDigitCount != 3)
130 // must be exactly 3 digits since the last group separator.
131 return false;
133 rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
134 sal_Int32 nParseEnd = 0;
135 rVal = ::rtl::math::stringToDouble(aBuf.makeStringAndClear(), dsep, gsep, &eStatus, &nParseEnd);
136 if (eStatus != rtl_math_ConversionStatus_Ok)
137 return false;
139 return true;