Some simplification

Change-Id: I8dc994421ef036f609e270c4891ac4494276ba23
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177299
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2024-11-25 20:18:38 +05:00
parent 8f2bf78a55
commit d88683e467
2 changed files with 89 additions and 130 deletions

View File

@@ -44,6 +44,8 @@ using namespace sca::analysis;
#define STDPAR false // all parameters are described #define STDPAR false // all parameters are described
#define INTPAR true // first parameter is internal #define INTPAR true // first parameter is internal
#define INV_MATCHLEV 1764 // guess, what this is... :-) - I doubt this kind of comment looks fun :-(
#define FUNCDATA( FUNCNAME, DBL, OPT, NUMOFPAR, CAT ) \ #define FUNCDATA( FUNCNAME, DBL, OPT, NUMOFPAR, CAT ) \
{ "get" #FUNCNAME, ANALYSIS_FUNCNAME_##FUNCNAME, ANALYSIS_##FUNCNAME, DBL, OPT, ANALYSIS_DEFFUNCNAME_##FUNCNAME, NUMOFPAR, CAT, nullptr } { "get" #FUNCNAME, ANALYSIS_FUNCNAME_##FUNCNAME, ANALYSIS_##FUNCNAME, DBL, OPT, ANALYSIS_DEFFUNCNAME_##FUNCNAME, NUMOFPAR, CAT, nullptr }
@@ -2032,39 +2034,30 @@ void ComplexList::Append( const uno::Sequence< uno::Any >& aMultPars )
} }
} }
ConvertData::ConvertData(const char p[], double fC, ConvertDataClass e, bool bPrefSupport) ConvertData::ConvertData(std::u16string_view sUnitName, double fC, ConvertDataClass e, bool bPrefSupport)
: fConst(fC) : fConst(fC)
, aName(p, strlen(p), RTL_TEXTENCODING_MS_1252) , aName(sUnitName)
, eClass(e) , eClass(e)
, bPrefixSupport(bPrefSupport) , bPrefixSupport(bPrefSupport)
{ {
assert(!aName.empty());
} }
ConvertData::~ConvertData() ConvertData::~ConvertData() = default;
{
}
sal_Int16 ConvertData::GetMatchingLevel( const OUString& rRef ) const sal_Int16 ConvertData::GetMatchingLevel( const OUString& rRef ) const
{ {
OUString aStr = rRef; OUString aStr = rRef;
sal_Int32 nLen = rRef.getLength(); if (sal_Int32 nIndex = rRef.lastIndexOf('^'); nIndex > 0 && nIndex == (rRef.getLength() - 2))
sal_Int32 nIndex = rRef.lastIndexOf( '^' ); aStr = aStr.replaceAt(nIndex, 1, "");
if( nIndex > 0 && nIndex == ( nLen - 2 ) )
aStr = aStr.subView( 0, nLen - 2 ) + OUStringChar( aStr[ nLen - 1 ] );
if( aName == aStr ) if( aName == aStr )
return 0; return 0;
else if (std::u16string_view prefix; bPrefixSupport && aStr.endsWith(aName, &prefix))
{ {
const sal_Unicode* p = aStr.getStr(); if (prefix.size() == 1 || prefix == u"da")
nLen = aStr.getLength();
bool bPref = bPrefixSupport;
bool bOneChar = (bPref && nLen > 1 && (aName == p + 1));
if (bOneChar || (bPref && nLen > 2 && (aName == p + 2) &&
*p == 'd' && *(p+1) == 'a'))
{ {
sal_Int16 n; sal_Int16 n;
switch( *p ) switch (prefix[0])
{ {
case 'y': n = -24; break; // yocto case 'y': n = -24; break; // yocto
case 'z': n = -21; break; // zepto case 'z': n = -21; break; // zepto
@@ -2076,12 +2069,10 @@ sal_Int16 ConvertData::GetMatchingLevel( const OUString& rRef ) const
case 'm': n = -3; break; case 'm': n = -3; break;
case 'c': n = -2; break; case 'c': n = -2; break;
case 'd': case 'd':
{ if (prefix.size() == 1)
if ( bOneChar ) n = -1; // deci
n = -1; // deci else
else n = 1; // deca
n = 1; // deca
}
break; break;
case 'e': n = 1; break; case 'e': n = 1; break;
case 'h': n = 2; break; case 'h': n = 2; break;
@@ -2093,85 +2084,72 @@ sal_Int16 ConvertData::GetMatchingLevel( const OUString& rRef ) const
case 'E': n = 18; break; case 'E': n = 18; break;
case 'Z': n = 21; break; // zetta case 'Z': n = 21; break; // zetta
case 'Y': n = 24; break; // yotta case 'Y': n = 24; break; // yotta
default: default: return INV_MATCHLEV;
n = INV_MATCHLEV;
} }
// We could weed some nonsense out, ODFF doesn't say so though. // We could weed some nonsense out, ODFF doesn't say so though.
#if 0 #if 0
if (n < 0 && Class() == CDC_Information) if (n < 0 && Class() == CDC_Information)
n = INV_MATCHLEV; // milli-bits doesn't make sense return INV_MATCHLEV; // milli-bits doesn't make sense
#endif #endif
//! <HACK> "cm3" is not 10^-2 m^3 but 10^-6 m^3 !!! ------------------ //! <HACK> "cm3" is not 10^-2 m^3 but 10^-6 m^3 !!! ------------------
if( n != INV_MATCHLEV ) if (aStr.endsWith("2"))
{ n *= 2;
sal_Unicode cLast = p[ aStr.getLength() - 1 ]; else if (aStr.endsWith("3"))
if( cLast == '2' ) n *= 3;
n *= 2;
else if( cLast == '3' )
n *= 3;
}
//! </HACK> ------------------------------------------------------------------- //! </HACK> -------------------------------------------------------------------
return n; return n;
} }
else if ( nLen > 2 && ( aName == p + 2 ) && ( Class() == CDC_Information ) ) else if (prefix.size() == 2 && prefix[1] == 'i' && Class() == CDC_Information)
{ {
const sal_Unicode* pStr = aStr.getStr(); switch (prefix[0])
if ( *(pStr + 1) != 'i')
return INV_MATCHLEV;
sal_Int16 n;
switch( *pStr )
{ {
case 'k': n = 10; break; case 'k': return 10;
case 'M': n = 20; break; case 'M': return 20;
case 'G': n = 30; break; case 'G': return 30;
case 'T': n = 40; break; case 'T': return 40;
case 'P': n = 50; break; case 'P': return 50;
case 'E': n = 60; break; case 'E': return 60;
case 'Z': n = 70; break; case 'Z': return 70;
case 'Y': n = 80; break; case 'Y': return 80;
default: default: return INV_MATCHLEV;
n = INV_MATCHLEV;
} }
return n;
} }
else
return INV_MATCHLEV;
} }
return INV_MATCHLEV;
} }
double ConvertData::Convert( double ConvertData::Convert(
double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const
{ {
if( Class() != r.Class() ) assert(Class() == r.Class());
throw lang::IllegalArgumentException();
bool bBinFromLev = ( nLevFrom > 0 && ( nLevFrom % 10 ) == 0 );
bool bBinToLev = ( nLevTo > 0 && ( nLevTo % 10 ) == 0 );
if ( Class() == CDC_Information && ( bBinFromLev || bBinToLev ) )
{
if ( bBinFromLev && bBinToLev )
{
nLevFrom = sal::static_int_cast<sal_Int16>( nLevFrom - nLevTo );
f *= r.fConst / fConst;
if( nLevFrom )
f *= pow( 2.0, nLevFrom );
}
else if ( bBinFromLev )
f *= ( r.fConst / fConst ) * ( pow( 2.0, nLevFrom ) / pow( 10.0, nLevTo ) );
else
f *= ( r.fConst / fConst ) * ( pow( 10.0, nLevFrom ) / pow( 2.0, nLevTo ) );
return f;
}
nLevFrom = sal::static_int_cast<sal_Int16>( nLevFrom - nLevTo ); // effective level
f *= r.fConst / fConst; f *= r.fConst / fConst;
if (Class() == CDC_Information)
{
bool bBinFromLev = (nLevFrom > 0 && (nLevFrom % 10) == 0);
bool bBinToLev = (nLevTo > 0 && (nLevTo % 10) == 0);
if (bBinFromLev || bBinToLev)
{
if (bBinFromLev && bBinToLev)
{
nLevFrom -= nLevTo;
if (nLevFrom)
f *= pow(2.0, nLevFrom);
}
else if (bBinFromLev)
f *= pow(2.0, nLevFrom) / pow(10.0, nLevTo);
else
f *= pow(10.0, nLevFrom) / pow(2.0, nLevTo);
return f;
}
}
nLevFrom -= nLevTo; // effective level
if( nLevFrom ) if( nLevFrom )
f = ::rtl::math::pow10Exp( f, nLevFrom ); f = ::rtl::math::pow10Exp( f, nLevFrom );
@@ -2179,21 +2157,14 @@ double ConvertData::Convert(
} }
double ConvertData::ConvertFromBase( double f, sal_Int16 n ) const ConvertDataLinear::~ConvertDataLinear() = default;
{
return ::rtl::math::pow10Exp( f * fConst, -n );
}
ConvertDataLinear::~ConvertDataLinear()
{
}
double ConvertDataLinear::Convert( double ConvertDataLinear::Convert(
double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const double f, const ConvertData& r, sal_Int16 nLevFrom, sal_Int16 nLevTo ) const
{ {
if( Class() != r.Class() ) assert(Class() == r.Class());
throw lang::IllegalArgumentException(); assert(dynamic_cast<const ConvertDataLinear*>(&r));
return r.ConvertFromBase( ConvertToBase( f, nLevFrom ), nLevTo ); return static_cast<const ConvertDataLinear&>(r).ConvertFromBase( ConvertToBase( f, nLevFrom ), nLevTo );
} }
@@ -2223,10 +2194,13 @@ double ConvertDataLinear::ConvertFromBase( double f, sal_Int16 n ) const
ConvertDataList::ConvertDataList() ConvertDataList::ConvertDataList()
{ {
#define NEWD(str,unit,cl) maVector.push_back(std::make_unique<ConvertData>(str,unit,cl)) #define NEWD(str,unit,cl) maVector.push_back(std::make_unique<ConvertData>(u"" str,unit,cl))
#define NEWDP(str,unit,cl) maVector.push_back(std::make_unique<ConvertData>(str,unit,cl,true)) #define NEWDP(str,unit,cl) maVector.push_back(std::make_unique<ConvertData>(u"" str,unit,cl,true))
#define NEWL(str,unit,offs,cl) maVector.push_back(std::make_unique<ConvertDataLinear>(str,unit,offs,cl)) #define NEWL(str,unit,offs,cl) maVector.push_back(std::make_unique<ConvertDataLinear>(u"" str,unit,offs,cl))
#define NEWLP(str,unit,offs,cl) maVector.push_back(std::make_unique<ConvertDataLinear>(str,unit,offs,cl,true)) #define NEWLP(str,unit,offs,cl) maVector.push_back(std::make_unique<ConvertDataLinear>(u"" str,unit,offs,cl,true))
const size_t expected_size = 146;
maVector.reserve(expected_size);
// *** are extra and not standard Excel Analysis Addin! // *** are extra and not standard Excel Analysis Addin!
@@ -2401,12 +2375,12 @@ ConvertDataList::ConvertDataList()
// INFORMATION: 1 Bit is... // INFORMATION: 1 Bit is...
NEWDP( "bit", 1.00E00, CDC_Information); // *** Bit NEWDP( "bit", 1.00E00, CDC_Information); // *** Bit
NEWDP( "byte", 1.25E-01, CDC_Information); // *** Byte NEWDP( "byte", 1.25E-01, CDC_Information); // *** Byte
assert(maVector.size() == expected_size);
} }
ConvertDataList::~ConvertDataList() ConvertDataList::~ConvertDataList() = default;
{
}
double ConvertDataList::Convert( double fVal, const OUString& rFrom, const OUString& rTo ) double ConvertDataList::Convert( double fVal, const OUString& rFrom, const OUString& rTo )
@@ -2420,41 +2394,32 @@ double ConvertDataList::Convert( double fVal, const OUString& rFrom, const OUStr
for( const auto& rItem : maVector ) for( const auto& rItem : maVector )
{ {
ConvertData* p = rItem.get();
if( bSearchFrom ) if( bSearchFrom )
{ {
sal_Int16 n = p->GetMatchingLevel( rFrom ); sal_Int16 n = rItem->GetMatchingLevel(rFrom);
if( n != INV_MATCHLEV ) if( n != INV_MATCHLEV )
{ {
if( n ) pFrom = rItem.get();
nLevelFrom = n;
if (!n)
{ // only first match for partial equality rulz a little bit more { // only first match for partial equality rulz a little bit more
pFrom = p; // ... but exact match rulz most
nLevelFrom = n;
}
else
{ // ... but exact match rulz most
pFrom = p;
bSearchFrom = false; bSearchFrom = false;
nLevelFrom = n;
} }
} }
} }
if( bSearchTo ) if( bSearchTo )
{ {
sal_Int16 n = p->GetMatchingLevel( rTo ); sal_Int16 n = rItem->GetMatchingLevel(rTo);
if( n != INV_MATCHLEV ) if( n != INV_MATCHLEV )
{ {
if( n ) pTo = rItem.get();
nLevelTo = n;
if (!n)
{ // only first match for partial equality rulz a little bit more { // only first match for partial equality rulz a little bit more
pTo = p; // ... but exact match rulz most
nLevelTo = n;
}
else
{ // ... but exact match rulz most
pTo = p;
bSearchTo = false; bSearchTo = false;
nLevelTo = n;
} }
} }
} }
@@ -2466,6 +2431,9 @@ double ConvertDataList::Convert( double fVal, const OUString& rFrom, const OUStr
if( !pFrom || !pTo ) if( !pFrom || !pTo )
throw lang::IllegalArgumentException(); throw lang::IllegalArgumentException();
if (pFrom->Class() != pTo->Class())
throw lang::IllegalArgumentException();
return pFrom->Convert( fVal, *pTo, nLevelFrom, nLevelTo ); return pFrom->Convert( fVal, *pTo, nLevelFrom, nLevelTo );
} }

View File

@@ -492,20 +492,17 @@ enum ConvertDataClass
}; };
#define INV_MATCHLEV 1764 // guess, what this is... :-)
class ConvertData class ConvertData
{ {
protected: protected:
friend class ConvertDataList; friend class ConvertDataList;
double fConst; double fConst;
OUString aName; std::u16string_view aName;
ConvertDataClass eClass; ConvertDataClass eClass;
bool bPrefixSupport; bool bPrefixSupport;
public: public:
ConvertData( ConvertData(
const char pUnitName[], std::u16string_view sUnitName,
double fConvertConstant, double fConvertConstant,
ConvertDataClass eClass, ConvertDataClass eClass,
bool bPrefSupport = false ); bool bPrefSupport = false );
@@ -522,9 +519,8 @@ public:
/// @throws css::lang::IllegalArgumentException /// @throws css::lang::IllegalArgumentException
virtual double Convert( double fVal, const ConvertData& rTo, virtual double Convert( double fVal, const ConvertData& rTo,
sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const; sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const;
virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
inline ConvertDataClass Class() const; ConvertDataClass Class() const { return eClass; }
}; };
class ConvertDataLinear final : public ConvertData class ConvertDataLinear final : public ConvertData
@@ -532,7 +528,7 @@ class ConvertDataLinear final : public ConvertData
double fOffs; double fOffs;
public: public:
inline ConvertDataLinear( inline ConvertDataLinear(
const char pUnitName[], std::u16string_view sUnitName,
double fConvertConstant, double fConvertConstant,
double fConvertOffset, double fConvertOffset,
ConvertDataClass eClass, ConvertDataClass eClass,
@@ -548,7 +544,7 @@ public:
// throws exception if not from same class // throws exception if not from same class
// this implementation is for proportional cases only // this implementation is for proportional cases only
double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const; double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const override; double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
}; };
@@ -709,14 +705,9 @@ inline void ComplexList::Append( Complex&& p )
} }
inline ConvertDataClass ConvertData::Class() const inline ConvertDataLinear::ConvertDataLinear( std::u16string_view sUnitName, double fC, double fO, ConvertDataClass e,
{
return eClass;
}
inline ConvertDataLinear::ConvertDataLinear( const char p[], double fC, double fO, ConvertDataClass e,
bool bPrefSupport ) : bool bPrefSupport ) :
ConvertData( p, fC, e, bPrefSupport ), ConvertData( sUnitName, fC, e, bPrefSupport ),
fOffs( fO ) fOffs( fO )
{ {
} }