1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 // Implementation class for Basic command: Format$( d,formatStr )
26 Grammar of format string (a try):
27 -----------------------------------------------
29 format_string := {\special_char} general_format | scientific_format {\special_char} {;format_string}
30 general_format := {#[,]}{0[,]}[.{0}{#}]
31 scientific_format := {0}[.{0}{#}](e | E)(+ | -){#}{0}
34 special_char := \char | + | - | ( | ) | $ | space_char
35 char := all_ascii_chars
38 {} repeated multiple times (incl. zero times)
39 [] exactly one or zero times
40 () parenthesis, e.g. (e | E) means e or E times
42 Additional predefined formats for the format string:
53 Note: invalid format string are ignored just as in VisualBasic, the output is
54 probably 'undefined'. ASCII letters are outputted directly.
56 Constraints in VisualBasic:
57 - the exponent (scientific syntax) has a maximum of three digits!
59 Constraints of new implementation:
60 - the '+' sign is not allowed as wildcard in the mantissa
64 Wildcards are: 'h', 'm', 's', 'y'
65 predefined String-Constants/Commands:
66 "AMPM", "Long Date", "Long Time"
69 #include <rtl/ustring.hxx>
70 #include <rtl/ustrbuf.hxx>
72 class SbxBasicFormater
{
74 // Constructor takes signs for decimal point, thousand separation sign
75 // and necessary resource strings.
76 SbxBasicFormater( sal_Unicode _cDecPoint
, sal_Unicode _cThousandSep
,
83 OUString _sCurrencyStrg
,
84 OUString _sCurrencyFormatStrg
);
86 /* Basic command: Format$( number,format-string )
89 dNumber : number to be formatted
90 sFormatStrg : the Format-String, e.g. ###0.0###
93 String containing the formatted output
95 OUString
BasicFormat( double dNumber
, const OUString
& sFormatStrg
);
96 static OUString
BasicFormatNull( std::u16string_view sFormatStrg
);
98 static bool isBasicFormat( std::u16string_view sFormatStrg
);
101 static inline void ShiftString( OUStringBuffer
& sStrg
, sal_uInt16 nStartPos
);
102 static void AppendDigit( OUStringBuffer
& sStrg
, short nDigit
);
103 void LeftShiftDecimalPoint( OUStringBuffer
& sStrg
);
104 void StrRoundDigit( OUStringBuffer
& sStrg
, short nPos
, bool& bOverflow
);
105 void StrRoundDigit( OUStringBuffer
& sStrg
, short nPos
);
106 static void ParseBack( OUStringBuffer
& sStrg
, std::u16string_view sFormatStrg
,
108 // Methods for string conversion with sprintf():
109 void InitScan( double _dNum
);
110 void InitExp( double _dNewExp
);
111 short GetDigitAtPosScan( short nPos
, bool& bFoundFirstDigit
);
112 short GetDigitAtPosExpScan( double dNewExponent
, short nPos
,
113 bool& bFoundFirstDigit
);
114 short GetDigitAtPosExpScan( short nPos
, bool& bFoundFirstDigit
);
115 static OUString
GetPosFormatString( std::u16string_view sFormatStrg
, bool & bFound
);
116 static OUString
GetNegFormatString( std::u16string_view sFormatStrg
, bool & bFound
);
117 static OUString
Get0FormatString( std::u16string_view sFormatStrg
, bool & bFound
);
118 static OUString
GetNullFormatString( std::u16string_view sFormatStrg
, bool & bFound
);
119 static void AnalyseFormatString( std::u16string_view sFormatStrg
,
120 short& nNoOfDigitsLeft
, short& nNoOfDigitsRight
,
121 short& nNoOfOptionalDigitsLeft
,
122 short& nNoOfExponentDigits
,
123 short& nNoOfOptionalExponentDigits
,
124 bool& bPercent
, bool& bCurrency
, bool& bScientific
,
125 bool& bGenerateThousandSeparator
,
126 short& nMultipleThousandSeparators
);
127 void ScanFormatString( double dNumber
, std::u16string_view sFormatStrg
,
128 OUString
& sReturnStrg
, bool bCreateSign
);
131 sal_Unicode cDecPoint
; // sign for the decimal point
132 sal_Unicode cThousandSep
; // sign for thousand delimiter
140 OUString sCurrencyStrg
;
141 OUString sCurrencyFormatStrg
;
143 //*** temporary data for scan loop ***
145 // String containing the number in scientific format
146 OUString sSciNumStrg
;
147 // String containing the exponent of the number
148 OUString sNumExpStrg
;
149 double dNum
; // the number that is scanned
150 short nNumExp
; // the exponent of the number
151 short nExpExp
; // the number of digits in the exponent
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */