mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-30 16:41:03 -05:00
Add String::length method (#39)
This commit is contained in:
parent
f284905f2e
commit
91b8a70fd2
3 changed files with 14 additions and 0 deletions
|
@ -14,6 +14,10 @@ String* v8__String__NewFromUtf8(Isolate* isolate,
|
||||||
return maybe_local_to_ptr(String::NewFromUtf8(isolate, data, type, length));
|
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) {
|
int v8__String__Utf8Length(const String& self, Isolate* isolate) {
|
||||||
return self.Utf8Length(isolate);
|
return self.Utf8Length(isolate);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ extern "C" {
|
||||||
length: int,
|
length: int,
|
||||||
) -> *mut String;
|
) -> *mut String;
|
||||||
|
|
||||||
|
fn v8__String__Length(this: &String) -> int;
|
||||||
|
|
||||||
fn v8__String__Utf8Length(this: &String, isolate: *mut CxxIsolate) -> int;
|
fn v8__String__Utf8Length(this: &String, isolate: *mut CxxIsolate) -> int;
|
||||||
|
|
||||||
fn v8__String__WriteUtf8(
|
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 {
|
pub fn utf8_length(&self, isolate: &mut impl LockedIsolate) -> usize {
|
||||||
unsafe { v8__String__Utf8Length(self, isolate.cxx_isolate()) as usize }
|
unsafe { v8__String__Utf8Length(self, isolate.cxx_isolate()) as usize }
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,8 @@ fn test_string() {
|
||||||
let reference = "Hello 🦕 world!";
|
let reference = "Hello 🦕 world!";
|
||||||
let local =
|
let local =
|
||||||
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
|
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));
|
assert_eq!(reference, local.to_rust_string_lossy(scope));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue