Skip to content

Commit e94e818

Browse files
feat: add const char* constructors to Decimal and Int128
1 parent cfce615 commit e94e818

7 files changed

Lines changed: 114 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Added
66

7-
- NIL
7+
- Added `Decimal(const char*)` and `Int128(const char*)` constructors for C-string initialization
88

99
### Changed
1010

include/nfx/datatypes/Decimal.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,33 +236,41 @@ namespace nfx::datatypes
236236
* @brief Construct from 32-bit integer
237237
* @param value Integer value to convert
238238
*/
239-
explicit inline Decimal( std::int32_t value ) noexcept;
239+
inline explicit Decimal( std::int32_t value ) noexcept;
240240

241241
/**
242242
* @brief Construct from 64-bit integer
243243
* @param value Integer value to convert
244244
*/
245-
explicit inline Decimal( std::int64_t value ) noexcept;
245+
inline explicit Decimal( std::int64_t value ) noexcept;
246246

247247
/**
248248
* @brief Construct from 32-bit unsigned integer
249249
* @param value Unsigned integer value to convert
250250
*/
251-
explicit inline Decimal( std::uint32_t value ) noexcept;
251+
inline explicit Decimal( std::uint32_t value ) noexcept;
252252

253253
/**
254254
* @brief Construct from 64-bit unsigned integer
255255
* @param value Unsigned integer value to convert
256256
*/
257-
explicit inline Decimal( std::uint64_t value ) noexcept;
257+
inline explicit Decimal( std::uint64_t value ) noexcept;
258258

259259
/**
260260
* @brief Construct from string (exact parsing)
261261
* @param str String representation (e.g., "123.456")
262262
* @throws std::invalid_argument if string is not a valid decimal
263263
* @see fromString() for non-throwing parsing
264264
*/
265-
explicit inline Decimal( std::string_view str );
265+
inline explicit Decimal( std::string_view str );
266+
267+
/**
268+
* @brief Construct from C-string (exact parsing)
269+
* @param scStr Null-terminated C-string representation (e.g., "123.456")
270+
* @throws std::invalid_argument if string is not a valid decimal
271+
* @see fromString() for non-throwing parsing
272+
*/
273+
inline explicit Decimal( const char* scStr );
266274

267275
/**
268276
* @brief Construct from 128-bit integer with overflow detection

include/nfx/datatypes/Int128.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ namespace nfx::datatypes
200200
*/
201201
inline explicit Int128( std::string_view str );
202202

