Skip to content

Complete casting between floats and ints #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions subdoc/lib/gen/markdown_to_html.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,25 @@ void apply_syntax_highlighting(std::string& str) noexcept {
usize pos;
auto view = std::string_view(str);
while (true) {
// Find the <pre> tags.
pos = view.find("<pre>", pos);
if (pos == std::string::npos) break;
usize end_pos = view.find("</pre>", pos + 5u);
pos += 5u;
usize end_pos = view.find("</pre>", pos);
if (end_pos == std::string::npos) break;

// Inside <pre>, find the <code> tags and move inside them.
pos = [&](usize pos) {
usize plain = view.find("<code>", pos);
if (plain != std::string::npos) plain += strlen("<code>");
usize with_lang = view.find("<code class=\"language-cpp\">", pos);
if (with_lang != std::string::npos)
with_lang += strlen("<code class=\"language-cpp\">");
return sus::cmp::min(plain, with_lang);
}(pos);
if (pos == std::string::npos) break;
end_pos = sus::cmp::min(end_pos, usize::from(view.find("</code>", pos)));

bool in_comment = false;
bool in_string = false;
bool in_char = false;
Expand Down Expand Up @@ -205,7 +219,7 @@ void apply_syntax_highlighting(std::string& str) noexcept {
continue;
}

// There's a <pre> tag at the start so we can always look backward one
// There's a <code> tag at the start so we can always look backward one
// char.
sus::check(pos > 0u);
char before = view[pos - 1u];
Expand Down Expand Up @@ -330,7 +344,7 @@ sus::Result<MarkdownToHtml, MarkdownToHtmlError> markdown_to_html(
} else {
// SAFETY: The input is a char, so tolower will give a value in the
// range of char.
c = sus::mog<char>(std::tolower(c));
c = sus::cast<char>(std::tolower(c));
}
}

