Skip to content

Commit

Permalink
Fix compiler warning, and align Symbol and String apis a bit (use lon…
Browse files Browse the repository at this point in the history
…g not size and add string_view constructor).
  • Loading branch information
cfis committed Mar 5, 2024
1 parent 3530a28 commit f7e4bc1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions rice/cpp_api/Symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace Rice
//! Construct a Symbol from an std::string.
Symbol(std::string const& s);

//! Construct a Symbol from an std::string_view.
Symbol(std::string_view const& s);

//! Return a string representation of the Symbol.
char const* c_str() const;

Expand Down
7 changes: 6 additions & 1 deletion rice/cpp_api/Symbol.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ namespace Rice
}

inline Symbol::Symbol(std::string const& s)
: Object(detail::protect(rb_id2sym, detail::protect(rb_intern2, s.c_str(), s.size())))
: Object(detail::protect(rb_id2sym, detail::protect(rb_intern2, s.c_str(), (long)s.length())))
{
}

inline Symbol::Symbol(std::string_view const& view)
: Object(detail::protect(rb_id2sym, detail::protect(rb_intern2, view.data(), (long)view.length())))
{
}

Expand Down
7 changes: 7 additions & 0 deletions test/test_Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ TESTCASE(construct_from_string)
ASSERT_EQUAL(ID2SYM(rb_intern("Foo")), symbol.value());
}

TESTCASE(construct_from_string_view)
{
std::string_view view("Foo");
Symbol symbol(view);
ASSERT_EQUAL(ID2SYM(rb_intern("Foo")), symbol.value());
}

TESTCASE(default_construct)
{
Symbol symbol;
Expand Down

0 comments on commit f7e4bc1

Please sign in to comment.