From ccf905a9d5e7f9c890a0f4c3b1f9be1879061aa1 Mon Sep 17 00:00:00 2001 From: Mindaugas Kavaliauskas Date: Mon, 6 Apr 2009 16:35:13 +0000 Subject: [PATCH] 2009-04-06 19:35 UTC+0200 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) * harbour/source/rtl/hbstrfmt.c + added precision support for %s and %d. COMMENT: I've used GNU printf() function for test. Some of formats is not supported by it. For example, zero padding is ignored if left alignment is used, etc. I'm not sure if it is intentional. --- harbour/ChangeLog | 8 ++++ harbour/source/rtl/hbstrfmt.c | 90 +++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index ab33f21535..4b1852bbd5 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,14 @@ 2009-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2009-04-06 19:35 UTC+0200 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) + * harbour/source/rtl/hbstrfmt.c + + added precision support for %s and %d. + COMMENT: I've used GNU printf() function for test. Some of + formats is not supported by it. For example, zero padding is + ignored if left alignment is used, etc. I'm not sure if it is + intentional. + 2009-04-06 18:12 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * bin/hb-mkdyn.bat * owatcom tweaks. Build is still going dunno if this will be okay. diff --git a/harbour/source/rtl/hbstrfmt.c b/harbour/source/rtl/hbstrfmt.c index e2efe59fd6..7bf5992645 100644 --- a/harbour/source/rtl/hbstrfmt.c +++ b/harbour/source/rtl/hbstrfmt.c @@ -79,7 +79,7 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P char *pFmt, *pFmtEnd, *pFmtSave; int i, iParam, iParamNo, iWidth, iDec; ULONG ulSize; - BOOL fLeftAlign, fForceSign, fPadZero, fSpaceSign; + BOOL fLeftAlign, fForceSign, fPadZero, fSpaceSign, fSign; pFmt = hb_itemGetCPtr( pItemFormat ); ulSize = hb_itemGetCLen( pItemFormat ); @@ -216,6 +216,7 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P const char * pStr2; int iSize, iExtra; + fSign = 0; if( HB_IS_NUMERIC( pItem ) ) { iSize = sizeof( HB_LONG ) * 3 + 1; @@ -224,6 +225,12 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P while( *pStr2 == ' ' ) pStr2++; iSize = strlen( pStr2 ); + if( *pStr2 == '-' ) + { + fSign = 1; + iSize--; + pStr2++; + } } else if( HB_IS_LOGICAL( pItem ) ) { @@ -240,24 +247,29 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P } iExtra = 0; - if( ( fForceSign || fSpaceSign ) && *pStr2 != '-' ) + if( fForceSign || fSpaceSign || fSign ) iExtra = 1; + /* If decimals is set, zero padding flag is ignored */ + if( iDec >= 0 ) fPadZero = 0; + if( fLeftAlign ) { /* Zero padding is ignored on left Align */ - if( *pStr2 != '-' ) - { - /* ForceSign has priority over SpaceSign */ - if( fForceSign ) - bufadd( &buffer, "+", 1 ); - else - { - if( fSpaceSign ) - bufadd( &buffer, " ", 1 ); - } - } + /* ForceSign has priority over SpaceSign */ + if( fSign ) + bufadd( &buffer, "-", 1 ); + else if( fForceSign ) + bufadd( &buffer, "+", 1 ); + else if( fSpaceSign ) + bufadd( &buffer, " ", 1 ); + + for( i = iSize; i < iDec; i++ ) + bufadd( &buffer, "0", 1 ); + bufadd( &buffer, pStr2, ( ULONG ) iSize ); + if( iDec > iSize ) + iSize = iDec; for( i = iSize + iExtra; i < iWidth; i++ ) bufadd( &buffer, " ", 1 ); } @@ -266,21 +278,14 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P /* Right align */ if( fPadZero ) { - if( *pStr2 == '-' ) - { - bufadd( &buffer, pStr2++, 1 ); - } - else - { - /* ForceSign has priority over SpaceSign */ - if( fForceSign ) - bufadd( &buffer, "+", 1 ); - else - { - if( fSpaceSign ) - bufadd( &buffer, " ", 1 ); - } - } + /* ForceSign has priority over SpaceSign */ + if( fSign ) + bufadd( &buffer, "-", 1 ); + else if( fForceSign ) + bufadd( &buffer, "+", 1 ); + else if( fSpaceSign ) + bufadd( &buffer, " ", 1 ); + for( i = iSize + iExtra; i < iWidth; i++ ) bufadd( &buffer, "0", 1 ); @@ -288,20 +293,20 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P } else { - for( i = iSize + iExtra; i < iWidth; i++ ) + for( i = (iSize > iDec ? iSize : iDec) + iExtra; i < iWidth; i++ ) bufadd( &buffer, " ", 1 ); - if( *pStr2 != '-' ) - { - /* ForceSign has priority over SpaceSign */ - if( fForceSign ) - bufadd( &buffer, "+", 1 ); - else - { - if( fSpaceSign ) - bufadd( &buffer, " ", 1 ); - } - } + /* ForceSign has priority over SpaceSign */ + if( fSign ) + bufadd( &buffer, "-", 1 ); + else if( fForceSign ) + bufadd( &buffer, "+", 1 ); + else if( fSpaceSign ) + bufadd( &buffer, " ", 1 ); + + for( i = iSize; i < iDec; i++ ) + bufadd( &buffer, "0", 1 ); + bufadd( &buffer, pStr2, ( ULONG ) iSize ); } } @@ -430,6 +435,11 @@ PHB_ITEM hb_strFormat( PHB_ITEM pItemReturn, PHB_ITEM pItemFormat, int iCount, P char * pStr = hb_itemGetCPtr( pItem ); ulSize = hb_itemGetCLen( pItem ); + if( iDec >= 0 ) + { + if( ( ULONG ) iDec < ulSize ) + ulSize = iDec; + } if( fLeftAlign ) bufadd( &buffer, pStr, ulSize );