Expand Down
8 changes: 4 additions & 4 deletions sus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ target_sources(subspace PUBLIC
"construct/into.h"
"construct/default.h"
"construct/safe_from_reference.h"
"construct/transmogrify.h"
"construct/cast.h"
"collections/__private/slice_methods_impl.inc"
"collections/__private/slice_methods.inc"
"collections/__private/slice_mut_methods.inc"
Expand Down Expand Up @@ -171,7 +171,7 @@ target_sources(subspace PUBLIC
"num/__private/unsigned_integer_consts.inc"
"num/__private/unsigned_integer_methods.inc"
"num/__private/unsigned_integer_methods_impl.inc"
"num/transmogrify.h"
"num/cast.h"
"num/float.h"
"num/float_concepts.h"
"num/float_impl.h"
Expand Down Expand Up @@ -251,7 +251,7 @@ if(${SUBSPACE_BUILD_TESTS})
"cmp/eq_unittest.cc"
"cmp/ord_unittest.cc"
"cmp/reverse_unittest.cc"
"construct/transmogrify_unittest.cc"
"construct/cast_unittest.cc"
"collections/array_unittest.cc"
"collections/compat_deque_unittest.cc"
"collections/compat_forward_list_unittest.cc"
Expand Down Expand Up @@ -294,7 +294,7 @@ if(${SUBSPACE_BUILD_TESTS})
"mem/take_unittest.cc"
"num/__private/literals_unittest.cc"
"num/cmath_macros_unittest.cc"
"num/transmogrify_unittest.cc"
"num/cast_unittest.cc"
"num/f32_unittest.cc"
"num/f64_unittest.cc"
"num/i8_unittest.cc"
Expand Down
2 changes: 1 addition & 1 deletion sus/boxed/dyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ namespace sus::boxed {
/// In order to ensure the target of the `DynC&` reference outlives the function
/// it can be constructed as a stack variable before calling the function.
/// ```
/// std::srand(sus::mog<unsigned>(std::time(nullptr)));
/// std::srand(sus::cast<unsigned>(std::time(nullptr)));
///
/// auto x = [](sus::Option<sus::fn::DynFn<std::string()>&> fn) {
/// if (fn.is_some())
Expand Down
2 changes: 1 addition & 1 deletion sus/boxed/dyn_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ TEST(Dyn, Example_Macro) {
} // namespace example_macro

TEST(Dyn, Example_Stack) {
std::srand(sus::mog<unsigned>(std::time(nullptr)));
std::srand(sus::cast<unsigned>(std::time(nullptr)));

auto x = [](sus::Option<sus::fn::DynFn<std::string()>&> fn) {
if (fn.is_some())
Expand Down
2 changes: 1 addition & 1 deletion sus/collections/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct Storage<T, 0> final {};
/// subtracting a pointer at a greater distance results in Undefined Behaviour.
template <class T, size_t N>
class Array final {
static_assert(N <= ::sus::mog<usize>(isize::MAX));
static_assert(N <= ::sus::cast<usize>(isize::MAX));
static_assert(!std::is_reference_v<T>,
"Array<T&, N> is invalid as Array must hold value types. Use "
"Array<T*, N> instead.");
Expand Down
14 changes: 7 additions & 7 deletions sus/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#include "sus/mem/clone.h"
#include "sus/mem/move.h"
#include "sus/mem/swap.h"
#include "sus/num/cast.h"
#include "sus/num/signed_integer.h"
#include "sus/num/transmogrify.h"
#include "sus/num/unsigned_integer.h"
#include "sus/ops/range.h"
#include "sus/option/option.h"
Expand Down Expand Up @@ -115,7 +115,7 @@ class [[sus_trivial_abi]] Slice final {
sus_pure static constexpr Slice from_raw_parts(
::sus::marker::UnsafeFnMarker, const T* data sus_lifetimebound,
usize len) noexcept {
::sus::check(len <= ::sus::mog<usize>(isize::MAX));
::sus::check(len <= ::sus::cast<usize>(isize::MAX));
// We strip the `const` off `data`, however only const access is provided
// through this class. This is done so that mutable types can compose Slice
// and store a mutable pointer.
Expand Down Expand Up @@ -150,7 +150,7 @@ class [[sus_trivial_abi]] Slice final {
sus_pure static constexpr Slice from_raw_collection(
::sus::marker::UnsafeFnMarker, ::sus::iter::IterRefCounter refs,
const T* data sus_lifetimebound, usize len) noexcept {
::sus::check(len <= ::sus::mog<usize>(isize::MAX));
::sus::check(len <= ::sus::cast<usize>(isize::MAX));
// We strip the `const` off `data`, however only const access is provided
// through this class. This is done so that mutable types can compose Slice
// and store a mutable pointer.
Expand All @@ -163,7 +163,7 @@ class [[sus_trivial_abi]] Slice final {
///
/// #[doc.overloads=from.array]
template <size_t N>
requires(N <= ::sus::mog<usize>(isize::MAX))
requires(N <= ::sus::cast<usize>(isize::MAX))
sus_pure static constexpr Slice from(const T (&data)[N] sus_lifetimebound) {
// We strip the `const` off `data`, however only const access is provided
// through this class. This is done so that mutable types can compose Slice
Expand Down Expand Up @@ -342,7 +342,7 @@ class [[sus_trivial_abi]] SliceMut final {
sus_pure static constexpr SliceMut from_raw_parts_mut(
::sus::marker::UnsafeFnMarker, T* data sus_lifetimebound,
usize len) noexcept {
::sus::check(len <= ::sus::mog<usize>(isize::MAX));
::sus::check(len <= ::sus::cast<usize>(isize::MAX));
return SliceMut(::sus::iter::IterRefCounter::empty_for_view(), data, len);
}

Expand Down Expand Up @@ -372,7 +372,7 @@ class [[sus_trivial_abi]] SliceMut final {
sus_pure static constexpr SliceMut from_raw_collection_mut(
::sus::marker::UnsafeFnMarker, ::sus::iter::IterRefCounter refs,
T* data sus_lifetimebound, usize len) noexcept {
::sus::check(len <= ::sus::mog<usize>(isize::MAX));
::sus::check(len <= ::sus::cast<usize>(isize::MAX));
return SliceMut(::sus::move(refs), data, len);
}

Expand All @@ -382,7 +382,7 @@ class [[sus_trivial_abi]] SliceMut final {
///
/// #[doc.overloads=from.array]
template <size_t N>
requires(N <= ::sus::mog<usize>(isize::MAX_PRIMITIVE))
requires(N <= ::sus::cast<usize>(isize::MAX_PRIMITIVE))
sus_pure static constexpr SliceMut from(T (&data)[N] sus_lifetimebound) {
return SliceMut(::sus::iter::IterRefCounter::empty_for_view(), data, N);
}
Expand Down
13 changes: 7 additions & 6 deletions sus/collections/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
#include "sus/mem/relocate.h"
#include "sus/mem/replace.h"
#include "sus/mem/size_of.h"
#include "sus/num/cast.h"
#include "sus/num/integer_concepts.h"
#include "sus/num/signed_integer.h"
#include "sus/num/transmogrify.h"
#include "sus/num/unsigned_integer.h"
#include "sus/ops/range.h"
#include "sus/option/option.h"
Expand Down Expand Up @@ -147,7 +147,8 @@ class Vec final {
/// # Panics
/// Panics if the capacity exceeds `isize::MAX` bytes.
sus_pure static constexpr Vec with_capacity(usize capacity) noexcept {
check(::sus::mem::size_of<T>() * capacity <= ::sus::mog<usize>(isize::MAX));
check(::sus::mem::size_of<T>() * capacity <=
::sus::cast<usize>(isize::MAX));
return Vec(WITH_CAPACITY, std::allocator<T>(), capacity);
}

Expand Down Expand Up @@ -210,12 +211,12 @@ class Vec final {
requires(std::same_as<T, u8> && //
(std::same_as<C, char> || std::same_as<C, signed char> ||
std::same_as<C, unsigned char>) &&
N <= ::sus::mog<usize>(isize::MAX))
N <= ::sus::cast<usize>(isize::MAX))
static constexpr Vec from(const C (&arr)[N]) {
auto s = sus::Slice<C>::from(arr);
auto v = Vec::with_capacity(N - 1);
for (auto c : s[sus::ops::RangeTo<usize>(N - 1)])
v.push_with_capacity_internal(sus::mog<uint8_t>(c));
v.push_with_capacity_internal(sus::cast<uint8_t>(c));
::sus::check(s[N - 1] == 0); // Null terminated.
return v;
}
Expand Down Expand Up @@ -1011,7 +1012,7 @@ Vec(T&&, Ts&&...) -> Vec<std::remove_cvref_t<T>>;
template <class T>
constexpr T* Vec<T>::alloc_internal_check_cap(usize cap) noexcept {
sus_debug_check(!is_alloced());
::sus::check(cap <= ::sus::mog<usize>(isize::MAX));
::sus::check(cap <= ::sus::cast<usize>(isize::MAX));
T* const new_data = std::allocator_traits<A>::allocate(allocator_, cap);
data_ = new_data;
capacity_ = cap;
Expand All @@ -1022,7 +1023,7 @@ template <class T>
constexpr T* Vec<T>::grow_to_internal_check_cap(usize cap) noexcept {
sus_debug_check(is_alloced());
sus_debug_check(cap > capacity_);
::sus::check(cap <= ::sus::mog<usize>(isize::MAX));
::sus::check(cap <= ::sus::cast<usize>(isize::MAX));
T* const new_data =
std::allocator_traits<std::allocator<T>>::allocate(allocator_, cap);
if constexpr (::sus::mem::relocate_by_memcpy<T>) {
Expand Down
12 changes: 6 additions & 6 deletions sus/collections/vec_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1029,22 +1029,22 @@ TEST(Vec, FromCharArray) {
const signed char SIGNED[] = "abcdefg";
auto v = Vec<u8>::from(SIGNED);
EXPECT_EQ(v.len(), 7u);
EXPECT_EQ(v[0u], sus::mog<u8>('a'));
EXPECT_EQ(v[6u], sus::mog<u8>('g'));
EXPECT_EQ(v[0u], sus::cast<u8>('a'));
EXPECT_EQ(v[6u], sus::cast<u8>('g'));
}
{
const unsigned char UNSIGNED[] = "abcdefg";
auto v = Vec<u8>::from(UNSIGNED);
EXPECT_EQ(v.len(), 7u);
EXPECT_EQ(v[0u], sus::mog<u8>('a'));
EXPECT_EQ(v[6u], sus::mog<u8>('g'));
EXPECT_EQ(v[0u], sus::cast<u8>('a'));
EXPECT_EQ(v[6u], sus::cast<u8>('g'));
}
{
const char CHARS[] = "abcdefg";
auto v = Vec<u8>::from(CHARS);
EXPECT_EQ(v.len(), 7u);
EXPECT_EQ(v[0u], sus::mog<u8>('a'));
EXPECT_EQ(v[6u], sus::mog<u8>('g'));
EXPECT_EQ(v[0u], sus::cast<u8>('a'));
EXPECT_EQ(v[6u], sus::cast<u8>('g'));
}
}

Expand Down
Loading