Skip to content

Commit

Permalink
Fix setFormatedNumber() in FString for separator > 127
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed May 31, 2023
1 parent 143a4eb commit 382891a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2023-05-31 Markus Gans <guru.mail@muenster.de>
* Fix setFormatedNumber() in FString for separator > 127
(e.g. non-breaking space)

2023-05-18 Markus Gans <guru.mail@muenster.de>
* Version 0.9.0

Expand Down
6 changes: 3 additions & 3 deletions examples/string-operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ void convertToNumberExample()
try
{
const double double_num = \
finalcut::FString("2.718'281'828'459'045'235'3").toDouble();
finalcut::FString("2.7182818284590452353").toDouble();
std::ios_base::fmtflags save_flags = std::cout.flags();
std::cout << " toDouble: " << std::setprecision(11)
<< double_num << std::endl;
Expand Down Expand Up @@ -579,11 +579,11 @@ void formatedNumberExample()
finalcut::FString fnum2{};
#if defined(__LP64__) || defined(_LP64)
// 64-bit architecture
fnum1.setFormatedNumber(0xffffffffffffffffU, '\'');
fnum1.setFormatedNumber(0xffffffffffffffffU, finalcut::C_STR("'"));
fnum2.setFormatedNumber(-9223372036854775807);
#else
// 32-bit architecture
fnum1.setFormatedNumber(0xffffffffU, '\'');
fnum1.setFormatedNumber(0xffffffffU, finalcut::C_STR("'"));
fnum2.setFormatedNumber(-2147483647);
#endif
std::cout << "setFormatedNumber: "
Expand Down
6 changes: 3 additions & 3 deletions final/fobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
/* Inheritance diagram
* ═══════════════════
*
* ▔▔▔▔▔▔▔▔▏
* ▕ FTimer
* ▁▁▁▁▁▁▁▁▏
* ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
* ▕ FObjectTimer
* ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
* ▲
* │
* ▕▔▔▔▔▔▔▔▔▔▏
Expand Down
11 changes: 6 additions & 5 deletions final/ftimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
* <http://www.gnu.org/licenses/>. *
***********************************************************************/

/* Base class
* ══════════
/* Standalone class Base class
* ════════════════ ══════════
*
* ▕▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
* ▕ FTimer ▏ ▕ FObjectTimer ▏
* ▕▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
*
* ▕▔▔▔▔▔▔▔▔▏
* ▕ FTimer ▏
* ▕▁▁▁▁▁▁▁▁▏
*/

#ifndef FTIMER_H
Expand Down
16 changes: 8 additions & 8 deletions final/util/fstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,15 +760,15 @@ auto FString::setNumber (lDouble f_num, int precision) -> FString&
}

//----------------------------------------------------------------------
auto FString::setFormatedNumber (sInt64 num, char separator) -> FString&
auto FString::setFormatedNumber (sInt64 num, FString separator) -> FString&
{
int n{0};
std::array<wchar_t, 30> buf{};
wchar_t* s = &buf[29]; // Pointer to the last character
auto abs_num = static_cast<uInt64>(num);

if ( separator == 0 )
separator = ' ';
if ( separator[0] == 0 )
separator = L" ";

if ( num < 0 )
abs_num = static_cast<uInt64>(-num);
Expand All @@ -782,7 +782,7 @@ auto FString::setFormatedNumber (sInt64 num, char separator) -> FString&
n++;

if ( abs_num && n % 3 == 0 )
*--s = separator;
*--s = separator[0];
}
while ( abs_num );

Expand All @@ -795,15 +795,15 @@ auto FString::setFormatedNumber (sInt64 num, char separator) -> FString&
}

//----------------------------------------------------------------------
auto FString::setFormatedNumber (uInt64 num, char separator) -> FString&
auto FString::setFormatedNumber (uInt64 num, FString separator) -> FString&
{
int n{0};
std::array<wchar_t, 30> buf{};
wchar_t* s = &buf[29]; // Pointer to the last character
*s = L'\0';

if ( separator == 0 )
separator = ' ';
if ( separator[0] == 0 )
separator = L" ";

do
{
Expand All @@ -812,7 +812,7 @@ auto FString::setFormatedNumber (uInt64 num, char separator) -> FString&
n++;

if ( num && n % 3 == 0 )
*--s = separator;
*--s = separator[0];
}
while ( num );

Expand Down
8 changes: 4 additions & 4 deletions final/util/fstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ class FString
auto setNumber (lDouble, int = int(getPrecision<lDouble>())) -> FString&;

template <typename NumT>
auto setFormatedNumber (NumT, char = nl_langinfo(THOUSEP)[0]) -> FString&;
auto setFormatedNumber (sInt64, char = nl_langinfo(THOUSEP)[0]) -> FString&;
auto setFormatedNumber (uInt64, char = nl_langinfo(THOUSEP)[0]) -> FString&;
auto setFormatedNumber (NumT, FString = nl_langinfo(THOUSEP)) -> FString&;
auto setFormatedNumber (sInt64, FString = nl_langinfo(THOUSEP)) -> FString&;
auto setFormatedNumber (uInt64, FString = nl_langinfo(THOUSEP)) -> FString&;

auto insert (const FString&, int) -> const FString&;
auto insert (const FString&, std::size_t) -> const FString&;
Expand Down Expand Up @@ -690,7 +690,7 @@ inline auto FString::setNumber (NumT num, int precision) -> FString&

//----------------------------------------------------------------------
template <typename NumT>
inline auto FString::setFormatedNumber (NumT num, char separator) -> FString&
inline auto FString::setFormatedNumber (NumT num, FString separator) -> FString&
{
if ( isNegative(num) )
return setFormatedNumber (sInt64(num), separator);
Expand Down
15 changes: 15 additions & 0 deletions test/fstring-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,14 @@ void FStringTest::formatTest()

fnum2.setFormatedNumber(uInt64(9223372036854775807u), '\0');
CPPUNIT_ASSERT ( fnum2 == "9 223 372 036 854 775 807" );

// Non-breaking space
// value: 0xa0 = \240
// UTF-8: 0xc2 0xa0 = \302 \204
fnum2.setFormatedNumber(uInt64(9223372036854775807u), L"\240");
CPPUNIT_ASSERT ( fnum2 == "9\302\240" "223\302\240" "372\302\240"
"036\302\240" "854\302\240" "775\302\240"
"807" );
#else
// 32-bit architecture
fnum1.setFormatedNumber(0xffffffffu, '\'');
Expand All @@ -1395,6 +1403,13 @@ void FStringTest::formatTest()

fnum2.setFormatedNumber(uInt32(2147483647u), '\0');
CPPUNIT_ASSERT ( fnum2 == "2 147 483 647" );

// Non-breaking space
// value: 0xa0 = \240
// UTF-8: 0xc2 0xa0 = \302 \204
fnum2.setFormatedNumber(uInt32(2147483647u), L"\240");
CPPUNIT_ASSERT ( fnum2 == "2\302\240" "147\302\240" "483\302\240"
"647" );
#endif

fnum1.setFormatedNumber(sInt16(-2048), '_');
Expand Down

0 comments on commit 382891a

Please sign in to comment.