0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-28 16:21:04 -05:00

Add String::length method (#39)

This commit is contained in:
Bartek Iwańczuk 2019-12-05 18:28:20 +01:00 committed by Bert Belder
parent f284905f2e
commit 91b8a70fd2
3 changed files with 14 additions and 0 deletions

View file

@ -14,6 +14,10 @@ String* v8__String__NewFromUtf8(Isolate* isolate,
return maybe_local_to_ptr(String::NewFromUtf8(isolate, data, type, length));
}
int v8__String__Length(const String& self) {
return self.Length();
}
int v8__String__Utf8Length(const String& self, Isolate* isolate) {
return self.Utf8Length(isolate);
}

View file

@ -19,6 +19,8 @@ extern "C" {
length: int,
) -> *mut String;
fn v8__String__Length(this: &String) -> int;
fn v8__String__Utf8Length(this: &String, isolate: *mut CxxIsolate) -> int;
fn v8__String__WriteUtf8(
@ -78,6 +80,12 @@ impl String {
}
}
/// Returns the number of characters (UTF-16 code units) in this string.
pub fn length(&self) -> usize {
unsafe { v8__String__Length(self) as usize }
}
/// Returns the number of bytes in the UTF-8 encoded representation of this string.
pub fn utf8_length(&self, isolate: &mut impl LockedIsolate) -> usize {
unsafe { v8__String__Utf8Length(self, isolate.cxx_isolate()) as usize }
}

View file

@ -90,6 +90,8 @@ fn test_string() {
let reference = "Hello 🦕 world!";
let local =
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
assert_eq!(15, local.length());
assert_eq!(17, local.utf8_length(scope));
assert_eq!(reference, local.to_rust_string_lossy(scope));
});
}