203+
/**
204+
* @brief Construct from C-string (exact parsing)
205+
* @param scStr Null-terminated C-string representation (e.g., "123", "-456789")
206+
* @throws std::invalid_argument if string is not a valid integer
207+
*/
208+
inline explicit Int128( const char* scStr );
209+
203210
/**
204211
* @brief Construct from single-precision floating-point value
205212
* @param val Float value to convert

include/nfx/detail/datatypes/Decimal.inl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ namespace nfx::datatypes
9494
}
9595
}
9696

97+
inline Decimal::Decimal( const char* scStr )
98+
: Decimal{ std::string_view{ scStr } }
99+
{
100+
}
101+
97102
//----------------------------------------------
98103
// Comparison with built-in floating point types
99104
//----------------------------------------------

include/nfx/detail/datatypes/Int128.inl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ namespace nfx::datatypes
8484
}
8585
}
8686

87+
inline Int128::Int128( const char* scStr )
88+
: Int128{ std::string_view{ scStr } }
89+
{
90+
}
91+
8792
inline constexpr Int128::Int128( std::uint64_t low, std::uint64_t high ) noexcept
8893
: m_value{ static_cast<NFX_DATATYPES_NATIVE_INT128>( high ) << constants::BITS_PER_UINT64 | low }
8994
{
@@ -398,6 +403,11 @@ namespace nfx::datatypes
398403
}
399404
}
400405

406+
inline Int128::Int128( const char* scStr )
407+
: Int128{ std::string_view{ scStr } }
408+
{
409+
}
410+
401411
inline constexpr Int128::Int128( std::uint64_t low, std::uint64_t high ) noexcept
402412
: m_layout{ low, high }
403413
{

test/Tests_Decimal.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,39 @@ namespace nfx::datatypes::test
24442444
EXPECT_FALSE( d5 == 0 );
24452445
}
24462446

2447+
TEST( DecimalStringParsing, CStringConstruction )
2448+
{
2449+
// Valid C-strings - tests const char* constructor
2450+
const char* str1 = "123.456";
2451+
datatypes::Decimal d1{ str1 };
2452+
EXPECT_EQ( d1.toString(), "123.456" );
2453+
2454+
const char* str2 = "-789.123";
2455+
datatypes::Decimal d2{ str2 };
2456+
EXPECT_TRUE( d2 < 0 );
2457+
2458+
const char* str3 = "0";
2459+
datatypes::Decimal d3{ str3 };
2460+
EXPECT_TRUE( d3 == 0 );
2461+
2462+
const char* str4 = "0.0001";
2463+
datatypes::Decimal d4{ str4 };
2464+
EXPECT_FALSE( d4 == 0 );
2465+
2466+
// Test with many Decimal places
2467+
const char* str5 = "123.1234567890123456789";
2468+
datatypes::Decimal d5{ str5 };
2469+
EXPECT_FALSE( d5 == 0 );
2470+
2471+
// Test invalid C-string throws exception
2472+
const char* invalid = "invalid";
2473+
EXPECT_THROW( datatypes::Decimal{ invalid }, std::invalid_argument );
2474+
2475+
// Test empty string throws exception
2476+
const char* empty = "";
2477+
EXPECT_THROW( datatypes::Decimal{ empty }, std::invalid_argument );
2478+
}
2479+
24472480
TEST( DecimalStringParsing, FromStringMethod )
24482481
{
24492482
datatypes::Decimal result;

test/Tests_Int128.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,51 @@ namespace nfx::datatypes::test
21622162
EXPECT_FALSE( datatypes::Int128::fromString( "1e10", result ) );
21632163
}
21642164

2165+
TEST( Int128StringParsing, CStringConstruction )
2166+
{
2167+
// Valid C-strings - tests const char* constructor
2168+
const char* str1 = "12345";
2169+
datatypes::Int128 i1{ str1 };
2170+
EXPECT_EQ( 12345ULL, i1.toLow() );
2171+
EXPECT_FALSE( i1 < 0 );
2172+
2173+
const char* str2 = "-9876543210";
2174+
datatypes::Int128 i2{ str2 };
2175+
EXPECT_TRUE( i2 < 0 );
2176+
2177+
const char* str3 = "0";
2178+
datatypes::Int128 i3{ str3 };
2179+
EXPECT_TRUE( i3 == 0 );
2180+
2181+
const char* str4 = "123456789012345678901234567890";
2182+
datatypes::Int128 i4{ str4 };
2183+
EXPECT_FALSE( i4 == 0 );
2184+
EXPECT_FALSE( i4 < 0 );
2185+
2186+
const char* str5 = "-123456789012345678901234567890";
2187+
datatypes::Int128 i5{ str5 };
2188+
EXPECT_TRUE( i5 < 0 );
2189+
2190+
const char* str6 = "+42";
2191+
datatypes::Int128 i6{ str6 };
2192+
EXPECT_EQ( 42ULL, i6.toLow() );
2193+
EXPECT_FALSE( i6 < 0 );
2194+
2195+
// Test invalid C-string throws exception
2196+
const char* invalid1 = "abc";
2197+
EXPECT_THROW( datatypes::Int128{ invalid1 }, std::invalid_argument );
2198+
2199+
const char* invalid2 = "123abc";
2200+
EXPECT_THROW( datatypes::Int128{ invalid2 }, std::invalid_argument );
2201+
2202+
const char* invalid3 = "12.34";
2203+
EXPECT_THROW( datatypes::Int128{ invalid3 }, std::invalid_argument );
2204+
2205+
// Test empty string throws exception
2206+
const char* empty = "";
2207+
EXPECT_THROW( datatypes::Int128{ empty }, std::invalid_argument );
2208+
}
2209+
21652210
TEST( Int128StringParsing, ParseMethod )
21662211
{
21672212
// Valid positive parsing

0 commit comments

Comments
 (0)