diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-05 18:20:54 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-05 18:20:54 -0500 |
| commit | 147e0f0269440fce15d6b88a8a90627f3a3b2df2 (patch) | |
| tree | bc33522ba71b5a2996fae22e102cce5046cf1333 /stdlib/bytes.c | |
| parent | 2c4324670ff569ede360d13875c5e4b5720a626d (diff) | |
Overhaul of constructors, making it more consistent and correct. Also
changed T(), T, T_t, T_s type names to T(), T$$info, T$$type, T$$struct
for unambiguity
Diffstat (limited to 'stdlib/bytes.c')
| -rw-r--r-- | stdlib/bytes.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/stdlib/bytes.c b/stdlib/bytes.c index 1e889f6d..b24a721b 100644 --- a/stdlib/bytes.c +++ b/stdlib/bytes.c @@ -30,6 +30,29 @@ public Text_t Byte$hex(Byte_t byte, bool uppercase, bool prefix) { return text; } +public PUREFUNC Byte_t Byte$from_int(Int_t i, bool truncate) { + if unlikely (truncate && Int$compare_value(i, I_small(0xFF)) > 0) + fail("This value is too large to convert to a byte without truncation: %k", (Text_t[1]){Int$value_as_text(i)}); + else if unlikely (truncate && Int$compare_value(i, I_small(0)) < 0) + fail("Negative values can't be converted to bytes: %k", (Text_t[1]){Int$value_as_text(i)}); + return (i.small != 0); +} +public PUREFUNC Byte_t Byte$from_int64(Int64_t i, bool truncate) { + if unlikely (truncate && i != (Int64_t)(Byte_t)i) + fail("This value can't be converted to a byte without truncation: %ld", i); + return (Byte_t)i; +} +public PUREFUNC Byte_t Byte$from_int32(Int32_t i, bool truncate) { + if unlikely (truncate && i != (Int32_t)(Byte_t)i) + fail("This value can't be converted to a byte without truncation: %d", i); + return (Byte_t)i; +} +public PUREFUNC Byte_t Byte$from_int16(Int16_t i, bool truncate) { + if unlikely (truncate && i != (Int16_t)(Byte_t)i) + fail("This value can't be converted to a byte without truncation: %d", i); + return (Byte_t)i; +} + public const TypeInfo_t Byte$info = { .size=sizeof(Byte_t), .align=__alignof__(Byte_t), |
