Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 8f1dedd

Browse files
committed
libfoundation: Use new narrowing casts
Use the new `MCNarrowCast()` and `MCNarrow()` templates in some places where explicit narrowing occurs. Note that when using `MCNarrow()` it is no longer necessary to know anything about the types involved.
1 parent 9bde75b commit 8f1dedd

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

libfoundation/include/foundation-unicode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ inline bool MCUnicodeCodepointToSurrogates(codepoint_t p_codepoint,
3939
{
4040
if (p_codepoint < 0x10000)
4141
{
42-
r_leading = unichar_t(p_codepoint); /* safe narrowing */
42+
r_leading = MCNarrowCast<unichar_t>(p_codepoint);
4343
r_trailing = 0;
4444
return false;
4545
}

libfoundation/src/foundation-stream.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,14 @@ static bool __MCMemoryInputStreamTell(MCStreamRef p_stream, filepos_t& r_positio
112112
{
113113
__MCMemoryInputStream *self;
114114
self = (__MCMemoryInputStream *)MCStreamGetExtraBytesPtr(p_stream);
115-
r_position = self -> pointer;
116-
return true;
115+
return MCNarrow(self->pointer, r_position);
117116
}
118117

119118
static bool __MCMemoryInputStreamSeek(MCStreamRef p_stream, filepos_t p_position)
120119
{
121-
/* This type is large enough to hold both the maximum value of
122-
* filepos_t and the maximum value of a size_t. */
123-
using file_size_t = decltype(filepos_t{} + size_t{});
124-
125120
__MCMemoryInputStream *self;
126121
self = (__MCMemoryInputStream *)MCStreamGetExtraBytesPtr(p_stream);
127-
if (p_position < 0
128-
|| /*NARROWING*/file_size_t(p_position) > self -> length)
129-
return false;
130-
self -> pointer = (size_t)p_position;
131-
return true;
122+
return MCNarrow(p_position, self->pointer);
132123
}
133124

134125
static MCStreamCallbacks kMCMemoryInputStreamCallbacks =

0 commit comments

Comments
 (0)