Skip to content

Commit

Permalink
Merge pull request #72 from vil02/avoid_sign_conversion_in_utf8_to_runes
Browse files Browse the repository at this point in the history
style: avoid sign conversion in `utf8_to_runes`
  • Loading branch information
mertcandav authored Feb 1, 2024
2 parents ded1922 + 388227b commit 2e15ac4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/utf16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace jule
for (std::size_t index = 0; index < s.length();)
{
jule::I32 rune;
jule::Int n;
std::size_t n;
std::tie(rune, n) = jule::utf8_decode_rune_str(str + index,
s.length() - index);
index += n;
Expand Down
22 changes: 11 additions & 11 deletions api/utf8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace jule

struct UTF8AcceptRange;
std::string runes_to_utf8(const std::vector<jule::I32> &s) noexcept;
std::tuple<jule::I32, jule::Int> utf8_decode_rune_str(const char *s, const jule::Int &len);
std::tuple<jule::I32, std::size_t> utf8_decode_rune_str(const char *s, const std::size_t len);
std::vector<jule::U8> utf8_rune_to_bytes(const jule::I32 &r);

// Definitions
Expand Down Expand Up @@ -340,10 +340,10 @@ namespace jule
return buffer;
}

std::tuple<jule::I32, jule::Int>
utf8_decode_rune_str(const char *s, const jule::Int &len)
std::tuple<jule::I32, std::size_t>
utf8_decode_rune_str(const char *s, const std::size_t len)
{
if (len < 1)
if (len == 0)
return std::make_tuple<jule::I32, jule::Int>(jule::UTF8_RUNE_ERROR, 0);

const auto s0 = static_cast<jule::U8>(s[0]);
Expand All @@ -356,35 +356,35 @@ namespace jule
1);
}

const auto sz = static_cast<jule::Int>(x & 7);
const auto sz = static_cast<std::size_t>(x & 7);
const struct jule::UTF8AcceptRange accept = jule::utf8_accept_ranges[x >> 4];
if (len < sz)
return std::make_tuple<jule::I32, jule::Int>(jule::UTF8_RUNE_ERROR, 1);
return std::make_tuple<jule::I32, std::size_t>(jule::UTF8_RUNE_ERROR, 1);

const auto s1 = static_cast<jule::U8>(s[1]);
if (s1 < accept.lo || accept.hi < s1)
return std::make_tuple<jule::I32, jule::Int>(jule::UTF8_RUNE_ERROR, 1);
return std::make_tuple<jule::I32, std::size_t>(jule::UTF8_RUNE_ERROR, 1);

if (sz <= 2)
return std::make_tuple<jule::I32, jule::Int>(
return std::make_tuple<jule::I32, std::size_t>(
(static_cast<jule::I32>(s0 & jule::UTF8_MASK2) << 6) |
static_cast<jule::I32>(s1 & jule::UTF8_MASKX),
2);

const auto s2 = static_cast<jule::U8>(s[2]);
if (s2 < jule::UTF8_LOCB || jule::UTF8_HICB < s2)
return std::make_tuple<jule::I32, jule::Int>(jule::UTF8_RUNE_ERROR, 1);
return std::make_tuple<jule::I32, std::size_t>(jule::UTF8_RUNE_ERROR, 1);

if (sz <= 3)
return std::make_tuple<jule::I32, jule::Int>(
return std::make_tuple<jule::I32, std::size_t>(
(static_cast<jule::I32>(s0 & jule::UTF8_MASK3) << 12) |
(static_cast<jule::I32>(s1 & jule::UTF8_MASKX) << 6) |
static_cast<jule::I32>(s2 & jule::UTF8_MASKX),
3);

const auto s3 = static_cast<jule::U8>(s[3]);
if (s3 < jule::UTF8_LOCB || jule::UTF8_HICB < s3)
return std::make_tuple<jule::I32, jule::Int>(jule::UTF8_RUNE_ERROR, 1);
return std::make_tuple<jule::I32, std::size_t>(jule::UTF8_RUNE_ERROR, 1);

return std::make_tuple((static_cast<jule::I32>(s0 & jule::UTF8_MASK4) << 18) |
(static_cast<jule::I32>(s1 & jule::UTF8_MASKX) << 12) |
Expand Down

0 comments on commit 2e15ac4

Please sign in to comment.