Skip to content
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

RString utf8 fixes #101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 27 additions & 1 deletion src/class/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct RString {

impl RString {
/// Creates a new instance of Ruby `String` containing given `string`.
/// Not that since Rust strings are assumed to be in UTF-8 encoding, this method is equivalent to `new_utf8`.
///
/// # Examples
///
Expand All @@ -33,7 +34,7 @@ impl RString {
/// str == 'Hello, World!'
/// ```
pub fn new(string: &str) -> Self {
Self::from(string::new(string))
Self::new_utf8(string)
}

/// Creates a new instance of Ruby `String`, with UTF8 encoding, containing
Expand Down Expand Up @@ -61,6 +62,31 @@ impl RString {
Self::from(string::new_utf8(string))
}

/// Creates a new instance of Ruby `String` containing given `string`.
/// This method forces the string's encoding to ASCII-8bit.
///
/// # Examples
///
/// ```
/// use ruru::{RString, VM};
/// # VM::init();
///
/// let string = RString::new_ascii("Hello, World!");
///
/// assert_eq!(string.to_str(), "Hello, World!");
/// ```
///
/// Ruby:
///
/// ```ruby
/// str = 'Hello, World!'
///
/// str == 'Hello, World!'
/// ```
pub fn new_ascii(string: &str) -> Self {
Self::from(string::new(string))
}

/// Retrieves underlying Rust `String` from Ruby `String` object.
///
/// # Examples
Expand Down