Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
110 changes: 110 additions & 0 deletions compiler+runtime/include/cpp/jtl/utf8.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <jtl/immutable_string.hpp>

namespace jtl
Comment thread
djblue marked this conversation as resolved.
{
constexpr bool
is_continuation_byte(immutable_string const &s, immutable_string::size_type const i)
{
if(i == immutable_string::npos)
{
throw std::runtime_error{ "Invalid unicode string." };
Comment thread
djblue marked this conversation as resolved.
Outdated
}

return (s[i] & 0xC0) == 0x80;
}

struct utf8_reverse_range
{
utf8_reverse_range(immutable_string const &s)
: data{ s }
{
}
Comment thread
djblue marked this conversation as resolved.
Outdated

struct iterator
{
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = immutable_string;
using reference = value_type const &;

iterator()
: data{}
, i{}
, n{}
{
}
Comment thread
djblue marked this conversation as resolved.
Outdated

iterator(immutable_string const &s)
: data{ s }
, i{ s.size() }
, n{ prev_char_size() }
{
}

constexpr value_type::size_type prev_char_size() const
Comment thread
djblue marked this conversation as resolved.
Outdated
{
if(!is_continuation_byte(data, i - 1))
{
return 1;
}
else if(!is_continuation_byte(data, i - 2))
{
return 2;
}
else if(!is_continuation_byte(data, i - 3))
{
return 3;
}
else if(!is_continuation_byte(data, i - 4))
{
return 4;
}
else
{
throw std::runtime_error{ "Invalid unicode string." };
}
}

value_type operator*() const
{
return data.substr(i - n, n);
}

iterator &operator++()
{
i -= n;
if(i > n)
{
n = prev_char_size();
}
Comment thread
djblue marked this conversation as resolved.
Outdated
return *this;
}

bool operator==(iterator const &it) const
{
return i == it.i;
Comment thread
djblue marked this conversation as resolved.
Outdated
}

value_type data;
value_type::size_type i;
value_type::size_type n;
Comment thread
djblue marked this conversation as resolved.
Outdated
};

iterator begin() const
{
if(data.empty())
{
return {};
}

return { data };
}

iterator end() const
{
return {};
}

immutable_string data;
};
}
9 changes: 8 additions & 1 deletion compiler+runtime/src/cpp/clojure/string_native.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <jtl/utf8.hpp>

#include <clojure/string_native.hpp>
#include <jank/runtime/core.hpp>
#include <jank/runtime/core/call.hpp>
Expand Down Expand Up @@ -26,7 +28,12 @@ namespace clojure::string_native

jtl::immutable_string reverse(jtl::immutable_string const &s)
{
return { s.rbegin(), s.rend() };
Comment thread
djblue marked this conversation as resolved.
jtl::string_builder buff{ s.size() };
for(auto const &c : jtl::utf8_reverse_range(s))
{
buff(c);
}
return buff.release();
}

jtl::immutable_string lower_case(jtl::immutable_string const &s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
clojure.string-test.ends-with-qmark
; clojure.string-test.escape ; Uncaught exception: Can't convert character to integer.
clojure.string-test.lower-case
; clojure.string-test.reverse ; issues with unicode
clojure.string-test.reverse
clojure.string-test.starts-with-qmark
clojure.string-test.upper-case
])
Expand Down
Loading